Assuming you have recently started writing C++ programs, you should keep three goals in mind:
(1) Make your program compile
In your current program you have several compiler errors. E.g. you are missing a semicolon in the first cout
statement. You have not declared the type of sum, etc.
(2) Test your program to get correct output and make corrections if needed.
Your average is declared as int
rather than double
. So your result would not be accurate. Further, in your average calculation statement, you are not keeping the operands grouped in a bracket. Also, you don't want to do integer division which results in an integer than a double.
(3) Improve the efficiency of your program.
E.g. you have already calculated the sum. While calculating the average, you can just reuse the sum instead of summing the values again.
See the following working snippet:
int main()
{
int val1, val2, val3;
double avg;
cout << "Please enter 3 integers, separated by spaces: ";
cin >> val1 >> val2 >> val3;
int sum = val1 + val2 + val3;
cout << "The sum = " << sum;
avg = (val1 + val2 + val3) / 3.0;
cout << "The average = " << avg << endl;
return 0;
}