starting over if statements

I ask a person to enter in if they're male or female by typing m or f, but if they type something other than M, m, F, or f I want and an error message to happen and have them try again. The problem is the program continues on whether or not the correct character was typed in. How do you start the if statement over until the right character is typed in?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  cout << "Gender (M or F): " << endl;
   cin >> gender;
   
   
   if(gender == 'M' || gender == 'm'){
      gender = 8;
   }
   else if(gender == 'F' || gender == 'f'){
      gender = 10;
   }
   else{
      cout << "Not a valide answer" << "Please enter M for male or F for female" << endl;
   }
You will want to do a do while loop or create a function that will call for users input

1
2
3
4
5
do 
{
all your code here
}
while (gender != "m" || "M" || "f" || "F" }


That will keep asking the user for their gender until they enter one of the valid inputs you want.
Last edited on
it asks the user over and over and over and wont let any new input in
Oh sorry. It is because my do while loop is checking for "m" and "f" not 8 or 10 as the variables you set. You will have to update that so it says to do the loop while gender != 10 or 8.

Also, is your gender variable a char or an int? you can't have it be 'm' and then set it to 8.
it's a char and the do loop has to be m, f, M, or F. 8 or 10 won't work.
Why are you setting gender = 8 and gender = 10 in your code?

Also, since it is char you will have to do:

 
while (gender != 'm')

instead of "m" you will have 'm' to represent a char not a string.
Last edited on
because if the user types in m then the gender = 8 and if the user types in f then gender = 10
You should use a different variable since gender is a char and you are then you are trying to save gender as an integer?

Past your new code and I can look at it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cout << "Gender (M or F): " << endl;
   cin >> gender;

do{
      if(gender == 'M' || gender == 'm'){
         gender = 8;
      }
      else if(gender == 'F' || gender == 'f'){
         gender = 10;
      }
      else{
         cout << "Not a valide answer" << endl <<
         "Please enter M for male or F for female" << endl <<
         "Gender (M or F): " << endl;
      }
   }
while(gender != 'm' || 'M' || 'f' || 'F');
You'll want to put the do { before it prompts the user. Otherwise it just keeps looping back to check the input to see if it changes, and since it won't change (because the loop doesn't prompt the user to change it) it will keep going forever.

You also won't need to prompt the user for a second time since it will fail and start from the do { again.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>;
#include <iostream>;

using namespace std;

void main()
{
	string inputVar = "";
	do
	{
		cout << "enter the letter m: ";
		getline (cin,inputVar);
	}
	while (inputVar != "m");

	cout << "Exited loop";
	system("pause");
}
Last edited on
well I still can't get out of the loop. It starts back at the start and the user can input new letters, but if the right letter is typed the loop still loops.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

while(gender != 'm' || 'M' || 'f' || 'F'){
      cout << "Gender (M or F): " << endl;
      cin >> gender;
      
      if(gender == 'M' || gender == 'm'){
         gender = 8;
      }
      else if(gender == 'F' || gender == 'f'){
         gender = 10;
      }
      else{
         cout << "Not a valide answer" << endl <<
         "Please enter M for male or F for female" << endl;
      }
   }
Like i mentioned before, it is because you are overwriting the users input with numbers 8 and 10. You need to have them be different variables.

What happens is the user puts in "m" and your code sets 'm' as gender. Then your if statement sees that and changes 'm' to 8.

Then when it gets to the end of the statement it asks if gender is 'm' or 'f' (which it is not anymore, it is an 8 or a 10) so it repeats the loop.

Set a new variable like:
1
2
3
4
5
6
int genderCode

while (gender !=...)
{
if (gender == m) { genderCode = 8; }
}


Then when the code finishes, gender will still be an 'm' or 'f' and exit the loop.
still busted I tried every way :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

do{
   cout << "Gender (M or F): " << endl;
      cin >> gender;
      
      if(gender == 'M' || gender == 'm'){
         person = 8;
      }
      else if(gender == 'F' || gender == 'f'){
         person = 10;
      }
      else{
         cout << "Not a valide answer" << endl <<
         "Please enter M for male or F for female" << endl;
      }
}while(person != 8 || 10);
Your last while statement is off.
It should be:
 
while(person != 8 && person != 10)

Also, the semicolon after the while statement is not necessary.
Hmm. You could change the setup a bit to:
1
2
3
4
5

do {
your code here
}
while (true);


then in your if statements you can do a:
1
2
3
4
5
if (gender == 'M' || gender == 'm')
{
person = 8;
break;
}


that will break you out of the loop. I think part of the issue is you are using cin and never convert the users input to a char. Sorry I'm a beginner myself so i'm working it through with you :)
if I use && that means person must equal 8 and 10 at the same time. And my program needs the semicolon at the end or it will not work
Okay, you're right about the semicolon; however, the while I used makes sense.
 
while(person != 8 && person != 10)


It's saying while "person" is not equal to 8 and "person" is not equal to 10, continue to loop.
Once person is equal to 8, the loop will terminate because it is now equal to 8. Both conditions need to be met for the loop to continue. Once "person" becomes equal to either 8 or 10, the loop will terminate.
ok that make sense, thanks.
Topic archived. No new replies allowed.