I am doing this two exercises for an assignment.
First exercise is ok but the second one is not running.It's saying there is a building error, but I checked and double checked and couldn't find it.
Can you see what is wrong in the code?
Thanks a lot for you help.
1.Write a program that accepts a time (24 hour clock) in hours and minutes and converts it to 12 hour clock time. Output should be in the format hours: minutes.
#include<iostream>
using namespace std;
int main ()
{
double hr24,min,hr12;
cout<<"What is the hour? ";
cin>>hr24;
cout<<"What is the minute? ";
cin>>min;
if(hr24>=12){
hr12=hr24-12;
}
else{
hr12=hr24;
}
cout<<"The time is "<<hr12<<" : "<<min<<"\n";
system ("pause");
return 0;
}
2.Modify the above program so that it first validates the time entered before it performs the conversion. An appropriate error message should be output if the time is invalid.
#include<iostream>
using namespace std;
int main ()
{
double hr24,min,hr12;
cout<<"What is the hour? ";
cin>>hr24;
cout<<"What is the minute? ";
cin>>min;
if(hr24>23||hr24<0||min<0||min>59)
{
cout<<"Error! \n";
}
else if(hr24>=12){
hr12=hr24-12;
cout<<"The time is "<<hr12<<" : "<<min<<"PM \n";
}
else
{
hr12=hr24;
cout<<"The time is "<<hr12<<" ; "<<min<<"AM \n";
system ("pause");
return 0;
}
Aucun commentaire:
Enregistrer un commentaire