Hi, I was wondering if anyone could help me out in making any number greater than 20 and less than 3 give an error message that redirects the user to inputting another value again. I have it to work when they first input a wrong value but then it does not work. Also when i try to input a special character such as 'a', 'b', and 'c' or even a word I get stuck in an infinite loop.
if(limit != 1)
{
cout << "OOPS! Looks like you typed some bad data here!" << endl;
cout << "The acceptable dimension should range from 3 to 20, so choose carefully..." << endl;
cout << "Side Size: ";
cin >> limit;
count1 = 1;
cout<< '+';
}
Executing things in a certain way? Also i tried your suggestion and still could not get it to work :/ i've spent hours on trying to get this done and its becoming a bit frustrating [Insert crying emoji]
while(!cin > a && a > 3 && a < 20)
{
cout << "oh no! It seems you entered invalid input, try again.\n";
}
/* if(limit > 20 || limit < 3)
{
cout << "OOPS! Looks like you typed some bad data here!" << endl;
cout << "The acceptable dimension should range from 3 to 20, so choose carefully..." << endl;
cout << "Side Size: ";
cin >> limit;
count1 = 1;
cout<< '+';
} */
cout <<"To try my shape generator program again type Y for Yes and N for No: ";
cin >> doAgain;
Yeah my code, just doesn’t work, sorry, I guess I answered to fast. You could use sstream to first check for a number, and then either input again or just give the number to a. Like so...
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <sstream>
bool isDigit(string a){
return (int)a[0] < (int)'9' && (int)a[0] > (int)'0';
}
//...
string in;
cin >> in;
while(!isDigit(in)){
cout << "try again";
cin >> in;
}
stringstream(in) >> a; // temporarily make string stream so can insert number into a