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

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




switch statement help

 
Reply to this topicStart new topic

switch statement help

hmapes
5 Nov, 2006 - 01:51 PM
Post #1

New D.I.C Head
*

Joined: 18 Sep, 2006
Posts: 13


My Contributions
CODE
#include <iostream>

using namespace std;


int main()
{
int hours, minutes;

cout<< "Enter time as hours and minutes separated by a space in numeric form:";
cin >> hours >> minutes;



Ok. I got the above code to actually compile. Now, I'm stuck as to what to do next. I need to use switch statements to complete the rest of the program. The program should read a time in numeric form and print in English. Should be in 24- hour time, but should have 12 hour AM/PM form. Could someone please give me some direction on how to get this started? I've tried some different coding, but it's incomplete and won't compile. I know the coding I've posted is pretty simple I just need some direction.

Thank you.

This post has been edited by Dark_Nexus: 5 Nov, 2006 - 03:51 PM
User is offlineProfile CardPM
+Quote Post

ihatepikingnames
RE: Switch Statement Help
5 Nov, 2006 - 02:04 PM
Post #2

D.I.C Head
Group Icon

Joined: 31 Jul, 2006
Posts: 115


Dream Kudos: 75
My Contributions
See if this doesn't help with your switch statement: http://www.dreamincode.net/code/snippet652.htm
ph34r.gif
User is offlineProfile CardPM
+Quote Post

hmapes
RE: Switch Statement Help
5 Nov, 2006 - 02:14 PM
Post #3

New D.I.C Head
*

Joined: 18 Sep, 2006
Posts: 13


My Contributions
QUOTE(ihatepikingnames @ 5 Nov, 2006 - 03:04 PM) *

See if this doesn't help with your switch statement: http://www.dreamincode.net/code/snippet652.htm
ph34r.gif



I read over that info. My problem is with what comes after Case 1 when I begin the switch. Do I case out the hours or minutes or what? I just don't know what I'm giving "cases" for. Dumb question I know.


User is offlineProfile CardPM
+Quote Post

ihatepikingnames
RE: Switch Statement Help
5 Nov, 2006 - 02:31 PM
Post #4

D.I.C Head
Group Icon

Joined: 31 Jul, 2006
Posts: 115


Dream Kudos: 75
My Contributions
QUOTE

I read over that info. My problem is with what comes after Case 1 when I begin the switch. Do I case out the hours or minutes or what? I just don't know what I'm giving "cases" for. Dumb question I know.


Post the code your having problems with then... I have no idea what your trying to do man!
and if you dont know what your giving cases for then what are you even trying to do?

case1: if (this is true) or (this exists) or (this unput == whatever)
DO THIS

P.S.- READ THIS!

This post has been edited by ihatepikingnames: 5 Nov, 2006 - 02:56 PM
User is offlineProfile CardPM
+Quote Post

NyeNye
RE: Switch Statement Help
5 Nov, 2006 - 05:03 PM
Post #5

D.I.C Head
**

Joined: 24 Sep, 2006
Posts: 248


My Contributions
Actually you could had both in the switch statement....

aside you had two inputs....

Post your code....

reading one fourth of programming and the rest is action..






User is offlineProfile CardPM
+Quote Post

hmapes
RE: Switch Statement Help
5 Nov, 2006 - 07:09 PM
Post #6

New D.I.C Head
*

Joined: 18 Sep, 2006
Posts: 13


My Contributions
CODE
#include <iostream>

using namespace std;


int main()
{
int hours, minutes;

cout<< "Please enter time as hours and minutes, separated by a space in numeric form:";
cin >> hours >> minutes;
}

switch (hours)
{
case 0: cout <<"Midnight"; break;
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 <<"Noon"; break;
case 13: cout <<"One"; break;
case 14: cout <<"Two"; break;
case 15: cout <<"Three"; break;
case 16: cout <<"Four"; break;
case 17: cout <<"Five"; break;
case 18: cout <<"Six"; break;
case 19: cout <<"Seven"; break;
case 20: cout <<"Eight"; break;
case 21: cout <<"Nine"; break;
case 22: cout <<"Ten"; break;
case 23: cout <<"Eleven"; break;
default: cout << "Error, Hours must be 0-23";
}

switch (minutes)
{
case 00: cout << " "; break;
case 1: cout << "Zero One"; break;
case 2: cout << "Zero Two"; break;
case 3: cout << "Zero Three"; break;
case 4: cout <<"Zero Four"; break;
case 5: cout <<"Zero Five"; break;
case 6: cout <<"Zero Six"; break;
case 7: cout <<"Zero Seven"; break;
case 8: cout <<"Zero Eight"; break;
case 9: cout <<"Zero 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 << "Fifteen"; break;
case 16: cout << "Sixteen"; break;
case 17: cout << "Seventeen"; break;
case 18: cout << "Eighteen"; break;
case 19: cout << "Nineteen"; break;
}
if ((minutes >= 20) && (minutes <= 29))
{
cout << "Twenty ";
seconddigit (minutes - 20);
}
else
{
    if ((minutes >=30) && (minutes <=39))
        cout<<"Thirty";
    seconddigit (minutes - 30);
}
else
{
    if ((minutes >=40) && (minutes <=49))
        cout << "Forty";
    seconddigit (minutes - 40);
}
else
{
    if ((minutes >=50) && (minutes <=59))
        cout <<"Fifty";
    seconddigit (minutes - 50);
}
if (hour >=1 && hour < 11)
cout <<"" << "AM";
else if (hour >=13 && hour < 23)
cout <<"" << "PM";

return 0;




Okay, so I've been working on this all day and feel like I'm pretty close. can anyone help me with these errors?
QUOTE
(22): error C2059: syntax error : 'switch'
(23): error C2143: syntax error : missing ';' before '{'
(23): error C2447: '{' : missing function header (old-style formal list?)
(51): error C2059: syntax error : 'switch'
(52): error C2143: syntax error : missing ';' before '{'
(52): error C2447: '{' : missing function header (old-style formal list?)
(74): error C2059: syntax error : 'if'
(75): error C2143: syntax error : missing ';' before '{'
(75): error C2447: '{' : missing function header (old-style formal list?)
(79): error C2059: syntax error : 'else'
(80): error C2143: syntax error : missing ';' before '{'
(80): error C2447: '{' : missing function header (old-style formal list?)
(85): error C2059: syntax error : 'else'
(86): error C2143: syntax error : missing ';' before '{'
(86): error C2447: '{' : missing function header (old-style formal list?)
(91): error C2059: syntax error : 'else'
(92): error C2143: syntax error : missing ';' before '{'
(92): error C2447: '{' : missing function header (old-style formal list?)
(97): error C2059: syntax error : 'if'
(99): error C2059: syntax error : 'else'
(102): error C2059: syntax error : 'return'


edit: added tags ~ jayman9
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Switch Statement Help
5 Nov, 2006 - 08:16 PM
Post #7

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
You have a closing bracket out of place.
CODE

#include <iostream>
using namespace std;


int main()
{
int hours, minutes;

cout<< "Please enter time as hours and minutes, separated by a space in numeric form:";
cin >> hours >> minutes;
//you had a bracket here that belongs at the very end.

switch (hours)
{
case 0: cout <<"Midnight"; break;
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 <<"Noon"; break;
case 13: cout <<"One"; break;
case 14: cout <<"Two"; break;
case 15: cout <<"Three"; break;
case 16: cout <<"Four"; break;
case 17: cout <<"Five"; break;
case 18: cout <<"Six"; break;
case 19: cout <<"Seven"; break;
case 20: cout <<"Eight"; break;
case 21: cout <<"Nine"; break;
case 22: cout <<"Ten"; break;
case 23: cout <<"Eleven"; break;
default: cout << "Error, Hours must be 0-23";
}

switch (minutes)
{
case 00: cout << " "; break;
case 1: cout << "Zero One"; break;
case 2: cout << "Zero Two"; break;
case 3: cout << "Zero Three"; break;
case 4: cout <<"Zero Four"; break;
case 5: cout <<"Zero Five"; break;
case 6: cout <<"Zero Six"; break;
case 7: cout <<"Zero Seven"; break;
case 8: cout <<"Zero Eight"; break;
case 9: cout <<"Zero 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 << "Fifteen"; break;
case 16: cout << "Sixteen"; break;
case 17: cout << "Seventeen"; break;
case 18: cout << "Eighteen"; break;
case 19: cout << "Nineteen"; break;
}
if ((minutes >= 20) && (minutes <= 29))
{
cout << "Twenty ";
seconddigit (minutes - 20);
}
else
{
    if ((minutes >=30) && (minutes <=39))
        cout<<"Thirty";
    seconddigit (minutes - 30);
}
else
{
    if ((minutes >=40) && (minutes <=49))
        cout << "Forty";
    seconddigit (minutes - 40);
}
else
{
    if ((minutes >=50) && (minutes <=59))
        cout <<"Fifty";
    seconddigit (minutes - 50);
}
if (hour >=1 && hour < 11)
cout <<"" << "AM";
else if (hour >=13 && hour < 23)
cout <<"" << "PM";

return 0;
} // moved the bracket to this position

User is offlineProfile CardPM
+Quote Post

hmapes
RE: Switch Statement Help
5 Nov, 2006 - 08:26 PM
Post #8

New D.I.C Head
*

Joined: 18 Sep, 2006
Posts: 13


My Contributions
CODE

#include <iostream>

using namespace std;

//function prototype
void seconddigit (int minutes);

int main()
{
// Declare variables
int hours, minutes;


//Initialize variables and output
cout<< "Please enter time as hours and minutes, separated by a space in numeric form:";
cin >> hours >> minutes;


switch (hours)
{
case 0: cout <<"Midnight"<< ""; break;
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 8: cout <<"Eight"<<""; break;
case 9: cout <<"Nine"<<""; break;
case 10: cout <<"Ten"<<""; break;
case 11: cout <<"Eleven"<<""; break;
case 12: cout <<"Noon"<<""; break;
case 13: cout <<"One"<<""; break;
case 14: cout <<"Two"<<""; break;
case 15: cout <<"Three"<<""; break;
case 16: cout <<"Four"<<""; break;
case 17: cout <<"Five"<<""; break;
case 18: cout <<"Six"<<""; break;
case 19: cout <<"Seven"<<""; break;
case 20: cout <<"Eight"<<""; break;
case 21: cout <<"Nine"<<""; break;
case 22: cout <<"Ten"<<""; break;
case 23: cout <<"Eleven"<<""; break;
default: cout << "Error, Hours must be 0-23";
}

//function definition
void seconddigit (int minutes);

switch (minutes)
{
case 0: cout << " "; break;
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 << "Fifteen"; break;
case 16: cout << "Sixteen"; break;
case 17: cout << "Seventeen"; break;
case 18: cout << "Eighteen"; break;
case 19: cout << "Nineteen"; break;
}
if ((minutes >= 20) && (minutes <= 29))
{
    cout << "Twenty ";
    seconddigit (minutes - 20);
}
else if ((minutes >=30) && (minutes <=39))
{
    cout<<"Thirty";
    seconddigit (minutes - 30);
}
else if ((minutes >=40) && (minutes <=49))
{    
    cout << "Forty";
    seconddigit (minutes - 40);
}
else if ((minutes >=50) && (minutes <=59))
{        
    cout <<"Fifty";
    seconddigit (minutes - 50);
}
if (hours >=1 && hours < 11)
{
    cout <<"" << "AM";
}
else if (hours >=13 && hours < 23)
{
    cout <<"" << "PM";
}



cin.get();
}

OK. I fixed that problem. Now, I'm getting these two errors -

error LNK2019: unresolved external symbol "void __cdecl seconddigit(int)" (?seconddigit@@YAXH@Z) referenced in function _main
fatal error LNK1120: 1 unresolved externals

Also, I could get my program to compile before, but apparently i've changed something where it won't anymore. Thank you for your help. This is just really frustrating! You think you type so much code and then once you are done - it won't even work. oh well...gotta keep going with it I guess.

EDIT : Tags added - b2c
User is offlineProfile CardPM
+Quote Post

ihatepikingnames
RE: Switch Statement Help
6 Nov, 2006 - 03:25 PM
Post #9

D.I.C Head
Group Icon

Joined: 31 Jul, 2006
Posts: 115


Dream Kudos: 75
My Contributions
QUOTE(hmapes @ 5 Nov, 2006 - 10:26 PM) *

CODE

#include <iostream>

using namespace std;

//function prototype
void seconddigit (int minutes);

int main()
{
// Declare variables
int hours, minutes;


//Initialize variables and output
cout<< "Please enter time as hours and minutes, separated by a space in numeric form:";
cin >> hours >> minutes;


switch (hours)
{
case 0: cout <<"Midnight"<< ""; break;
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 8: cout <<"Eight"<<""; break;
case 9: cout <<"Nine"<<""; break;
case 10: cout <<"Ten"<<""; break;
case 11: cout <<"Eleven"<<""; break;
case 12: cout <<"Noon"<<""; break;
case 13: cout <<"One"<<""; break;
case 14: cout <<"Two"<<""; break;
case 15: cout <<"Three"<<""; break;
case 16: cout <<"Four"<<""; break;
case 17: cout <<"Five"<<""; break;
case 18: cout <<"Six"<<""; break;
case 19: cout <<"Seven"<<""; break;
case 20: cout <<"Eight"<<""; break;
case 21: cout <<"Nine"<<""; break;
case 22: cout <<"Ten"<<""; break;
case 23: cout <<"Eleven"<<""; break;
default: cout << "Error, Hours must be 0-23";
}

//function definition
void seconddigit (int minutes);

switch (minutes)
{
case 0: cout << " "; break;
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 << "Fifteen"; break;
case 16: cout << "Sixteen"; break;
case 17: cout << "Seventeen"; break;
case 18: cout << "Eighteen"; break;
case 19: cout << "Nineteen"; break;
}
if ((minutes >= 20) && (minutes <= 29))
{
    cout << "Twenty ";
    seconddigit (minutes - 20);
}
else if ((minutes >=30) && (minutes <=39))
{
    cout<<"Thirty";
    seconddigit (minutes - 30);
}
else if ((minutes >=40) && (minutes <=49))
{    
    cout << "Forty";
    seconddigit (minutes - 40);
}
else if ((minutes >=50) && (minutes <=59))
{        
    cout <<"Fifty";
    seconddigit (minutes - 50);
}
if (hours >=1 && hours < 11)
{
    cout <<"" << "AM";
}
else if (hours >=13 && hours < 23)
{
    cout <<"" << "PM";
}



cin.get();
}

OK. I fixed that problem. Now, I'm getting these two errors -

error LNK2019: unresolved external symbol "void __cdecl seconddigit(int)" (?seconddigit@@YAXH@Z) referenced in function _main
fatal error LNK1120: 1 unresolved externals

Also, I could get my program to compile before, but apparently i've changed something where it won't anymore. Thank you for your help. This is just really frustrating! You think you type so much code and then once you are done - it won't even work. oh well...gotta keep going with it I guess.

EDIT : Tags added - b2c



You made 2 different def. for seconddigit but didnt include the function of second digit.

CODE

#include <iostream>

using namespace std;

//function prototype
void seconddigit(int minutes){
if ((minutes >= 20) && (minutes <= 29))
{
    cout << "Twenty ";
    seconddigit(minutes - 20);
}
else if ((minutes >=30) && (minutes <=39))
{
    cout<<"Thirty";
    seconddigit  (minutes - 30);
}
else if ((minutes >=40) && (minutes <=49))
{    
    cout << "Forty";
    seconddigit  (minutes - 40);
}
else if ((minutes >=50) && (minutes <=59))
{        
    cout <<"Fifty";
    seconddigit  (minutes - 50);
}
};

int main()
{
// Declare variables
int hours, minutes;


//Initialize variables and output
cout<< "Please enter time as hours and minutes, separated by a space in numeric form:";
cin >> hours >> minutes;


switch (hours)
{
case 0: cout <<"Midnight"<< ""; break;
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 8: cout <<"Eight"<<""; break;
case 9: cout <<"Nine"<<""; break;
case 10: cout <<"Ten"<<""; break;
case 11: cout <<"Eleven"<<""; break;
case 12: cout <<"Noon"<<""; break;
case 13: cout <<"One"<<""; break;
case 14: cout <<"Two"<<""; break;
case 15: cout <<"Three"<<""; break;
case 16: cout <<"Four"<<""; break;
case 17: cout <<"Five"<<""; break;
case 18: cout <<"Six"<<""; break;
case 19: cout <<"Seven"<<""; break;
case 20: cout <<"Eight"<<""; break;
case 21: cout <<"Nine"<<""; break;
case 22: cout <<"Ten"<<""; break;
case 23: cout <<"Eleven"<<""; break;
default: cout << "Error, Hours must be 0-23";
}




switch (minutes)
{
case 0: cout << " "; break;
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 << "Fifteen"; break;
case 16: cout << "Sixteen"; break;
case 17: cout << "Seventeen"; break;
case 18: cout << "Eighteen"; break;
case 19: cout << "Nineteen"; break;
}

if (hours >=1 && hours < 11)
{
    cout <<"" << "AM";
}
else if (hours >=13 && hours < 23)
{
    cout <<"" << "PM";
}



cin.get();
}



You may want to clean it up a little bit but it runs find and does its job

User is offlineProfile CardPM
+Quote Post

NyeNye
RE: Switch Statement Help
6 Nov, 2006 - 05:33 PM
Post #10

D.I.C Head
**

Joined: 24 Sep, 2006
Posts: 248


My Contributions
Does "0" had a corresponding word

Lol

CODE

switch (minutes)
{
case 0: cout << " "; break;
case 1: cout << "One"; break;

.......


i
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:19PM

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