Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ program help! Can\'t find what\'s wrong with my program. The sample output i

ID: 3938191 • Letter: C

Question

C++ program help! Can't find what's wrong with my program.

The sample output is below and the program I wrote is below that. The program is good but if you enter in a number of minutes like "120" it doesn't know what to do and crashes. Anything helps. Thank you.

-In this project, the “current time” should be stored in 24-hour format. Only when the user actually interacts with the program will the output and input be sometimes in 12-hour format. Implement a main menu that will allow the user to (1) enter the current time, (2) add a forward delta to the “current time”, (3) add a backwards delta to the “current time”, (4) display the current time, (5) toggle the interface mode between 24-hour and 12-hour (should impact both getTime and printTime). The program should identify invalid input for the main menu. When entering the current time, only allow hours between 0 and 23 and minutes between 0 and 59. Accept only ‘a’ and ‘p’ when entering the time in 12-hour mode. When entering time deltas, only allow positive values or 0 (this is true for both forward and backwards time deltas).

Sample Output: (user input in italics)

1 - Enter a time

2 - Add delta to time

3 - Subtract delta from time

4 - Display current time

5 - Toggle 24-hour mode

6 - Exit

> 1

Enter the time with whitespace separating the hours and minutes: 5 50

1 - Enter a time

2 - Add delta to time

3 - Subtract delta from time

4 - Display current time

5 - Toggle 24-hour mode

6 - Exit

> 4

The current time is 05:50

1 - Enter a time

2 - Add delta to time

3 - Subtract delta from time

4 - Display current time

5 - Toggle 24-hour mode

6 - Exit

> 2

Enter an increment of hours and minutes (separated by a space): 0 120

The new time would be 07:50

1 - Enter a time

2 - Add delta to time

3 - Subtract delta from time

4 - Display current time

5 - Toggle 24-hour mode

6 - Exit

> 2

Enter an increment of hours and minutes (separated by a space): 2 20

The new time would be 08:10

1 - Enter a time

2 - Add delta to time

3 - Subtract delta from time

4 - Display current time

5 - Toggle 24-hour mode

6 - Exit

> 5

12-hour mode turned on

1 - Enter a time

2 - Add delta to time

3 - Subtract delta from time

4 - Display current time

5 - Toggle 24-hour mode

6 - Exit

> 1

Enter the time with whitespace separating the hours, minutes, and either 'a' for am or 'p' for pm: 0 15 a

1 - Enter a time

2 - Add delta to time

3 - Subtract delta from time

4 - Display current time

5 - Toggle 24-hour mode

6 - Exit

> 4

The current time is 12:15 am

1 - Enter a time

2 - Add delta to time

3 - Subtract delta from time

4 - Display current time

5 - Toggle 24-hour mode

6 - Exit

> 3

Enter a decrement of hours and minutes (separated by a space): 1 15 The new time would be 11:00 pm

1 - Enter a time

2 - Add delta to time

3 - Subtract delta from time

4 - Display current time

5 - Toggle 24-hour mode

6 - Exit

> 3

Enter a decrement of hours and minutes (separated by a space): 0 90 The new time would be 10:45 pm

*****This is the program I wrote:

#include <fstream> // file processing
#include <iostream> // cin and cout
#include <cctype> // toupper
#include <iomanip> // setw
#include <cstring> // cstring functions strlen, strcmp, strcpy stored in string.h
#include <string.h> // string class
#include <stdlib.h>

using namespace std;

