loops for invalid entries

im having a problem with this loop where if a person does not enter m, f, or x it should loop to keep asking for a non-valid gender or to exit. however it asks for a gender and if you enter a non-valid gender it still goes onto the height question. my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <iomanip>
#include "conio.h"
using namespace std;

void main() {
	char gender;
	bool heightok;
	bool weightok;
	int height;
	int weight;
	
	do{
	cout << "please enter the candidates information (Enter 'x' to exit) " << endl;

	cout << setw(10) << "Gender: ";
	gender = ('x' || 'X' || 'F' || 'f' || 'm' || 'M') ;
	cin >> gender;

	
	if (gender != ('x' || 'X' || 'F' || 'f' || 'm' || 'M')){

		cout << "invaild entry" << endl;

	
	}

	if (gender = 'f' || 'F') {
		
	cout << setw(10) << "height: ";
	cin >> height;
	heightok = (height >=24 && height <= 110 && height >=60 && height <= 72);
	cout << heightok;

	cout << setw(10) << "weight: ";
	cin >> weight;
	weightok = (weight >= 50 && weight <= 1400 && weight >= 100 && weight <= 185);
	cout << weightok;
		
	}

	if (gender = 'm' || 'M'){
		cout << setw(10) << "height: ";
	cin >> height;
	heightok = (height >=24 && height <= 110 && height >=62 && height <= 78);
	cout << heightok;

	cout << setw(10) << "weight: ";
	cin >> weight;
	weightok = (weight >= 50 && weight <= 1400 && weight >= 130 && weight <= 250);
	cout << weightok;
	
	}


	else{cout << "please enter a vaild gender"; }
	}while (gender = 'X' || 'x');
	_getch();

}



my output

please enter the candidates information < enter 'X' to exit>
    gender: D
     Invalid entry
     height : 60


Last edited on
if (gender = 'f' || 'F')

You should have == there instead of a single =.
ahhh thank you i didnt see that. i also added a do while loop for the gender. so it will do until the user inputs f, m, or x. but now it says invalid entry when you do enter the right input.

1
2
3
4
5
6
7
8
do{
	cout << "please enter the candidates information (Enter 'x' to exit) " << endl;

	cout << setw(10) << "Gender: ";
	cin >> gender;

	cout << "invaild entry" << endl;
	}while (gender != 'x' && gender != 'X' && gender != 'F' && gender != 'f' && gender != 'm' && gender != 'M');


currently it outputs

Gender: f
***** Invalid entry *****
Height:



i want it to output like

Gender:  k
***** Invalid gender; please enter M or F *****
Gender:
hmmm it seems to work with a do while and a if statement. im not sure if its a clean way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void main() {
	char gender;
	bool heightok;
	bool weightok;
	int height;
	int weight;
	
	do{
	cout << "please enter the candidates information (Enter 'x' to exit) " << endl;

	cout << setw(10) << "Gender: ";
	cin >> gender;
	
	if (gender != 'x' && gender != 'X' && gender != 'F' && gender != 'f' && gender != 'm' && gender != 'M') {
	cout << "invaild entry" << endl;
	}

	}while (gender != 'x' && gender != 'X' && gender != 'F' && gender != 'f' && gender != 'm' && gender != 'M');
Topic archived. No new replies allowed.