Hi, I am new to C++, self learning, and not really sure why this is happening, here are the details.
I prompt the user for an int input between 1 and 100 (inclusive) or 0 to exit...
When a valid (0-100) reply is entered, everything works fine. The problem is a non-int or over 100 value is entered, my function is supposed to inform them that was an invalid entry, and recall the function for the input to allow them to retry or again enter 0 to exit.
However, an invalid entry results in the function looping (seeming) endlessly with the invalid entry message. Here is the code:
Code:
void Input()
{
int depth = 0;
cout<< "Enter the number (1-100), 0 to exit.\n";
cin>> depth;
if(depth > 0 && depth < 101)
{
// code removed... operates as expected
}
else if(depth == 0)
{
// code removed... operates as expected
}
else
{
cout<< "Your entry was not valid, please try again\n";
cout<< "Enter 0 to Exit\n";
Input();
}
}
I do know (what I think is the long way) to accomplish this by recalling the function from within main after an invalid entry, or is that the shortest way possible?
Is it just not possible to self-call a function?
Is it possible, but poor practice?
ANY teaching offered on anything would be much appreciated, for example it seems to me there would be a better way to prompt for an input and filter it, but so far this is the best way I have learned, most frequently expected answers on the first filter etc
(ps... I have read many posts in searching for help on this, however FYI I do not want the answer written out for me, just some tips on which direction to travel)