Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 convertedPrintMenu();
cin >> letter;while(letter != 'Q')
{
if (letter =- ‘R’)
PrintMenu();
else
{
cout > tempIn;
if (letter == 'F')
cout > letter;
} //end of whilereturn 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?

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 whileHope this helps

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..
#includeusing 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.

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 casedefault: //error checking
printf("Not a letter choice"\n");
break;
}
}

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;
}

![]() |
![]() |
![]() |

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