Help Loop program.

Hello!!

Trying to create a program that will not move forward until the proper input is accepted.

here is part of my code.

cout << "Please enter s for single, m for married: ";
string in_marital_status;
cin >> in_marital_status;
if ( in_marital_status == "m" || in_marital_status == "s")
marital_status_ok = true ;
else
{
cout << "m or s only, please try again ";
cout << "Please enter s for single, m for married: ";
cin>> in_marital_status;
}

As I run this part it will reject the first wrong input however the second input will be accepted be it "m" or "s".

Any help would be gladly appreciated.
try:
1
2
3
4
5
6
7
8
9
10
11
12
#include <string>
using namespace std;

int main() {
	string in_marital_status;
	do {
		cout << "Please enter s for single, m for married: ";
		cin >> in_marital_status;
	} while(!(in_marital_status == "m" || in_marital_status == "s"));
	return(0);
}
	

you only using an if, you need to loop over the code, until correct input is given
Thank you Script Coder.

How about accomplishing it without using do loop.
It is possible, you should try implement it yourself.
rynzor6,
Hint Hint,Use This Code And Don't Let Script Coder Know!!!(Just Joking)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #include <iostream>
#include <string>
using namespace std;
int main()
int married_or_No;
cout <<"Type in m for married and s for single";
cin >> married_or_No;
if (married_or_No == "m"){
//What Ever You Would Like To Happen
}
elseif (married_or_No =="s"){
//whatever You would Like TO Happen
}
else {
return (0);
}

Sorry IF There Isn't Alot Of ';' Where There Should Be.Im Lazy (;
Topic archived. No new replies allowed.