I need the user to input a 7 digit number that will later be output back. If the user inputs a number shorter or longer, they will be prompted to re-input a 7 digit number. When it comes time to output their number, the initial number input will be output, not the last correct 7 digit one. Can anyone help?
When validating input, I find it easiest to break out of the middle of the loop. Something like this:
1 2 3 4 5 6 7 8
while (true) {
// Prompt for input
// Get the input
// If the input is valid then break
// else tell the user what's wrong with the input
}
// When you get here, the input is valid.
Here is what i have. I just don't understand why only the initial input is saved into 'idNumber'. How can the newest input override the previous one(s)?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
while (true) {
cout<<"\nPlease Enter you 7 Digit ID: "; //prompting for id
cin>>idNumber;
if (idNumber.length() ==7) {
cin>>idNumber;
break;
}
else {
for (string idNumber;idNumber.length()<7||idNumber.length()>7;){
cout<< "\nPlease Enter your 7 Digit ID: ";
cin>> idNumber;
}
}
> I just don't understand why only the initial input is saved into 'idNumber'.
What part of "you have TWO variables with the same name" is confusing you?
> for (string idNumber;idNumber.length()<7||idNumber.length()>7;)
This is a BRAND NEW VARIABLE, that has nothing to do with the other one with the same name.
If you write to this one, then whatever you store in it is LOST when the for loop exits.
1 2 3 4
do {
cout<< "\nPlease Enter your 7 Digit ID: ";
cin>> idNumber;
} while ( idNumber.length() != 7 );