void calcDeltaFutr(int h, int m, int deltaH, int deltaM, int& hNew, int& mNew, bool mode);
void calcDeltaPast(int h, int m, int deltaH, int deltaM, int& hNew, int& mNew, bool mode);
void getTime(int &h, int &m, bool mode);
void rollTimeForward(int& h, int& m, bool mode);
void rollTimeBack(int& h, int& m, bool mode);
void printTime(int h, int m, bool mode);
int current_h=-1,current_m=-1;//current time..
bool mode=true;//24 hr format
int main()
{
int choice=-1,c=0;
while(choice!=6)//loop.. exits when user gives 6
{
cout<<" 1 - Enter a time 2 - Add delta to time 3 - Subtract delta from time 4 - Display current time 5 - Toggle 24-hour mode 6 - Exit ";
cout<<"Enter ur choice:";
cin>>choice;//taking user choice
if((choice!=1)&&c==0)
{
cout<<" First Enter current time.... ";//errror message..
}
else if(choice==1)
{
c=1;
getTime(current_h,current_m,mode);//calling function that takes time from the user
//cout<<current_h<<" "<<current_m<<" ";
}
else if(choice==2)
{
int deltaH,deltaM,hNew,mNew;
cout<<"Enter an increment of hours and minutes (separated by a space):";
cin>>deltaH;cin>>deltaM;
calcDeltaFutr(current_h,current_m,deltaH,deltaM,hNew,mNew, mode);
current_h = hNew;
current_m = mNew;
cout<<" The New time would be:";
printTime(hNew,mNew,mode);
}
else if(choice==3)
{
int deltaH,deltaM,hNew,mNew;
cout<<"Enter an decrement of hours and minutes (separated by a space):";
cin>>deltaH;cin>>deltaM;
calcDeltaPast(current_h,current_m,deltaH,deltaM,hNew,mNew,mode);
current_h = hNew;
current_m = mNew;
cout<<" The New time would be:";
printTime(hNew,mNew,mode);
  
}
else if(choice==4)
{
cout<<" The Current time is:";
printTime(current_h,current_m,mode);
}
else if(choice==5)
{
if(mode==false)
mode = true;
else mode = false;
}
else
{
cout<<" Select appropriate choice... ";//error message
}
}
return 0;
}
void getTime(int& h,int& m,bool mode)//function that takes current time as input from the user
{
int hh,mm;
  
while(true)
{
if(mode==true)
{
cout<<" Enter the time with whitespace separating the hours and minutes:";
cin>>hh;
cin>>mm;
if(hh>=24 || mm >= 60|| hh<0 || mm <0)
{
cout<<" Error:invalid time..enter again.. ";
}
else break;
}
else
{
char c;
cout<<"Enter the time with whitespace separating the hours and minutes and (a/p)character:";
cin>>hh;
cin>>mm;
cin>>c;
cin.ignore();
if(hh>=13 || mm >= 60|| hh<0 || mm <0||(c!='a'&&c!='p'))
{
cout<<" Error:invalid time..enter again.. ";
}
else
{
if(c=='p')
{
if(hh!=12)hh=12+hh;
}
else if(hh==12)
hh=0;
break;
}
}
  
}
h=hh;m=mm;
//cout<<hh<<" "<<mm<<" ";
//cout<<h<<" "<<m<<" ";
}
void calcDeltaPast(int h, int m, int deltaH, int deltaM, int& hNew, int& mNew, bool mode)
{
hNew = h-deltaH;
mNew = m-deltaM;
rollTimeBack(hNew, mNew, mode);
}
void rollTimeBack(int& h , int& m, bool mode)
{
cout << mode << endl;
if(mode == false)
{
if(m<0)
{
while(m<0)
{
h=h-1;
m=m+60;
}
}

if(h < 0)
h = h + 12;
}

else
{
if(m<0)
{
while(m<0)
{
h=h-1;
m=m+60;
}
}
if(h<0)
{
cout<<" ERROR: Limit exceeded.. rounding hours.. ";
h=0;
}
}

}
void calcDeltaFutr(int h, int m, int deltaH, int deltaM, int& hNew, int& mNew, bool mode)
{
hNew = deltaH+h;
mNew = deltaM+m;
rollTimeForward(hNew, mNew, mode);
}

void rollTimeForward(int& h, int& m, bool mode)
{
int hh=h,mm=m;
if(mm>=60)
{
hh=hh+mm/60;
mm=mm%60;
cout<<hh<<":"<<mm<<" ";
}
if(hh>24)
{
cout<<" Error : limit exceeded..Rounding hours.. ";
hh=hh%24;
}


h=hh;m=mm;
}

void printTime(int h, int m, bool mode)
{
  
  
if(mode==true)
{
if(h<9)
cout<<'0';
cout<<h<<":";
if(m<9)
cout<<'0';
cout<<m<<" ";
}
else
{
if(h<12)
{
if(h==0)cout<<"12:";
else
{
if(h<9)
cout<<'0';
cout<<h<<":";
}
if(m<9)
cout<<'0';
cout<<m<<" am ";
  
}
else
{
if(h==12)cout<<"12:";
else
{
cout<<h-12;
cout<<":";
}
if(m<9)
cout<<'0';
cout<<m<<" pm ";
}
}
}

