c++ - boolean statement to change 24 hour format to 12 hour format for time equations in a switch menu? -
so have project requires me make clock/watch. default format of code 24 hour format option 5 in menu, "toggle 24 hour format", required hour formats change 12 hour format. not required keep previous input of 24 hours, example: user inputs 23:45, presses option 5, option 4, shouldn't display 11:45pm. user should go option 1 input new time , input 'a' choose or 'p' choose pm. question is, how write boolean statement when user inputs '5' in switch, void functions know format 12 hour format , variation of equations deal 12 hour times.
the problem see happening subtraction or addition functions in 12 hour format. how write 12 hour addition , subtraction statements if time 12:30 , 45 minutes added, new time gets outputted 1:15 , not 13:15.
i have idea of how (i'm terrible boolean logic , functions):
bool mode if (mode==true) { \\12 hour format } else (mode==false) { \\24 hour format }
but have no idea how format , make option 5 changes mode. kind of understand how in void functions:
if mode==true then add hours , minutes 12 hour format
but again, confused on how this. looked @ similar problem posted 6 days ago, answer didn't satisfy me, , dealt single input , made user has input 24 or 12 denote time instead of making option 5 toggles mode. did use:
mode= !mode
which looked promising if option 5 toggled, user can input option 5 again , make format toggle again, again: how make option 5 toggles mode?
#include <iostream> #include <iomanip> #include <sstream> using namespace std; void calcdeltafutr(int h, int m, int deltah, int deltam, int& hnew, int& mnew); void calcdeltapast(int h, int m, int deltah, int deltam, int& hnew, int& mnew); void gettime(int &h, int &m, bool mode); void rolltimeforward(int& h, int& m); void rolltimeback(int& h, int& m); void printtime(int h, int m, bool mode); void calcdeltafutr(int h, int m, int deltah, int deltam, int& hnew, int& mnew) { cout << "enter new time:" << endl << "hours: "; cin >> deltah; cout << "minutes: "; cin >> deltam; hnew = h + deltah; mnew = m + deltam; rolltimeforward(hnew, mnew); cout << "the new time is: " << hnew << ":" << mnew << endl; return; } void calcdeltapast(int h, int m, int deltah, int deltam, int& hnew, int& mnew) { cout << "enter new time:" << endl << "hours: "; cin >> deltah; cout << "minutes: "; cin >> deltam; hnew = h - deltah; mnew = m - deltam; rolltimeback(hnew, mnew); cout << "the new time is: " << hnew << ":" << mnew << endl; return; } void gettime(int &h, int &m, bool mode) { cout << "enter time (hours, minutes): "; cin >> h; cin >> m; if (h>23 || m>59 || h<0 || m<0) { cout << "invalid input! please input value hours below 24 , value of minutes below 60." << endl; } return; } void rolltimeforward(int& h, int& m) { h = h + m / 60; m = m % 60; h = h % 24; return; } void rolltimeback(int& h, int& m) { h = h - m / 60; m = m % 60; return; } void printtime(int h, int m, bool mode) { cout << "the current time is: "; cout << h << ":" << m << endl; } int h, m, deltah, deltam, hnew, mnew; bool mode; char time; int main() { int choice; { cout << "-----menu-----" << endl << endl << "1-enter time" << endl << "2-add delta time" << endl << "3-subtract delta time" << endl << "4-display current time" << endl << "5-toggle 24 hour mode" << endl << "6-exit" << endl; cin >> choice; switch (choice) { case 1: gettime(h, m, mode); continue; case 2: calcdeltafutr(h, m, deltah, deltam, hnew, mnew); continue; case 3: calcdeltapast(h, m, deltah, deltam, hnew, mnew); continue; case 4: printtime(h, m, mode); continue; case 5: cout << "12 hour mode turned on" << endl; continue; case 6: cout << "exiting..."; break; } if (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6) { cout << "invalid input! please try again!" << endl << endl; } else { break; } } while (choice != 6); return 0; }
your print time function doesn't mode.
void printtime (int h, int m, bool mode) { if (mode) // assuming when mode true means 24 hour time { //print time in 24 hour format } else { //print time in 12 hour format } }
also time needs similar because of now, can set using 24 hour format.
in switch statement case 5, want switch mode value. mode = !mode work long mode has boolean value assigned it.
side note: need function prototypes if function called before defined.
Comments
Post a Comment