Hello all, I am a major noob a C++ and need some help with managing case. we are to write program that asks the user to enter a temperature and a single letter, F for Farenheit or C for Celsius. The program should manage the upper or lower case input symbols, F or f for Fahrenheit or C or c for Celsius. Then, terminate the program for all of the other input symbols. If the letter is F, the program converts the input temperature in F to the temperature in Celsius. If the letter is C, the program converts the input temperature in C to the temperature in Farenheit. The program displays input temperature, input letter symbol, converted temperature and the converted temperature unit symbol. I have most of the program complete but cannot figure out how to get it to recognize the letters F or C....Here is my code

//Advanced Temperature Conversion Program

#include<iostream>
#include<iomanip>
#include<cmath>

using namespace std;

//Section 1: define variables
double temp;
int sym;
double conv;


int main()
{
	cout <<fixed<<setprecision(2)<<showpoint; //show decimal to 2 places

//Section 2: User input
	cout <<"This is a simple program that will convert a temperature to either"<<endl;
	cout <<"celsius or farenheight."<<endl;
	cout <<endl;
	cout <<"Please enter a temperature. ";
	cout <<endl;
	cin >>temp;
	cout <<endl;
	cout <<"What would you like to convert to?";
	cout <<endl;
	cout <<"Enter F for Farenheight or C for Celsius. ";
	cin >>sym;
	cout <<endl;
	switch (sym)
		
	{
	case 'F':
	case 'f':
	case 'C':
	case 'c':

		if (sym == 'F' )
		{
			cout <<setw(10)<<"Input Temp"<<setw(14)<<"Input Symbol"<<setw(17)<<"Converted Temp"<<setw(18)<<"Converted Symbol";
			cout <<endl;
			cout <<setw(10)<<"__________"<<setw(14)<<"____________"<<setw(17)<<"______________"<<setw(18)<<"________________";
			cout <<endl;
			cout <<temp<<setw(12)<<"F"<<setw(17)<<(temp-32)*5/9<<setw(18)<<"C";
			cout <<endl;
		}
			
		else if (sym == 'C')
		{
			cout <<setw(10)<<"Input Temp"<<setw(14)<<"Input Symbol"<<setw(17)<<"Converted Temp"<<setw(18)<<"Converted Symbol";
			cout <<endl;
			cout <<setw(10)<<"__________"<<setw(14)<<"____________"<<setw(17)<<"______________"<<setw(18)<<"________________";
			cout <<endl;
			cout <<temp<<setw(12)<<"C"<<setw(17)<<9/5 * (temp+32)<<setw(18)<<"F";
			cout <<endl;
			cout <<endl;
		}
				
	}
	
		
		system ("pause");
		return 0;
}

First of all, to enter and process the user's conversion choice, sym must be of type char.

Then, in your switch, you don't need the if statements - the switch does that.

switch ( sym )
{
  case 'c':
  case 'C':   //do the Celsius conversion
                  break;
   case 'f':
   case 'F':  //do the Fahrenheit conversion
                  break;
    default:  //error message, quitting program
}

Thanks for the help, this work great.....now I just have to get my formulas to work properly.

as I mentioned the suggestion worked great.....can anyone tell my why my celsius to farenheit isnt working? if I input 100 as the temp and c as my convert to....i get 132 rather than 212.....I have tryed to manipulate the formula every which way...am I passing something else somewhere that I am not seeing? Celsius to farenheit works flawlessly
updated code is

[B]//Advanced Temperature Conversion Program

#include<iostream>
#include<iomanip>
#include<cmath>

using namespace std;

//Section 1: define variables
double temp;
char sym;
double conv;


int main()
{
	cout <<fixed<<setprecision(2)<<showpoint; //show decimal to 2 places

//Section 2: User input
	cout <<"This is a simple program that will convert a temperature to either"<<endl;
	cout <<"celsius or farenheight."<<endl;
	cout <<endl;
	cout <<"Please enter a temperature. ";
	cout <<endl;
	cin >>temp;
	cout <<endl;
	cout <<"Is this farenheiht or celsius?";
	cout <<endl;
	cout <<"Enter F for farenheiht or C for celsius. ";
	cin >>sym;
	cout <<endl;
	switch (sym)
		
	{
	case 'f':
	case 'F':
			cout <<setw(10)<<"Input Temp"<<setw(14)<<"Input Symbol"<<setw(17)<<"Converted Temp"<<setw(18)<<"Converted Symbol";
			cout <<endl;
			cout <<setw(10)<<"__________"<<setw(14)<<"____________"<<setw(17)<<"______________"<<setw(18)<<"________________";
			cout <<endl;
			cout <<temp<<setw(12)<<"F"<<setw(17)<<(temp-32)*5/9<<setw(18)<<"C";
			cout <<endl;
			break;
		
	case 'c':
	case 'C':
			cout <<setw(10)<<"Input Temp"<<setw(14)<<"Input Symbol"<<setw(17)<<"Converted Temp"<<setw(18)<<"Converted Symbol";
			cout <<endl;
			cout <<setw(10)<<"__________"<<setw(14)<<"____________"<<setw(17)<<"______________"<<setw(18)<<"________________";
			cout <<endl;
			cout <<temp<<setw(12)<<"C"<<setw(17)<<(9/5)*temp+32<<setw(18)<<"F";
			cout <<endl;
			cout <<endl;
			break;
	default: cout <<"Invalid symbol, exiting program"<<endl;
		}
			

		
		
		system ("pause");
		return 0;
}[/B]

F to C looks like it should work OK.

C to F, however

(9/5) * temp + 32

Remember that integer divided by integer gives an integer result, so 9/5 => 1 !!
Change that to ( 9.0/5.0 ) and it should be better.

Edited 6 Years Ago by vmanes: n/a

vmanes, your a life saver.....I should have thought about that....it is usually the little things.....works like a charm

This article has been dead for over six months. Start a new discussion instead.