-4

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 ')'

and

if statement has empty body

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
HippoEug
  • 65
  • 1
  • 1
  • 8
  • If you want to share solution post an answer, don't put solutions in the question. Question posts are only for holding questions :) – BartoszKP Nov 04 '16 at 15:00

5 Answers5

1
if(count! = 0.0);

Get rid of semicolon

Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
1

There are three errors:

  • The != operator is mis-spelled ! = in two places.
  • The if has a semicolon after the closing parentheses.
unwind
  • 391,730
  • 64
  • 469
  • 606
0

You have a semi-colon (;) right after the if statement. Remove it :)

if(count! = 0.0)
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
0

while(number! = 0.0) should be while(number != 0.0)

and

if(count! = 0.0) should be if(count != 0.0)

Note != is an operator but ! = is not - attention to the details!

artm
  • 17,291
  • 6
  • 38
  • 54
0

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;
}
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Kalle
  • 16