C Golledge C Golledge - 22 days ago 13
C++ Question

boolean statement to change 24 hour format to 12 hour format for time equations in a switch menu?

So I have a project that requires me to basically make a clock/watch. The default format of the code is a 24 hour format but option 5 in the menu, "toggle 24 hour format", it is required that hour formats change to a 12 hour format. It is not required to keep the previous input of the 24 hours, example: the user inputs 23:45, then presses option 5, then option 4, it shouldn't display 11:45pm. The user should go back to option 1 to input a new time and input an 'a' to choose am or 'p' to choose pm. My question is, how to write a boolean statement so when the user inputs '5' in the switch, the void functions know the format is now the 12 hour format and do a variation of the equations so they deal with 12 hour times.

The problem I see happening is the subtraction or addition functions in a 12 hour format. How would I write the 12 hour addition and subtraction statements so if the time is 12:30 and 45 minutes is added, the new time gets outputted as 1:15 and not 13:15.

I have some idea of how to do it (I'm terrible with boolean logic and functions):

bool mode
if (mode==true)
{
\\12 hour format
}
else (mode==false)
{
\\24 hour format
}


But I have no idea how to format that and make it so the option 5 changes the mode. I kind of understand how to do it in the void functions:

if mode==true
then add hours and minutes as a 12 hour format

But again, confused on how to do this. I looked at a similar problem posted about 6 days ago, but the answer didn't satisfy me, and only dealt with a single input and made it so the user has to input 24 or 12 to denote the time instead of making it so option 5 toggles to mode. It did use:

mode= !mode


which looked promising so that if option 5 is toggled, the user can input option 5 again and make the format toggle again, but again: how do I make it so option 5 toggles the 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 a 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 a 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 a time (hours, minutes): ";
cin >> h;
cin >> m;
if (h>23 || m>59 || h<0 || m<0)
{
cout << "Invalid input! Please input a value for hours below 24 and a 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;
do
{
cout << "-----MENU-----" << endl << endl << "1-Enter a time" << endl << "2-Add delta to time" << endl << "3-Subtract delta from 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;
}

Answer

Your print time function doesn't do anything with mode.

void printTime (int h, int m, bool mode)
{
  if (mode) // assuming that when mode is true it means 24 hour time
  {
    //print time in 24 hour format
  }
  else
  {
    //print time in 12 hour format 
  }
}

Also your get time needs to do something similar because as of now, you can only set it by using 24 hour format.

In your switch statement for case 5, you want to switch your mode value. mode = !mode will work as long as mode already has a boolean value assigned to it.

Side note: You only need the function prototypes if the function is called before it is defined.

Comments