Mar 13, 2014 at 6:20am UTC
hello All,
I have created a program to take a users input in 24 hours time and convert it to 12 hour text based out put an example is an input of 13 24 give an output of one twenty-four. The problem I am having is combining these value and formatting. I cannot combine via because I failed to plan for the fact I cannot convert int variable to string variable. Is there an easier basic way of doing this or a basic way to convert an int to a string? take a look and give me some suggestions if possible. I am new so anything with str:: is probably not going to help.
#include<iostream>
#include<string>
#include<cmath>
#include<fstream>
#include<iomanip>
using namespace std;
int main ()
{
int Hours;
int Minutes;
string Hrs;
string Mins;
string endstring;
cout << "Please enter a time 24-hours forat with a space seperating hours and numbers. Example 1 02." << endl;
cout <<"Enter Time:";
cin >> Hours;
cin >> Minutes;
cout <<"The time is"<< endl;
cout << Hours << Minutes << endstring; // my problem is here!!!!!
if (Hours <=12)
endstring= "AM";
else
if(Hours>=13)
endstring="PM";
if (Hours ==0 || Hours==00)
cout << "Midnight";
if(Hours ==1 || Hours==13)
cout<< "one";
else
if(Hours ==2||Hours==14)
cout<< "two";
else
if(Hours ==3||Hours==15)
cout<< "three";
else
if(Hours ==4||Hours==16)
cout<< "four";
else
if(Hours ==5||Hours==17)
cout<< "five";
else
if(Hours ==6||Hours==18)
cout<< "six";
else
if(Hours ==7||Hours==19)
cout<< "seven";
else
if(Hours ==8||Hours==20)
cout<< "eight";
else
if(Hours ==9||Hours==21)
cout<< "nine";
else
if(Hours ==10||Hours==22)
cout<< "ten";
else
if(Hours ==11||Hours==23)
cout<< "eleven";
else
if (Hours==12)
cout<< "Noon";
if (Minutes <=0)
cout<< " ";
if(Minutes ==1)
cout<< "one";
else
if(Minutes ==2)
cout<< "two";
else
if(Minutes ==3)
cout<< "three";
else
if(Minutes ==4)
cout<< "four";
else
if(Minutes ==5)
cout<< "five";
else
if(Minutes ==6)
cout<< "six";
else
if(Minutes ==7)
cout<< "seven";
else
if(Minutes ==8)
cout<< "eight";
else
if(Minutes ==9)
cout<< "nine";
else
if(Minutes ==10)
cout<< "ten";
else
if(Minutes ==11)
cout<< "eleven";
else
if (Minutes ==12)
cout<< "twelve";
else
if(Minutes ==13)
cout<< "thirteen";
else
if(Minutes ==14)
cout<< "fourteen";
else
if(Minutes ==15)
cout<< "fifteen";
else
if(Minutes ==16)
cout<< "sixteen";
else
if(Minutes ==17)
cout<< "seventeen";
else
if(Minutes ==18)
cout<< "eighteen";
else
if(Minutes ==19)
cout<< "nineteen";
else
if(Minutes ==20)
cout<< "twenty";
else
if(Minutes ==21)
cout<< "twenty-one";
else
if(Minutes ==22)
cout<< "twenty-two";
else
if(Minutes ==23)
cout<< "twenty-three";
else
if (Minutes ==24)
cout<< "twenty-four";
else
if(Minutes ==25)
cout<< "twenty-five";
else
if(Minutes ==26)
cout<< "twenty-six";
else
if(Minutes ==27)
cout<< "twenty-seven";
else
if(Minutes ==28)
cout<< "twenty-eight";
else
if(Minutes ==29)
cout<< "twenty-nine";
else
if(Minutes ==30)
cout<< "thirty";
else
if(Minutes ==31)
cout<< "thirty-one";
else
if(Minutes ==32)
cout<< "thirty-two";
else
if(Minutes ==33)
cout<< "thirty-three";
else
if(Minutes ==34)
cout<< "thirty-four";
else
if(Minutes ==35)
cout<< "thirty-five";
else
if (Minutes ==36)
cout<< "thirty-six";
else
if(Minutes ==37)
cout<< "thirty-seven";
else
if(Minutes ==38)
cout<< "thirty-eight";
else
if(Minutes ==39)
cout<< "thirty-nine";
else
if(Minutes ==40)
cout<< "forty";
else
if(Minutes ==41)
cout<< "forty-one";
else
if(Minutes ==42)
cout<< "forty-two";
else
if(Minutes ==43)
cout<< "forty-three";
else
if(Minutes ==44)
cout<< "forty-four";
else
if(Minutes ==45)
cout<< "forty-five";
else
if(Minutes ==46)
cout<< "forty-six";
else
if (Minutes ==47)
cout<< "forty-seven";
else
if(Minutes ==48)
cout<< "forty-eight";
else
if(Minutes ==49)
cout<< "forty-nine";
else
if(Minutes ==50)
cout<< "fifty";
else
if(Minutes ==51)
cout<< "fifty-one";
else
if(Minutes ==52)
cout<< "fifty-two";
else
if(Minutes ==53)
cout<< "fifty-three";
else
if(Minutes ==54)
cout<< "fifty-four";
else
if(Minutes ==55)
cout<< "fifty-five";
else
if(Minutes ==56)
cout<< "fifty-six";
else
if(Minutes ==57)
cout<< "fifty-seven";
else
if(Minutes ==58)
cout<< "fifty-eight";
else
if (Minutes ==59)
cout<< "fifty-nine";
return 0;
}
Mar 13, 2014 at 8:37am UTC
Hello
What is your desired output? I still don't get your question
One tip, you can use switch case instead of all those if's
Mar 13, 2014 at 11:27am UTC
I dont quite understand what you are asking, other than you are displaying a variable which doesnt contain anything. You are displaying it before its assigned in the code below it.
cout << Hours << Minutes << endstring; // my problem is here!!!!!
1 2 3 4 5
if (Hours <=12)
endstring= "AM" ;
else
if (Hours>=13)
endstring="PM" ;
Is that what you mean or are you referring to how it displays the time on the screen?
You can format it any way you choose..
cout << "You entered " << Hours << ":" << Minutes << endl;
As for the worded hours and minutes you should format those better because if I enter 22 10 it will show "tenten". For those you could put "ten minutes past" so that the format could be "ten minutes past ten".. just an idea.
Last edited on Mar 13, 2014 at 11:32am UTC