Explanation / Answer

#include <fstream> // file processing
#include <iostream> // cin and cout
#include <cctype> // toupper
#include <iomanip> // setw
#include <cstring> // cstring functions strlen, strcmp, strcpy stored in string.h
#include <string.h> // string class
#include <stdlib.h>

using namespace std;

void calcDeltaFutr(int h, int m, int deltaH, int deltaM, int& hNew, int& mNew, bool mode);
void calcDeltaPast(int h, int m, int deltaH, int deltaM, int& hNew, int& mNew, bool mode);
void getTime(int &h, int &m, bool mode);
void rollTimeForward(int& h, int& m, bool mode);
void rollTimeBack(int& h, int& m, bool mode);
void printTime(int h, int m, bool mode);
int current_h=-1,current_m=-1;//current time..
bool mode=true;//24 hr format
int main()
{
int choice=-1,c=0;
while(choice!=6)//loop.. exits when user gives 6
{
cout<<" 1 - Enter a time 2 - Add delta to time 3 - Subtract delta from time 4 - Display current time 5 - Toggle 24-hour mode 6 - Exit ";
cout<<"Enter ur choice:";
cin>>choice;//taking user choice
if((choice!=1)&&c==0)
{
cout<<" First Enter current time.... ";//errror message..
}
else if(choice==1)
{
c=1;
getTime(current_h,current_m,mode);//calling function that takes time from the user
//cout<<current_h<<" "<<current_m<<" ";
}
else if(choice==2)
{
int deltaH,deltaM,hNew,mNew;
cout<<"Enter an increment of hours and minutes (separated by a space):";
cin>>deltaH;cin>>deltaM;
calcDeltaFutr(current_h,current_m,deltaH,deltaM,hNew,mNew, mode);
//current_h = hNew; //modified
//current_m = mNew; //modified
cout<<" The New time would be:";
printTime(hNew,mNew,mode);
}
else if(choice==3)
{
int deltaH,deltaM,hNew,mNew;
cout<<"Enter an decrement of hours and minutes (separated by a space):";
cin>>deltaH;cin>>deltaM;
calcDeltaPast(current_h,current_m,deltaH,deltaM,hNew,mNew,mode);
//current_h = hNew;
//current_m = mNew;
cout<<" The New time would be:";
printTime(hNew,mNew,mode);

}
else if(choice==4)
{
cout<<" The Current time is:";
printTime(current_h,current_m,mode);
}
else if(choice==5)
{
if(mode==false)
{

mode = true;
cout<<" 12 hour mode Turned off.. ";
}
else {
mode = false;
cout<<" 12 hour mode Turned on.. ";
}
}
else
{
cout<<" Select appropriate choice... ";//error message
}
}
return 0;
}
void getTime(int& h,int& m,bool mode)//function that takes current time as input from the user
{
int hh,mm;

while(true)
{
if(mode==true)
{
cout<<" Enter the time with whitespace separating the hours and minutes:";
cin>>hh;
cin>>mm;
if(hh>=24 || mm >= 60|| hh<0 || mm <0)
{
cout<<" Error:invalid time..enter again.. ";
}
else break;
}
else
{
char c;
cout<<"Enter the time with whitespace separating the hours and minutes and (a/p)character:";
cin>>hh;
cin>>mm;
cin>>c;
cin.ignore();
if(hh>=13 || mm >= 60|| hh<0 || mm <0||(c!='a'&&c!='p'))
{
cout<<" Error:invalid time..enter again.. ";
}
else
{
if(c=='p')
{
if(hh!=12)hh=12+hh;
}
else if(hh==12)
hh=0;
break;
}
}

}
h=hh;m=mm;
//cout<<hh<<" "<<mm<<" ";
//cout<<h<<" "<<m<<" ";
}
void calcDeltaPast(int h, int m, int deltaH, int deltaM, int& hNew, int& mNew, bool mode)
{
hNew = h-deltaH;
mNew = m-deltaM;
rollTimeBack(hNew, mNew, mode);
}
void rollTimeBack(int& h , int& m, bool mode)
{
//cout << mode << endl;
/*if(mode == false)
{
if(m<0)
{
while(m<0)
{
h=h-1;
m=m+60;
}
}

if(h < 0)
h = h + 12;
}

else
{*/
if(m<0)
{
while(m<0)
{
h=h-1;
m=m+60;
}
}
if(h<0)
{
//cout<<" ERROR: Limit exceeded.. rounding hours.. ";
h=24+h;
}
//}

}
void calcDeltaFutr(int h, int m, int deltaH, int deltaM, int& hNew, int& mNew, bool mode)
{
hNew = deltaH+h;
mNew = deltaM+m;
rollTimeForward(hNew, mNew, mode);
}

