I want a simple c++ program that accepts four values from the user separated by commas and displays the average.
For example the user will input "20,21,22,23" without quotes and the software will output 21.5
C++ program about accepting values and displaying them?
Hi ..
For the 1st look it seems easy but the commas make it a Little ..........
try to use "scanf" function unlike "cin" its formatted Input function .
you need " include%26lt;stdio.h%26gt; " to use "scanf"
http://en.wikipedia.org/wiki/Scanf
include%26lt;stdio.h%26gt;
void main()
{
float x1,x2,x3,x4,avg;
printf("Enter Four Numbers : \n");
scanf("%f,%f,%f,%f" ,%26amp;x1, %26amp;x2, %26amp;x3, %26amp;x4);
avg=(x1+x2+x3+x4)/4;
printf("%f", avg);
}
I don't have C compiler now to check if any errors but i hope this will help ..
best regards ...
Reply:I`m happy you find it helpful ... Report It
Reply:get input as string
break them up
convert into int using atoi
perform rest requirements
Reply:It is a very basic and easy program, do it yourself.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment