而且&如果循环预期错误

时间:2016-11-04 12:02:23

标签: c++

我需要编写一个程序,从键盘读取数字,计算数字的平均值,然后显示它。数字序列将以零结束。

这是我的代码:

#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;
}

然而,我遇到两个错误:

  

预期')'

  

if语句有空体

5 个答案:

答案 0 :(得分:1)

if(count! = 0.0);

摆脱分号

答案 1 :(得分:1)

有三个错误:

  • !=运算符在两个地方拼写错误! =
  • if在右括号后面有分号。

答案 2 :(得分:0)

if语句后面有一个分号(;)。删除它:)

if(count! = 0.0)

答案 3 :(得分:0)

while(number! = 0.0)应为while(number != 0.0)

if(count! = 0.0)应为if(count != 0.0)

注意!=是运营商,但! =不是 - 注意细节!

答案 4 :(得分:0)

请不要在比较器之间插入任何空格。

你写了(number! = 0.0),但正确的应该是:(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;
}