void rollTimeForward(int& h, int& m, bool mode)
{
int hh=h,mm=m;
if(mm>=60)
{
hh=hh+mm/60;
mm=mm%60;
cout<<hh<<":"<<mm<<" ";
}
if(hh>24)
{
cout<<" Error : limit exceeded..Rounding hours.. ";
hh=hh%24;
}


h=hh;m=mm;
}

void printTime(int h, int m, bool mode)
{


if(mode==true)
{
if(h<9)
cout<<'0';
cout<<h<<":";
if(m<9)
cout<<'0';
cout<<m<<" ";
}
else
{
if(h<12)
{
if(h==0)cout<<"12:";
else
{
if(h<9)
cout<<'0';
cout<<h<<":";
}
if(m<9)
cout<<'0';
cout<<m<<" am ";

}
else
{
if(h==12)cout<<"12:";
else
{
cout<<h-12;
cout<<":";
}
if(m<9)
cout<<'0';
cout<<m<<" pm ";
}
}
}

output:-


1 - Enter a time
2 - Add delta to time
3 - Subtract delta from time
4 - Display current time
5 - Toggle 24-hour mode
6 - Exit
Enter ur choice:1

Enter the time with whitespace separating the hours and minutes:5 50

1 - Enter a time
2 - Add delta to time
3 - Subtract delta from time
4 - Display current time
5 - Toggle 24-hour mode
6 - Exit
Enter ur choice:4

The Current time is:05:50

1 - Enter a time
2 - Add delta to time
3 - Subtract delta from time
4 - Display current time
5 - Toggle 24-hour mode
6 - Exit
Enter ur choice:2
Enter an increment of hours and minutes (separated by a space):0 120
7:50

The New time would be:07:50

1 - Enter a time
2 - Add delta to time
3 - Subtract delta from time
4 - Display current time
5 - Toggle 24-hour mode
6 - Exit
Enter ur choice:2
Enter an increment of hours and minutes (separated by a space):2 20
8:10

The New time would be:08:10

1 - Enter a time
2 - Add delta to time
3 - Subtract delta from time
4 - Display current time
5 - Toggle 24-hour mode
6 - Exit
Enter ur choice:5

12 hour mode Turned on..

1 - Enter a time
2 - Add delta to time
3 - Subtract delta from time
4 - Display current time
5 - Toggle 24-hour mode
6 - Exit
Enter ur choice:1
Enter the time with whitespace separating the hours and minutes and (a/p)character:0 15 a

1 - Enter a time
2 - Add delta to time
3 - Subtract delta from time
4 - Display current time
5 - Toggle 24-hour mode
6 - Exit
Enter ur choice:4

The Current time is:12:15 am

1 - Enter a time
2 - Add delta to time
3 - Subtract delta from time
4 - Display current time
5 - Toggle 24-hour mode
6 - Exit
Enter ur choice:3
Enter an decrement of hours and minutes (separated by a space):1 15

The New time would be:11:00 pm

1 - Enter a time
2 - Add delta to time
3 - Subtract delta from time
4 - Display current time
5 - Toggle 24-hour mode
6 - Exit
Enter ur choice:3
Enter an decrement of hours and minutes (separated by a space):0 90

The New time would be:10:45 pm

1 - Enter a time
2 - Add delta to time
3 - Subtract delta from time
4 - Display current time
5 - Toggle 24-hour mode
6 - Exit
Enter ur choice:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote