所以问题是当我在VS 2015中编译并运行程序时,我把一个字符串作为第一个cin,它写了“错误的输入”就像我打算做的那样。但是,当我在codeblocks中构建并运行它并且我输入一个字符串作为第一个cin时,控制台开始无休止地吐出连续的自然数字,我不知道发生了什么。
#include <iostream>
#include <cmath>
using namespace std;
void printPrimes(int a, int b)
{
for (int i = a; i <= b; i++)
{
int x = 1;
for (int j = 2; j <= sqrt(i); j++)
{
double temp;
temp = i % j;
x++;
if (temp == 0) break;
else if (temp != 0 && x == floor(sqrt(i))) cout << i << endl;
}
}
}
int main()
{
cout << "Insert interval:" << endl;
int a, b;
cin >> a >> b;
if (cin.fail() || (a > b))
{
cout << "Wrong input." << endl;
}
if (a < 0)
{
a = 0;
}
printPrimes(a, b);
return 0;
}