函数 require() 中的循环需要 3 个条件,a > b 或“a”或“b”不是数字。即使我不满足条件并放入 2 个整数,它也会再次循环。
此外,当我输入一个字符时,它只是无休止地循环“输入最小数字输入最大数字”而忽略了 cins。有谁知道为什么?我是初学者,所以这可能真的很明显
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int random(int minN, int maxN) //generates random number within specified range
{
srand (time(NULL));
int x = (maxN - minN);
int y = minN + (rand() % (x+1));
return y;
}
int require() //makes sure a < b and both are digits
{
int a,b;
do {
cout << "Enter minimum number" << endl;
cin >> a;
cout << "Enter maximum number. Note: Has to be greater or equal to minimum." << endl;
cin >> b;
} while (a > b || !isdigit(a) || !isdigit(b));
return random(a,b);
}
int main()
{
cout << require() << endl;
}