closing a program with a certain key

so my program is suppose to stop when you enter 'X' or 'x'. i wrote

1
2
3
4
5
6
7
8
9
10
11
bool end;
end = true;
do{

cout << "please enter the candidates information (Enter 'x' to exit) " << endl;


****Code*****

}while (gender == 'x' || 'X');  
	end = false;



how ever this isnt working im not sure if im using the bool right. and when i tried to return a 0, it says an error "return value type does not match function type"
If only C++ supported something similar to this.*

You need to compare gender against both 'x' and 'X'. That means having gender == appear twice in that line.

Now, another thing you could do is have only one comparison and feed gender into the tolower() function in the <cctype> header, however if you're not sure how to do this then don't stress out over it. :)

As for your second error... could we see the whole function?

-Albatross

*I wonder what the operators would look like. ?| or ?& look good to me.
Last edited on
this is my code so far. its running okay, but i think i could improve it a little more. i guess the problem was i had the while at the bottom so it was running everything else before checking the while. i don't like having the two if statements though, not sure how i can improve that though.

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <iomanip>
#include "conio.h"
using namespace std;

void main() {
	char gender;
	bool heightok;
	bool weightok;
	int height;
	int weight;
	bool end;
	end = true;
	do{
	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');
	
	if (gender == 'x' || gender == 'X'){
	end = false;
	exit(1);

	
	}

	if (gender == 'f' || gender ==  '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;
		
	}

	else {(gender == 'm' || gender ==  '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;
	
	}

	
	 if (heightok == 1 && weightok == 1) {
		
		cout << "This candidate has been ACCEPTED!" << endl <<  endl;}
	
		
		if(heightok == 0 && weightok == 1) {
			
			
		cout << " This candidate has been rejected based on the HEIGHT requirement" << endl;}
		
		if(heightok == 1 && weightok == 0) {
			
		cout << "This candidate has been rejected based on the Weight requirement" << endl;}

		if (heightok == 0 && weightok == 0) {
			
	cout << "This candidate has been rejected based on the Weight and HEIGHT requirement" << endl;}

		_getch();
	
	}while (cout << " " << endl);
	
	system("pause");
	

}
Last edited on
1
2
3
4
5
6
7
8
9
10
bool end = false;

do
{
    //code....

    if( ( gender == 'x' ) || ( gender == 'X' ) )
        end = true;

}while( ! end );


Something like that?
i was trying something like

1
2
3
4
5
6
7
8
9
10
bool end = false;

do
{

//code.....

}while ( gender == 'x' ) || ( gender == 'X' ) );
        end = true;
      exit(1);


for some reason it runs the other code first instead of going straight to the while. but i ended up using an if statement. it runs fine i was just trying to find a different way.

1
2
3
4
5
6

if (gender == 'x' || gender == 'X')
{
	end = false;
	exit(1);
      }


I tried a do-while so i wouldn't have an another if.
It's cleaner for your program to run through to the end of main, instead of calling exit(1);!

Instead of calling exit(1);, use break;.
This will exit the loop you are currently in ( The do-while loop ).

EDIT:
Also, doing it that way, you wouldn't even need the exit bool. Unless you want other ways to exit the game.
Last edited on
ahhh thank you so much. i forgot about break;

i did

1
2
3
4
5
6
7
8
9
10
11
end = false
do{

if (  gender == 'x'  ||  gender == 'X'  )
{
        end = true;
	break;
}

}while (!end);
cout << "succesful break";  // test to make sure it was getting out of the loop. 



note:
just wondering when i declared end = true, at the beginning, then in the code if; end = false the while(!end) would end the program with out entering 'x'. and i want it to keep looping until you enter 'x'.
Last edited on
its about the (!end). im having trouble understanding what that does. i understand that it produces the opposite, i dont see how that gets out of the loop.
Last edited on
Well, you'd have your user input above the if statement, so the user would have entered something in to 'gender' and then if it's an x or X, then it will break; from the loop.
ahhh i see so it will do the code while end is false, but when the opposite ( end = true) is returned it goes onto the next set of code. okay thats so bad.
looking forward to starting computer science for this semester, that i'm trying to learn some stuff on my own before classes start.
while( ! end ); is saying:

while exit is not true


So, when you set it to true, it will exit.
ahaha, when someone reads this, it's like you're answering me before I post back! :P
hahaha thank you very much, it was very helpful.
You're more than welcome! And good luck!
Topic archived. No new replies allowed.