-5

I need to fix this code but i keep getting expected unqualified -id

I've tried placing the brackets at different positions and changed the semicolon to a colon but nothing happened

int main();

{int val1, val2, val3;
int avg;

cout << "Please enter 3 integers, separated by spaces: "
cin >> val1 >> val2 >> val3;
sum = val1 + val2 + val3;
cout << "The sum = " << sum;
avg = val1 + val2 + val3 / 3;
cout << 'The average = ' << avg << endl;
return 0;
}

I was expecting to get the average.

3 Answers3

3

You misplaced a ; after main. It needs to be removed. You didn't include the necessary header file for cin and cout. Forgot to declare sum variable. Forgot to include ; after "separated by space: ". Forgot to enclose the parentheses when computing the avg variable. So it computes val/3 added with val1 and val2, not val1, val2 and val3 added, then divided by 3. You used character literal '', but here you're printing a string literal composed of several characters. So you need to place "" surrounding them. Use double/float to correctly compute avg because int will truncate the result. And the second operand of division operator needs to be 3.0 instead of 3 to avoid integer division.

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main(){
    int val1, val2, val3;
    cout << "Please enter 3 integers, separated by spaces: ";
    cin >> val1 >> val2 >> val3;
    int sum = val1 + val2 + val3;
    cout << "The sum = " << sum;
    double avg = sum / 3.0; // or (val1 + val2 + val3)/3.0
    cout << " The average = " << avg << endl;
    return 0;
}
Robur_131
  • 374
  • 5
  • 15
  • Declaring `avg` as an integer will not get the correct average for anything that isn't divisible by 3. Also, you already have the `sum` variable, as Jarod42 mentions in a comment on the original question. – computercarguy Aug 22 '19 at 22:27
  • 2
    Thanks for pointing it out @computercarguy. I've edited my answer. Please have a look. – Robur_131 Aug 22 '19 at 22:31
0

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;
}
VHS
  • 9,534
  • 3
  • 19
  • 43
  • 1
    You mentioned reusing `sum`, but then forgot to do so. – computercarguy Aug 22 '19 at 22:33
  • @computercarguy. correct. I wanted to show the OP the requirement of grouping too. – VHS Aug 22 '19 at 22:35
  • I think having a commented line in there to show the correct grouping, then another comment saying that the uncommented line "below" has the appropriate streamlining would be appropriate in this case. The OP would also need to know to remove the comments in their final code, though. – computercarguy Aug 22 '19 at 22:39
-1

Step 1: after main() method you should not use semicolon

Step 2: Consider your need for output Do you just need the right part of the number or do the numbers matter to you? (My code will consider decimal places)

Step 3: The priority of mathematical operators must be respected

Code:

int main()
{
float val1, val2, val3, sum, avg;

cout << "Please enter 3 integers, separated by spaces: "
cin >> val1 >> val2 >> val3;
sum = val1 + val2 + val3;
cout << "The sum = " << sum;
avg = sum / 3.0f;
cout << 'The average = ' << avg << endl;
return0;}
ali sarkhosh
  • 183
  • 1
  • 12