cin >> hours >> tens >> minutes;
if
statement suffices to differentiate between AM and PM.
|
|
case 1:
and case 13:
instead of case '1':
and case '13':
, because '1'
is the character "1" and not the number 1.switch
statement and typing out all of those cases, you could shorten things up a bit by using an array.std::string names[] = {"", "one", "two", "three", "four", /* ... */};
./*This program satisfies homework #6 for CS1 SP14. Written by: Ashley Julin Date: 02/16/2014 "I have neither given nor received unauthorized aid in completing this work, nor have I presented someone else's work as my own." */ #include<iostream> #include <string> using namespace std; int time(int, int); void printTime(int hours, int minutes); int main() { int minutes; int hours; //Ask User for to input the time cout << "\n\nPlease enter a time in the 24 hour clock format: \n\n" << endl; cin >> hours >> minutes; hours = time(hours, minutes); printTime(hours, minutes); return 0; } int time(int hours, int minutes)//use only hours and minutes within the time function { switch (hours)//a swtich statement for the hours { case 1 : case 13: cout << "One"; break; case 2 : case 14: cout << "Two"; break; case 3 : case 15: cout << "Three"; break; case 4 : case 16: cout << "Four"; break; case 5 : case 17: cout << "Five"; break; case 6 : case 18: cout << "Six"; break; case 7 : case 19: cout << "Seven"; break; case 8 : case 20: cout << "Eight"; break; case 9 : case 21: cout << "Nine"; break; case 10: case 22: cout << "Ten"; break; case 11: case 23: cout << "Eleven"; break; case 12: cout << "Twelve"; } if (minutes >= 20) //Nest a switch statement within this if statement for use with the minutes variable { switch(minutes) { case 20: cout << " twenty"; break; case 21: cout << " twenty one"; break; case 22: cout << " twenty two"; break; case 23: cout << " twenty three"; break; case 24: cout << " twenty four"; break; case 25: cout << " twenty five"; break; case 26: cout << " twenty six"; break; case 27: cout << " twenty seven"; break; case 28: cout << " twenty eight"; break; case 29: cout << " twenty nine"; break; case 30: cout << " thirty"; break; case 31: cout << " thirty one"; break; case 32: cout << " thirty two"; break; case 33: cout << " thirty three"; break; case 34: cout << " thirty four"; break; case 35: cout << " thirty five"; break; case 36: cout << " thirty six"; break; case 37: cout << " thirty seven"; break; case 38: cout << " thirty eight"; break; case 39: cout << " thirty nine"; break; case 40: cout << " forty"; break; case 41: cout << " forty one"; break; case 42: cout << " forty two"; break; case 43: cout << " forty three"; break; case 44:cout << " forty four"; break; case 45: cout << " forty five"; break; case 46: cout << " forty six"; break; case 47: cout << " forty seven"; break; case 48: cout << " forty eight"; break; case 49: cout << " forty nine"; break; case 50: cout << " fifty"; break; case 51: cout << " fifty one"; break; case 52: cout << " fifty two"; break; case 53: cout << " fifty three"; break; case 54: cout << " fifty four"; break; case 55: cout << " fifty five"; break; case 56: cout << " fifty six"; break; case 57: cout << " fifty seven"; break; case 58: cout << " fifty eight"; break; case 59: cout << " fifty nine"; break; } }//IF Statement for minutes >= 20 if (minutes < 20) //Nest a switch statement within this if statement for use with the minutes variable { switch(minutes) { case 1: cout << " one"; break; case 2: cout << " two"; break; case 3: cout << " three"; break; case 4: cout << " four"; break; case 5: cout << " five"; break; case 6: cout << " six"; break; case 7: cout << " seven"; break; case 8: cout << " eight"; break; case 9: cout << " nine"; break; case 10: cout << " ten"; break; case 11: cout << " eleven"; break; case 12: cout << " twelve"; break; case 13: cout << " thirteen"; break; case 14: cout << " fourteen"; break; case 15: cout << " fifthteen"; break; case 16: cout << " sixteen"; break; case 17: cout << " seventeen"; break; case 18: cout << " eighteen"; break; case 19: cout << " nineteen"; break; } }//IF Statement for minutes < 20 if (minutes == 00) //check that the minutes == 00, if so, then print either Noon or Midnight { if(hours == 12) cout << "Noon"; if (hours == 24) cout << "Midnight"; }//IF Statement for if minutes == 00 return hours; }//time function void printTime(int hours, int minutes) { if (hours < 12) cout << "\n" << hours << minutes << " AM" <<endl; else cout << "\n" << hours << minutes << " PM" <<endl; system("pause"); return; }//printTime Function |
/*This program satisfies homework #6 for CS1 SP14. Written by: Ashley Julin Date: 02/16/2014 "I have neither given nor received unauthorized aid in completing this work, nor have I presented someone else's work as my own." */ #include<iostream> #include <string> using namespace std; int time(int, int); void printTime(int hours, int minutes); int main() { int minutes; int hours; //Ask User for to input the time cout << "\n\nPlease enter a time in the 24 hour clock format: \n\n" << endl; cin >> hours >> minutes; hours = time(hours, minutes); printTime(hours, minutes); return 0; } int time(int hours, int minutes)//use only hours and minutes within the time function { switch (hours)//a swtich statement for the hours { case 1 : case 13: cout << "One"; break; case 2 : case 14: cout << "Two"; break; case 3 : case 15: cout << "Three"; break; case 4 : case 16: cout << "Four"; break; case 5 : case 17: cout << "Five"; break; case 6 : case 18: cout << "Six"; break; case 7 : case 19: cout << "Seven"; break; case 8 : case 20: cout << "Eight"; break; case 9 : case 21: cout << "Nine"; break; case 10: case 22: cout << "Ten"; break; case 11: case 23: cout << "Eleven"; break; case 12: cout << "Twelve"; } if (minutes >= 20) //Nest a switch statement within this if statement for use with the minutes variable { switch(minutes) { case 20: cout << " twenty"; break; case 21: cout << " twenty one"; break; case 22: cout << " twenty two"; break; case 23: cout << " twenty three"; break; case 24: cout << " twenty four"; break; case 25: cout << " twenty five"; break; case 26: cout << " twenty six"; break; case 27: cout << " twenty seven"; break; case 28: cout << " twenty eight"; break; case 29: cout << " twenty nine"; break; case 30: cout << " thirty"; break; case 31: cout << " thirty one"; break; case 32: cout << " thirty two"; break; case 33: cout << " thirty three"; break; case 34: cout << " thirty four"; break; case 35: cout << " thirty five"; break; case 36: cout << " thirty six"; break; case 37: cout << " thirty seven"; break; case 38: cout << " thirty eight"; break; case 39: cout << " thirty nine"; break; case 40: cout << " forty"; break; case 41: cout << " forty one"; break; case 42: cout << " forty two"; break; case 43: cout << " forty three"; break; case 44:cout << " forty four"; break; case 45: cout << " forty five"; break; case 46: cout << " forty six"; break; case 47: cout << " forty seven"; break; case 48: cout << " forty eight"; break; case 49: cout << " forty nine"; break; case 50: cout << " fifty"; break; case 51: cout << " fifty one"; break; case 52: cout << " fifty two"; break; case 53: cout << " fifty three"; break; case 54: cout << " fifty four"; break; case 55: cout << " fifty five"; break; case 56: cout << " fifty six"; break; case 57: cout << " fifty seven"; break; case 58: cout << " fifty eight"; break; case 59: cout << " fifty nine"; break; } }//IF Statement for minutes >= 20 if (minutes < 20) //Nest a switch statement within this if statement for use with the minutes variable { switch(minutes) { case 1: cout << " one"; break; case 2: cout << " two"; break; case 3: cout << " three"; break; case 4: cout << " four"; break; case 5: cout << " five"; break; case 6: cout << " six"; break; case 7: cout << " seven"; break; case 8: cout << " eight"; break; case 9: cout << " nine"; break; case 10: cout << " ten"; break; case 11: cout << " eleven"; break; case 12: cout << " twelve"; break; case 13: cout << " thirteen"; break; case 14: cout << " fourteen"; break; case 15: cout << " fifthteen"; break; case 16: cout << " sixteen"; break; case 17: cout << " seventeen"; break; case 18: cout << " eighteen"; break; case 19: cout << " nineteen"; break; } }//IF Statement for minutes < 20 if (minutes == 00) //check that the minutes == 00, if so, then print either Noon or Midnight { if(hours == 12) cout << "Noon"; if (hours == 24 || hours == 00) cout << "Midnight"; }//IF Statement for if minutes == 00 if (hours < 12) cout << " AM" <<endl; else cout << " PM" <<endl; return hours; }//time function void printTime(int hours, int minutes) { //if (hours < 12) cout << "\n" << hours << minutes <<endl; //else //cout << "\n" << hours << minutes << " PM" <<endl; system("pause"); return; }//printTime Function |