错误:'while'|之前的预期unqualified-id

时间:2017-03-21 23:32:59

标签: c++

继续在第12行获取此错误

#include <iostream>

int num = 1;
int number;
int total = 0;
while (num<=5){
    cin >> number;
    total +=number;
    num++;
    cout<< total<<endl
}

1 个答案:

答案 0 :(得分:0)

您的代码缺少int main(void)函数,其中所有代码都应该在其中。根据定义,C ++程序必需具有int main(void)功能。您需要将所有代码放在int main(void)函数中。

此外,您的cin <<cout <<语句缺少命名空间标记std::。因此,您需要在您使用的每个实例std::cin添加cout,请参阅以下代码:

#include <iostream>

int main(void) // The main() function is required in a C++ program
{
    int num = 1;
    int number;
    int total = 0;
    while (num <= 5)
    {
        std::cin >> number; // You need to add the reference to std:: as "cin <<" is a part of the std namespace
        total += number;
        num++;
        std::cout << total << std::endl // You need to add the reference to std:: as "cout <<" and "endl <<" is a part of the std namespace
    }

    return 0;
}