HippoEug HippoEug - 28 days ago 16
C++ Question

While & If loop expected error

I need to write a program that would read in numbers from the keyboard, compute the average of the numbers, and display it. The sequence of numbers are to be terminated with a zero.

Here is my code:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int count;
double number, sum, average;

count = 0;
sum = 0;
average = 0;

cout << "\nEnter Number (0 to terminate): ";
cin >> number;

while(number! = 0.0)
{
sum = sum + number;
count = count + 1;
cout << "\nEnter Number: ";
cin >> number;
}
if(count! = 0.0)
{
average = sum/count;
}

cout << "\nThe average of the" << count << "number is" << average << endl;
return 0;
}


However, I am getting two errors:


expected ')'


EDIT : THANK YOU ALL SO MUCH FOR THE RESPONSE! EVEN THOUGH I GOT DOWNVOTED, THIS WEBSITE IS AWESOME! THANKS ONCE AGAIN!

Answer

Please do not insert any whitespaces between your comparators.

You wrote (number! = 0.0), but the correct one should be: (number != 0.0).

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
    int count;
    double number, sum, average;

    count = 0;
    sum = 0;
    average = 0;

    cout << "\nEnter Number (0 to terminate): ";
    cin >> number;

    while(number != 0.0)
    {
        sum = sum + number;
        count = count + 1;
        cout << "\nEnter Number: ";
        cin >> number;
    }
    if(count != 0.0)
    {
        average = sum/count;
    }

    cout << "\nThe average of the" << count << "number is" << average << endl;
    return 0;
}
Comments