在第 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::
在您使用的每个实例中 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;
}
关于c++ - 错误 : expected unqualified-id before 'while' |,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42940136/