c++ - 错误 : expected unqualified-id before 'while' |

标签 c++

在第 12 行继续出现此错误

#include <iostream>

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

最佳答案

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

此外,您的 cin <<cout <<语句缺少 namespace 标记 std:: .因此,您需要添加 std::在您使用的每个实例中 cincout请看下面的代码:

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

关于c++ - 错误 : expected unqualified-id before 'while' |,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42940136/

相关文章:

c++ - C++(MFC,而非 .NET)应用程序的程序集重定向

c++ - 在 QScrollBar 可见性发生变化时获取 QScrollBar 宽度

c++ - Range-v3 view::sliding(n) 返回元组(如果 n 在编译时已知)

c++ - jom.exe 返回退出代码 2 和许多 Boost 警告

c++ - 有什么方法可以只使用类运算符进行转换吗?

c++ - 如何比较 std::map 中的所有项目?

c++ - boost::program_options 中的短参数没有长

c++ - 如何在 C++ 中修复 "invalid use of incomplete type"

c++ - 在 C++ 中使用指向 char 数组的指针

c++ - 20 个问题游戏 - Switch 语句