# completed a program; desire input

• 05-25-2003
spirited
completed a program; desire input
/*Write a program that gives the user three choices:
Convert from Fahrenheit to Celsius, convert from Celsius
to Fahrenheit, or quit. If the third choice is selected,
the program should prompt the user for either a Fahrenheit
or Celsius temperature, as appropriate, and then calculate
and display the corresponding temperature. Use the
conversion equations: F = (9/5) C + 32 and C = (5/9) (F - 32) */

/* Algorithm:
A. Output a menu such as:
Temperature Conversion Program
Choose from the following options:
1. To Convert from Fahrenheit to Celsius
2. To convert from Celsius to Fahrenheit
3. To Quit
B. Store selection (store intergers 1, 2, 3)
with a cin statement
C. If the selection is 1 prompt the user for
fahtemp, store fahrenheit, calculate celcius
and output celcius
D. if selection is 2, store celcius, calc fah,
and output fah
E. If selection is 3, quit the program

#include <iostream.h>
#include <iomanip.h>

int main()
{
char selection;
float var1, var2, var3, temp, fahren, celsius, quit;
var1 = 1;
var2 = 2;
var3 = quit;

//I am not sure how to handle process a "the menu portion."
//I know this is not right, but here's my effort.
//any suggestions
cout << "Temperature Conversion Program"
cout << "\nChoose from the following options: ";
cout << "\nEnter a 1 if the temperature is in Fahrenheit";
cin >> var1;
cout << "\nEnter a 2 if the temperature is in Celsius";
cin >> var2;
cout << "\nEnter a 3 to quit the program";
cin >> var3;

cout << setiosflags (ios::fixed)
<< setiosflags (ios::showpoint)
<< setprecision (2);

//I am confused how to set the program to quit if 'q' is selected
if (selection== '1)
{
celsius = (5.0 / 9.0) * (temp - 32.0);
cout << "\nThe equivalent Celsius temperature is "
<< celsius << endl;

}
else
{
fahren = (9.0 / 5.0) * (temp + 32.0);
cout << "\nThe equivalent Fahrenheit temperature is "
<< fahren << endl;
}
//I am confused how to set the program to quit if 'q' is selected

return 0;
}
• 05-25-2003
Magos
What exactly are you asking? I can tell that your program is kinda messed up. "Selection" has an undefined value when testing it and your menu is plain weird.
Try something like this:
Code:

```cin >> Selection; switch(Selection) {   case 'C':   case 'c':       //Celcius       break;   case 'F':   case 'f':       //Fahrenheit       break;   case 'Q':   case 'q':       //Quit       break;   default:       //Handle invalid input       break; }```
• 05-25-2003
Prelude
>//I am confused how to set the program to quit if 'q' is selected
Code:

```cout<<"1) Fahrenheit"     <<"2) Celsius"     <<"3) Quit"     <<end; cin>> selection; if (selection == 1) {     // Calculate Celsius } else if (selection == 2) {     // Calculate Fahrenheit } else {     // Assume anything else is 3 }```