int main()
{
int AskRange;
while ( true )
{
cout << " Please enter a value that is greater than zero \n";
cin >> AskRange;
cin.ignore();
if ( AskRange > 0 )
{
break;
}
else
{
}
}
return(0);
}
how come when i enter a letter is spams me?
what am i doing wrong ?
You can just add a parameter to your cin.ignore().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int AskRange;
while (true)
{
cout << " Please enter a value that is greater than zero \n";
cin >> AskRange;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (AskRange > 0)
{
break;
}
else
{
}
}
My prof. hasn't taught any of this lol and by that i mean the "is digit" thing and the cin.ignore() parameters, so that makes me believe there must be a different way of doing it. Thanks for helping me by the way.
If your professor hasn't taught you the necessary tools to be able to handle this problem, then don't worry about it. He will (hopefully) teach you about these things in your future.
Yeah you're right haha, i read the sheet again and it just says " check that the user indeed enters a positive number " so, he shouldn't be entering a letter when compiling it.
By the way, just a thought. Possibly it's set up so that you realize it spams you when a letter is inputted, making for a great transition to the next lesson (which would be stringstreams)?