I am creating a program to read in 24 hour notation time and output standard time.
For example 23:32 should output 11:32 PM.
I have finished the program but am having trouble formatting the input.
This is what it shows on the screen when I run the program.
Please enter the time of day in 24 hour notation.
Hour : Min
23
: 32
Notice how after you enter the 23, it skips a line. I would like it to show up as 23:32 instead of 23
: 32
Is there a way to make it stay on the same line after cin?
Here is a copy of the input part of the code.
cout<<"Please enter the time of day in 24 hour notation.";
cout<<"\nHour : Min \n";
cin>>hour;
cout<<" : ";
cin>>min;
Here is the entire program code below if you need to copy and paste it.
Code:
#include <iostream>
#include <string>
using namespace std;
int inputFunction(int& hour, int& min);
int convertFunction(int hour);
string meridiemFunction(int militaryHour);
void outputFunction(int hour, int min, string meridiem);
char repeatFunction(char repeat);
int main()
{
string meridiem;
int hour, min, militaryHour;
char repeat('N');
do
{
inputFunction(hour, min);
militaryHour=hour;
hour=convertFunction(hour);
meridiem=meridiemFunction(militaryHour);
outputFunction(hour, min, meridiem);
repeat=repeatFunction(repeat);
cout<<endl<<endl;
}while(repeat=='y'||repeat=='Y');
return(0);
}
int inputFunction(int& hour, int& min)
{
int ok;
do
{
cout<<"Please enter the time of day in 24 hour notation.";
cout<<"\nHour : Min \n";
cin>>hour;
cout<<" : ";
cin>>min;
if(hour>24||min>59)
{
ok=0;
cout<<"Invalid input for Hours or Minutes";
}
else
ok=1;
}while(ok==0);
return(0);
}
int convertFunction(int hour)
{
if(hour>12)
{
hour=hour-12;
}
return(hour);
}
string meridiemFunction(int militaryHour)
{
string meridiem;
if(militaryHour>11)
meridiem="PM";
else
meridiem="AM";
return(meridiem);
}
void outputFunction(int hour, int min, string meridiem)
{
cout<<"The hour in standard time is ";
cout<<hour<<":"<<min<<" "<<meridiem<<endl;
}
char repeatFunction(char repeat)
{
int ok;
do
{
cout<<"Would you like to enter another time? (Y/N): ";
cin>>repeat;
if(repeat=='Y'||repeat=='y'||repeat=='N'||repeat== 'n')
ok=1;
else
{
cout<<"Enter a valid character.";
ok=0;
}
}while(ok==0);
return(repeat);
}