Question regarding switches, I think.

This is a snippet of a program I'm working on. The idea is that it switches between active and not active every time a password is entered. Could you show me a more efficient way of doing this? Also, I do not understand the necessity of bool if I can use int.

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
  void corrPassw() {

		if (password==setpw1 && a==1) {
			
			cout << "\n\033[0;32mFunction 1 is active.\033[0m\n";
			a = 2;
			
		}
		else if (password==setpw2 && b==1) {
			
			cout << "\n\033[0;32mFunction 2 is active.\033[0m\n";
			b = 2;
			
		}
		else if (password==setpw1 && a==2) {
			
			cout << "\n\033[0;31mFunction 1 is not active.\n\033[0m";
			a = 1;
			
		}
		else if (password==setpw2 && b==2) {
			
			cout << "\n\033[0;31mFunction 2 is not active.\n\033[0m";
			b = 1;
			
		}

	return;
}
int main () {

	a = 1;
	b = 1;
Last edited on
Could you show me a more efficient way of doing this?
Nope.

Also, I do not understand the necessity of bool if I can use int.
I don't see any bools.
I used int instead of bool, and I don't understand the point of using bool in the first place, that's what I'm wondering.
closed account (E0p9LyTq)
I do not understand the necessity of bool if I can use int.

Using bool you can toggle active/inactive easily using the logical NOT (!) operator (snippet follows):

1
2
3
4
5
bool active = true;
.
.
.
active = !active;  // now is false 


http://en.cppreference.com/w/c/language/operator_logical
Topic archived. No new replies allowed.