c++ - I'm trying to create a function that toggles between a 24 hour clock and a 12 hour clock using Boolean expressions -
in program creating, need able toggle option main menu change 24 hour clock 12 hour clock , vice versa. understand need swap bool values in order create toggle, have no idea how so. here function below:
void printtime(int h, int m, bool mode) { if (mode = 0) { mode = 1; cout << "24-hour mode turned on" << endl; } else { mode = 0; cout << "12-hour mode turned on" << endl; } }
to able change variable outside function's scope should pass reference.
void printtime(int h, int m, bool& mode) { mode = !mode; //toggle mode std::cout << (mode ? "24" : "12") << "-hour mode turned on" << std::endl; }
then can change value in different scope;
int main() { bool mode24 = true; printtime(2, 4, mode24); //changes mode24 false printtime(2, 4, mode24); //changes mode24 true return 0; }
Comments
Post a Comment