re-entering information.

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;
Last edited on
have you considered a do while instead of an if? -- line 7
um well we haven't covered that in class but if that's how i'd do it, I'll look it up. Any pointers on how to use it?
giblit is right - you will want to do a do-while loop. A basic example:
1
2
3
4
5
6
7
8
9
10
	string inputVar = "";
	do
	{
		cout << "Enter the letter m: ";
		getline (cin,inputVar);
	}
	while (inputVar != "m");

	cout << "m was entered, Exited loop" << endl;
	system("pause");

This will do your command "while" the inputVar does not equal "m". Once it is equal to m it will exit that loop and move on.
1
2
3
4
5
6
7
8
9
10
	cout<<"1. Single"<<endl
		<<"2. Married Filing Seperately"<<endl
		<<"3. Married Filing Jointly"<<endl
		<<"4. Head of Household"<<endl<<"Select filing status: ";
	cin>>status;
	do{cout<<"\nERROR: invalid selection."<<endl;
		
		cout<<"Please chose valid status from above:";
		cin>>status;
	}while(status<0 || status>4);

Ok this is what i've gotten so far, and it seems that no matter what value i put in, it will still show the error.

How do I fix this?
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?
well I changed it to
1
2
3
4
5
6
7
8
9
10
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. :)

For simplicity purpose I would do:
 
}while (status != 1 || status != 2) //etc 


But it depends on how strict your teacher is.
Oh, I see now, thanks for the help!
Topic archived. No new replies allowed.