Question regarding switches, I think.
Apr 4, 2018 at 10:18pm UTC
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 Apr 4, 2018 at 10:19pm UTC
Apr 4, 2018 at 10:56pm UTC
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.
Apr 7, 2018 at 2:15pm UTC
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.
Apr 7, 2018 at 4:52pm UTC
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.