Functions and time

Gee, I have a doozy this time. I can follow along in class, and then the homework pulls the rug out from underneath me.
I spent so much time writing out just the switch statements that now I am literally crossed eyed.
The compiler stops at the first IF ELSE statement. And I have a feeling that when the compiler does run successfully, I have not written the program successfully. I don't know how to use the switch(tens) correctly. Right now I am reading tens, just to have it declared as a parameter in the functions.
Oh, and the print function isn't working either. (sigh)
Any help is always going to be appreciated.

Here are the instructions for this homework:
Write a functional decomposition and a C++ program that reads a time in numeric form and prints it in English. The time is input as hours and minutes, separated by a space.
Hours are specified in 24 hour time (15 is 3 PM), but the output should be in 12 hour AM/PM form. Note that noon and midnight are special cases. Here are some examples:
Enter time: 12 00
Noon

Enter time: 0 00
Midnight

Enter time: 6 44
Six forty four AM

Enter time: 18 11
Six eleven PM

Here is my code so far:

/*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 timeAM(int, int, int);
int timePM(int, int, int);
void printTime(int, int, int, int);
int main()
{
int time;
int minutes;
int hours;
int tens;

cout << "Please enter a time in the 24 hour clock format: " << endl;
cin >> hours >> tens >> minutes;

if (hours > 0 && hours < 12)
time = timeAM(hours, tens, minutes);

else
time = timePM(hours,tens, minutes);

cout << hours << " " << tens << " " << minutes << " " << time;

return 0;
}
int timeAM(int hours, int tens, int minutes)
{
switch (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 << "Noon";
}
switch(tens)
{
case '20': cout << "twenty";
break;
case '30': cout << "thirty";
break;
case '40': cout << "forty";
break;
case '50': cout << "fifty";
}
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;
}
return hours;
}
int timePM(int hours, int tens, int minutes)
{
switch (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 << "Noon";
}
switch(tens)
{
case '20': cout << "twenty";
break;
case '30': cout << "thirty";
break;
case '40': cout << "forty";
break;
case '50': cout << "fifty";
}
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;
}

return 0;
}

void printTime(int hours, int tens, int minutes, int time)
{
cout << hours << " " << tens << " " << minutes << " " << time;
system("pause");
return;
}
//END//
First of all, your code would look much nicer if you put it in [code][/code] tags.

Second of all, unless you're separating the tens digit of your minutes from the units digit with a space,
cin >> hours >> tens >> minutes;
will read all of the minutes into the tens variable, then stop because you haven't inputted anything into minutes yet. (Remember, cin >> something only stops when it runs into a whitespace character.)

I would recommend just reading the hours and minutes, and then splitting the tens digit off using division and the modulus (%) operator.
(Dividing by 10 chops off the last digit, and n % 10 gives you the last digit of n.)

Your code can actually be simplified considerably, but I'll let you tinker around with that after fixing the input bug.
Hi long double main,

Thank you for your speedy reply.

I took out reading tens in the initial io lines.
And then I added a line within each time function:
int tens = (minutes % 10);

but now the main function is saying that int tens is uninitialized.

I can't wait until I can learn how to simplify this code. I'm getting a finger cramp scrolling down the page. lol

Thanks for your help!
For one, your timeAM and timePM functions are basically identical. (Actually, they are identical, except for the name and the return value at the end.)
There's no need to handle AM and PM in completely separate functions -- a simple if statement suffices to differentiate between AM and PM.

I wasn't quite sure at first where you were going with the tens and minutes thing, but now I think I have a better idea.
Okay, so let's go through everything that needs to happen.

The first thing you need to do is print the hour, in word form.
You seem to have the right idea, although there are a couple of things that need to be fixed:
1
2
3
4
5
switch (hours)
{
case '1' :
case '13': cout << "One";
    break;

Here, it should be just case 1: and case 13: instead of case '1': and case '13':, because '1' is the character "1" and not the number 1.
Also, for 12, it should be just "Twelve", not "Noon", since the time 12:13 is still read as "Twelve thirteen", not "Noon thirteen".
Of course, there's a chance that an hour value of 0 or 12 could be noon or midnight, so you'll have to deal with that separately (by checking if the minutes value is also 0).

The next thing after the hours is the minutes. Here, we have essentially three cases to cover:
1) minutes < 10: Make sure to add the "o' " part to the minute name. For minutes == 0, it should be "o' clock".

2) 10 <= minutes < 20: These all have different names (and you seem to be dealing with that in your code).

3) minutes >= 20: The minute names consist of two parts: the first word represents the tens digit and the second word represents the units digit. If you're clever, you might be able to reuse the units digit names from case 1) (or even from your hours part) so you don't have to retype it all yourself.

After that comes the AM/PM distinction.
This should be pretty easy -- if it's < 12, it's AM; otherwise, it's PM.

Rather than using a switch statement and typing out all of those cases, you could shorten things up a bit by using an array.
Something like
std::string names[] = {"", "one", "two", "three", "four", /* ... */};.
That way, you have a really easy way of converting numbers to word form -- for instance, names[3] == "three" and names[5] == "five" (see where I'm going there?).
Dear long double main,

You are amazing. Thank you for taking the time to explain everything in such detail that I was able to understand and edit my code so that it functions almost perfectly.
I've attached the corrected version.
I'm going to turn this one in for credit. I'll probably get docked points for not having AM and PM print out with the English output of the time, but hey, otherwise the code works now.
Thank you again for your help. I appreciate you!
Code:

/*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
Hey! I figured out where to put the IF Statement to print out either AM or PM after the English version of the time is output!
SWEET!!!!!!

I even found an error while checking the previously posted code for 0 00 input. I fixed that too!
Thank you again long double main!! :)

You've helped one more person enjoying programming. :)


/*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
Topic archived. No new replies allowed.