c++ - 编译 main.cpp :3:1: error: expected unqualified-id before ‘do’ do ^ what do i do? 时我一直收到此错误

标签 c++

你好我是 C++ 的新手,我正在做一个项目 我在编译时不断收到此错误

main.cpp:3:1: 错误:'do' 之前需要不合格的 id 做 ^

这是代码

int a,b,i,j,sum=0;

do
{   cout << "Enter a number: ";
    cin >> a;
    if (a < 4 || a > 1000000) 
    {   cout << "Input must be between 4 and 1000000 inclusive." << endl;
    }
}while (a < 4 || a > 1000000);

do
{   cout << "Enter a second number: ";
    cin >> b;
    if (b < 4 || b > 1000000) 
    {   cout << "Input must be between 4 and 1000000 inclusive." << endl;
    }
}while (b < 4 || b > 1000000);

if (a > b)
{   int hold;
    hold = b;
    b = a;
    a = hold;
}

cout << "The prime numbers between " << a << " and " << b << " inclusive are: " << endl;
//int sum;
for (i = a; i <= b; i++)
{
 for (j = 2; j <= i; j++) // Changed the < to <=, and got rid of semicolon
 {
    if (!(i%j)&&(i!=j)) break;
    if (j==i) 
    {
              cout << i << endl;
              sum += i;
              cout << sum ;

    }
 }
}

最佳答案

就像之前所说的那样,您的代码确实需要在函数中。尝试用

包裹它
int main () {

//your code here

} 

关于c++ - 编译 main.cpp :3:1: error: expected unqualified-id before ‘do’ do ^ what do i do? 时我一直收到此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22677031/

相关文章:

c++ - SQLite 'Unrecognized token: ":"C++

c++ - 使用函数作为另一个函数输入

c++ - 新别墅ACM解决攻略

c++ - 数组未通过 cout 打印出来

c++ - gluTessCallback 错误 C2440

c++ - 无法访问功能

c++ - 具有不同参数顺序的聚合初始化

c++ - 运算符重载导致模板参数推导失败

c++ - 为什么使用 “b < a ? a : b” 而不是 “a < b ? b : a” 来实现最大模板?

c++ - 如何选择编译应用程序的 Visual C++ 库版本?