I used the function in the FAQ to reject a non integer input when
an integer is required:
Code:
while(iINum<1||iINum>4)
{
InvalidInputChar("How many? (1-4)\n",iINum);
if(iINum<1||iINum>4)
{
cout<<"\nInvalid input"<<endl;
}
//Reject NON-INTEGER input
void InvalidInputChar(string arg1,int arg2)
{
while (cout<<arg1 && !(cin>>arg2))
{
cout<<"\nInvalid input"<<endl;
cin.clear();
cin.ignore(std::numeric_limits<int>::max(),'\n');
}
iINum=arg2;
}
As you can see, the top snippet calls the function at the bottom. The
point of this code is to force the user to enter an integer between
1 and 4, inclusive. I have to go through the function to weed out
a non integer input. The trouble is, it also reset arg2 to zero, no matter what the user enters. Since I am a novice, I admit that I do not know how the
two cin calls work. I removed both of them individually and had the
same problem. Since iINum is the argument, I thought it made sense
that whatever was inserted in the function call would stay there, but
that is not so. When the resulting iINum is fed through the
[if iINum<1||iINum>4] snippet, the result was always "invalid input".
After about an hour of work I figured out that the only thing I
needed to do was assign iINum to arg2, essentially telling it to equal
itself, since iINum IS arg2. I tried using a third argument, having
iINum equal it, and still the result was zero. Only when I had
something equal arg2 within the function did that something maintain
a nonzero value. However, when I had iINum equal arg3 (as a dummy
variable) externally, it was zero. I do not understand why having
iINum equal itself solved this problem.
Perhaps somebody else has had this or a similar problem and knows why
it happened? Is there a better way to accomplish this function? I
would appreciate any help.