gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:59:37 24 4
gpt4 key购买 nike

你好我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com