ok so i'm working on a program for a class and I want to know how to make someone re-enter data if they have an invalid selection, rather than just stopping the program or moving on with an invalid selection. Thanks for the help.
1 2 3 4 5 6 7 8 9 10 11 12
cout<<"1. Single"<<endl
<<"2. Married Filing Seperately"<<endl
<<"3. Married Filing Jointly"<<endl
<<"4. Head of Household"<<endl<<"Select filing status: "<<endl;
cin>>status;
if(status<1 || status>4)
{cout<<"Error invalid selection."<<endl;
system("pause");
exit(1);}
cout<<"\n\nNumber of Dependents: ";
cin>>dependents;
The reason it is repeating is because you do while loop just takes you back to check the data. You need to move your do { up to the top so it prompts the user to input data again. Does that make sense?
cout<<"1. Single"<<endl
<<"2. Married Filing Seperately"<<endl
<<"3. Married Filing Jointly"<<endl
<<"4. Head of Household"<<endl<<"Select filing status: ";
cin>>status;
while(status<1|| status>4)
{cout<<"\nERROR: invalid selection."<<endl;
cout<<"Please chose valid status from above:";
cin>>status;}
and it's doing exactly what I want it to but i see what you mean.
it'd have to be
1 2 3 4 5 6 7 8 9 10
do{cout<<"1. Single"<<endl
<<"2. Married Filing Seperately"<<endl
<<"3. Married Filing Jointly"<<endl
<<"4. Head of Household"<<endl<<"Select filing status: ";
cin>>status;
cout<<"\nERROR: invalid selection."<<endl;
cout<<"Please chose valid status from above:";
cin>>status;
}while(status<0 || status>4);
That's correct! That way if the status < 0 || status > 4, it will go up to the beginning and prompt the user again to input a new number. There is one issues with using ranges like that though, if I enter "1.2" it will take that as valid data. :)