Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,982 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,376 people online right now. Registration is fast and FREE... Join Now!




C++ Switch Statement program problem

 
Reply to this topicStart new topic

C++ Switch Statement program problem

sox_18
28 Nov, 2006 - 05:36 PM
Post #1

New D.I.C Head
*

Joined: 28 Nov, 2006
Posts: 1


My Contributions
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.
CODE
#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.


User is offlineProfile CardPM
+Quote Post

Amadeus
RE: C++ Switch Statement Program Problem
28 Nov, 2006 - 06:53 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,355



Thanked: 51 times
Dream Kudos: 25
My Contributions
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:
CODE

switch (hours)
{
  case 1 :
//etc

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 07:10PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month