Computing.Net > Forums > Programming > C++ Switch statement

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ Switch statement

Reply to Message Icon

Name: synk
Date: February 24, 2003 at 02:17:52 Pacific
OS: xp
CPU/Ram: 800mhz
Comment:

I've done the following program that converts temperatures, Fahrenheit to Celsius and vice versa.

#include
using namespace std;

int ConvertedTemp(int, char);
void PrintMenu();

int main ()
{
char letter; // Place to store input letter
int tempIn; // Temperature to be converted

PrintMenu();
cin >> letter;

while(letter != 'Q')
{
if (letter =- ‘R’)
PrintMenu();
else
{
cout > tempIn;
if (letter == 'F')
cout > letter;
} //end of while

return 0 ;
}

int ConvertedTemp(int tempIn. char letter)
{
if (letter == ‘C’)
return (9 * tempIn / 5) + 32;
else
return 5 * (tempIn - 32) / 9;
}

void PrintMenu()
{
cout “Input Menu” endl endl;
cout “F: Convert from Fahrenheit to Celsius” endl;
cout “C: Convert from Celsius to Fahrenheit” endl;
cout “R: Reprint the menu” endl;
cout “Q: Quit” endl;
cout “Type a C, F, R or Q; then press return.” endl;
}

How do I use switch statements instead of if statements in this program?




Sponsored Link
Ads by Google

Response Number 1
Name: rde
Date: February 24, 2003 at 03:15:13 Pacific
Reply:

while(1){ //program never exits this loop
cin >> letter;
switch(letter){
case 'q':
exit(0);
break;
case'r':
PrintMenu();
break;
case'c':
//Code to convert temp
break;
case 'f':
//Code to convert temp
break;
default:
//what to do if another letter was typed
}//end switch
}//end while

Hope this helps


0

Response Number 2
Name: synk
Date: February 24, 2003 at 05:12:41 Pacific
Reply:

How do I put that into my existing code?
I seem to be getting alot of errors.


0

Response Number 3
Name: rde
Date: February 24, 2003 at 09:18:38 Pacific
Reply:

Just replace the while loop.

What errors do you get?


0

Response Number 4
Name: synk
Date: February 24, 2003 at 10:03:41 Pacific
Reply:

I've made a more working example:

// Program Convert converts a temperature in Fahrenheit to

// Celsius or a temperature in Celsius to Fahrenheit

// depending on whether the user enters an F or a C..


#include

using namespace std;


int ConvertedTemp(int, char);
void PrintMenu();


int main ()

{

char letter; // Place to store input letter

int tempIn; // Temperature to be converted

PrintMenu();
cin >> letter;

while (letter != 'Q')

{
if (letter == 'R')
PrintMenu();

cout > tempIn;


if (letter == 'F')

cout > letter;

}

return 0;

}

// *******************************************

int ConvertedTemp(int tempIn, char letter)

{

if (letter == 'C')

return (9 * tempIn / 5) + 32;

else

return 5 * (tempIn - 32) / 9;}

// *******************************************

void PrintMenu()
{
cout "Input Menu" endl endl;
cout "F: Convert from Fahrenheit to Celsius" endl;
cout "C: Convert from Celsius to Fahrenheit" endl;
cout "R: Reprint the menu" endl;
cout "Q: Quit" endl;
cout "Type a C, F, R or Q; then press return." endl;
}

Its basically the if-else part thats confusing me. If it was straight if-dosomething() - it would be easy.


0

Response Number 5
Name: MrKnow
Date: February 24, 2003 at 21:04:59 Pacific
Reply:

switch looks much nicer than a bunch of if and else if statements

while(1)
{
switch(letter) //letter switched each case
{
//case is like any if statement
//case Q is like if(letter == 'Q')
//but you can make it easier by putting
//multiple cases before you do your task and
//break
case 'Q':
case 'q':
cout {{ "Quit" {{ endl; //do something
exit(0); //this will exit the program
case 'F':
case 'f':
ConvertTemp() //do conversion
break; //break out of switch
case 'C':
......and so on...and make sure there is a break after each case

default: //error checking
printf("Not a letter choice"\n");
break;


}
}


0

Related Posts

See More



Response Number 6
Name: gregrego
Date: March 1, 2003 at 13:22:30 Pacific
Reply:

synk your working example actually doesnt work :P becaaaauseeeee.. you didnt include your iostream, this will cause the compiler or linker im not sure which one but one of em will throw an error whereever you make a cout, or cin statement, i dont even think the switch would work lol

All cout statements must also have "
using namespace std;

int ConvertedTemp(int, char);
void PrintMenu();

int main ()
{
bool b = true;
bool b2 = false;
char letter; // Place to store input letter
char Units;
int tempIn, tempOut;

PrintMenu();

do{
b2 = false;
cin >> letter;
switch(letter)
{
case 'q':case 'Q':
b = false;
break;
case'r':case'R':
PrintMenu();
b2 = true;
break;
case'c':case'C':
Units = 'F';
cout > tempIn;
tempOut = ConvertedTemp(tempIn,letter);
break;
case 'f':case'F':
Units = 'C';
cout > tempIn;
tempOut = ConvertedTemp(tempIn,letter);
break;
default:
cout "Error: Must Enter a Menu Item" endl;
}//end switch
}while(b2);

if(b)
{
cout "Your Converted Temperature is " tempOut " degrees " Units endl;
}
return 0 ;
}

int ConvertedTemp(int tempIn, char letter)
{
int iTemp = 0;
if ((letter == 'C')||(letter == 'c'))
iTemp = (9 * tempIn / 5) + 32;
else if((letter == 'F')||(letter == 'f'))
iTemp = 5 * (tempIn - 32) / 9;

return iTemp;
}

void PrintMenu()
{
cout "Input Menu" endl endl;
cout "F: Convert from Fahrenheit to Celsius" endl;
cout "C: Convert from Celsius to Fahrenheit" endl;
cout "R: Reprint the menu" endl;
cout "Q: Quit" endl;
cout "Type a C, F, R or Q; then press return." endl;
}


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ Switch statement

Switch statement code in C++ ..? www.computing.net/answers/programming/switch-statement-code-in-c-/6052.html

How to use Switch statement in vb www.computing.net/answers/programming/how-to-use-switch-statement-in-vb/6687.html

Switch Statement in C www.computing.net/answers/programming/switch-statement-in-c/8277.html