This program prints a specified amount of numbers within a specified range. However, when I enter a character, it just endlessly loops whichever do-while loop I do it in. E.g: If I enter a character in "Enter maximum number" cin, it just spams "Enter maximum number" endlessly, it just skips the cin and loops the cout (same goes for the other 2 do-whiles. Anyone know why?
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int roll(int mini, int maxi)
{
int v = maxi - mini;
int x = mini + (rand() % (v+1));
return x;
}
void caller()
{
int a;
int b;
int c;
do {
cout << "Enter minimum number" << endl;
cin.clear();
cin >> a;
} while (cin.fail());
do {
cout << "Enter maximum number" << endl;
cin.clear();
cin >> b;
} while (cin.fail() || a > b);
do {
cout << "How many rolls?" << endl;
cin.clear();
cin >> c;
} while (cin.fail());
for (int i = 0; i < c; i++)
cout << roll(a, b) << endl;
}
int main()
{
srand (time(NULL));
caller();
return 0;
}