I cannot figure this one out can someone help? Here is a description of the prob
ID: 3622291 • Letter: I
Question
I cannot figure this one out can someone help? Here is a description of the problem:Write a program to convert the time from 24 -hour notation to 12 -hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following function: a function to convert the time from 24 -hour notation to 12 -hour notation, a function to convert the time from 12- hour notation to 24 -hour notation, a function to display the choices, function(s) to get the input, and function(s) to display the results. (For 12 -hour time notation, your program must display AM or PM.)
Explanation / Answer
please rate - thanks
#include <iostream>
#include <iomanip>
using namespace std;
void to12(int&,char&);
void to24(int&,char&);
void input(int&,int&,char&);
void display(int,int,char);
int gettime(int,int,string);
int menu(char);
int main()
{int hour, min,choice;
char ampm='n';
input(hour,min,ampm);
choice=menu(ampm);
while(choice!=4)
{if(choice==1)
{cout<<"Current time(24 hr): ";
display(hour,min,ampm);
to12(hour,ampm);
cout<<"(12 hr): ";
display(hour,min,ampm);
cout<<endl;
}
else if(choice==2)
{cout<<"Current time(12 hr): ";
display(hour,min,ampm);
to24(hour,ampm);
cout<<"(24 hr): ";
display(hour,min,ampm);
cout<<endl;
}
else
input(hour,min,ampm);
choice=menu(ampm);
}
return 0;
}
int menu(char mode)
{int choice;
bool error=true;
while(error)
{
cout<<"What would you like to do? ";
cout<<"1. convert from 24 hour to 12 hour ";
cout<<"2. convert from 12 hour to 24 hour ";
cout<<"3. input new time ";
cout<<"4. exit ";
cin>>choice;
if(choice==3||choice==4)
return choice;
if(choice==1&&mode=='d')
return choice;
if(choice==2&&(mode=='A'||mode=='P'))
return choice;
cout<<"Invalid entry ";
}
}
void input(int& h,int& m,char& mode)
{int choice;
bool error=true;
while(error)
{
cout<<"Enter type of entry ";
cout<<"1. 24 hour ";
cout<<"2. 12 hour ";
cin>>choice;
if(choice ==1||choice==2)
error=false;
else
cout<<"Invalid entry ";
}
if(choice==1)
{h=gettime(0,23,"hour");
m=gettime(0,59,"minute");
mode='d';
}
else
{h=gettime(1,12,"hour");
m=gettime(0,59,"minute");
error=true;
while(error)
{
cout<<"enter A for AM,P for PM: ";
cin>>mode;
mode=toupper(mode);
if(mode!='A'&&mode!='P')
cout<<"invalid entry ";
else
error=false;
}
}
return;
}
int gettime(int min,int max, string mess)
{int t;
bool error=true;
while(error)
{
cout<<"enter the "<<mess<<" between "<<min<<" and "<<max<<": ";
cin>>t;
if(t<min||t>max)
cout<<"invalid entry";
else
error=false;
}
return t;
}
void to12(int& h,char& m)
{if(h<13)
{m='A';
if(h==0)
h=12;
}
else
{m='P';
h=h-12;
}
}
void to24(int& h,char& m)
{
if(m=='P'&&h<12)
h+=12;
if(m=='A'&&h==12)
h=0;
if(h==24)
h=12;
m='d';
}
void display(int h,int m,char mode)
{cout<<setw(2)<<setfill('0')<<h<<":"<<setw(2)<<setfill('0')<<m<<" ";
if(mode!='d')
cout<<mode<<"M";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.