-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
  • 33,416
  • 13
  • 100
  • 127
HippoEug
  • 55
  • 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
  • 11,445
  • 5
  • 53
  • 73
1

There are three errors:

  • The != operator is mis-spelled ! = in two places.
  • The if has a semicolon after the closing parentheses.
unwind
  • 378,987
  • 63
  • 458
  • 590
0

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

if(count! = 0.0)
Moo-Juice
  • 37,292
  • 10
  • 72
  • 122
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
  • 16,750
  • 4
  • 31
  • 51
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,636
  • 22
  • 26
  • 40
Kalle
  • 16