1 Replies - 12672 Views - Last Post: 28 November 2006 - 07:53 PM Rate Topic: -----

#1 sox_18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-November 06

C++ Switch Statement program problem

Posted 28 November 2006 - 06:36 PM

Hi all,
I have a programing question that reads: 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 arespecified in 24-hour time(14 is 2 pm, etc.), but the output should be in 12-hour AM/PM form. Note that noon and midnight are special cases. Examples:
Enter Time: 12 00
Noon
Enter time: 6 44
Six forty four AM
And it says that this program should provide ample opportunity to use Switch Statements.

So, I have used switch statements, plenty of them as you will see in the code, but I am not sure how to do the AM/ PM part. Should I use an while loop for the entire code. I believe my switch statements are correct, but I could be wrong. Also if you spot anything else wrong or weird with my code that would be great if you could let me know. Or should I use void functions to complete, I am somewhat lost. I think I have to use the get function. So I will try that, but I welcome all suggestions.

I am working in C++ with Pico Text editor. Thanks for all of your help, you are a big help. And I have not compiled it yet either because it is not complete.
#include <iostream>
#include <string>
using namespace std;

int main ()

{

int time;
int minutes;
int hours;
int tens;

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

if (time > 0000 && time < 1200)
   time = AM;

else
   time = PM;


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;

}

cout << hours << " " << tens << " " << minutes << " " << time;
return 0;
}


Note: Tens are the tens of minutes. Should I use subfunctions?

Thanks again.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Switch Statement program problem

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: C++ Switch Statement program problem

Posted 28 November 2006 - 07:53 PM

First things first...you have decalred time as variable of type int, but are trying to assign it a string value. you will therefore have to declare variable time to be of type string, or a character array or pointer.

Secondly, you are switching on variable hours, also declared to be of type int, but are tryng to switch on one or more characters...it will need to be something like:
switch (hours)
{
  case 1 :
//etc


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1