Reputation: 33
I have a function that is being called after an input for validation. It will check for the range of numbers in groupsize. They cannot be smaller than 1 and no greater than 10. I am also validating it for invalid characters such as letters and symbols.
int validateGroupSize(int groupsize[], int i)
{
while((groupsize[i] < 0 || groupsize[i] > 11) || (!(cin >> groupsize[i])))
{
cout << string(60, '\n');
cout << "Please re-enter customer's group size. " << endl << "Input must be a number and must not be smaller than 1 and greater than 10: " << endl;
cin >> groupsize[i];
cin.clear();
cin.ignore(20, '\n');
}
return groupsize[i];
}
int main()
{
int groupsize[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int i;
char newentry = 'n';
do
{
cout << string(60, '\n');
cout << endl << endl << "Please enter customer's group size: " << endl;
groupsize[i] = validateGroupSize(groupsize, i);
i++;
cout << "Would you like to enter another questionare? Enter either 'y' or 'n': " << endl;
cin >> newentry;
}while((newentry == 'y') || (newentry == 'Y'));
system("pause");
return 0;
}
So far the program works for invalid characters, as when I input a letter it will prompt me with the error message. Unfortunately when I input a number that is greater than 10 it will simply ignore the condition and continue with the rest of the program. What do I do wrong?
Upvotes: 0
Views: 6219
Reputation: 96790
The main problem is that your code uses an uninitialized variable i
to access the array elements of groupsize
. Doing so invokes undefined behavior (UB) in your program, making any resulting unwanted behavior well within the rights of the compiler.
The first place where this happens is this line:
groupsize[i] = validateGroupSize(groupsize, i);
^^^^^^^^^^^^
i
should be initialized to 0
at construction, so the above line would be valid. Moreover, it would make the subsequent increment of i
valid as well.
Inside your validateGroupSize()
function your condition was:
while ((groupsize[i] < 0 || groupsize[i] > 11) || (!(cin >> groupsize[i]))
which is wrong, because you're executing your validation constraints before a value had been extracted into the array element. If groupsize
wasn't given default values prior to this code (all zeros), this code would be yet another candidate for undefined behavior.
To fix this, simply swap the two conditions: make the extraction occur before the actual validation.
And lastly, remove the nested cin >> groupsize[i]
. If the condition in the parameters returns true, and the loop doesn't break, the extraction will be performed when the loop executes again.
As JohnnyMopp points out, your validating condition should be changed to what he shows.
Upvotes: 2
Reputation: 13533
First, if you want 1 <= groupsize <= 10, your test should be:
groupsize[i] < 1 || groupsize[i] > 10
while(!(cin >> groupsize[i]) || groupsize[i] < 1 || groupsize[i] > 10){
cin.clear();
cin.ignore(20, '\n');
cout << "Please re-enter customer's group size. " << endl << "Input must be a number and must not be smaller than 1 and greater than 10: " << endl;
}
Upvotes: 0