if(input = letter)?
Jul 15, 2011 at 1:49am UTC
Hello, before I ask my question i'd like to say thanks for looking at my question. Ok so, I am making a converter for Celsius into Fahrenheit vice versa. I am stuck at getting it to see if the return value of an input is a letter. Any help would be appreciated. Thanks again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <iostream>
using namespace std;
int main ()
{
char degree;
int celsius;
int fahrenheit;
cout << "Would you like to convert Celsius (c) or Fahrenheit (f)? " ;
cin >> degree;
if (degree == "c" ) {
cout << "Enter your desired degree in Celsius: " ;
cin >> celsius;
int factorc;
factorc = 212 - 32;
fahrenheit = factorc * celsius/100 + 32;
cout << celsius << " degrees Celsius is " << fahrenheit << " degrees fahrenheit." << endl;
system("PAUSE" );
return 0;
}
if (degree == "f" ) {
cout << "Enter your desired degree in Fahrenheit: " ;
cin >> fahrenheit;
int factorf;
factorf = 212 + 32;
celsius = factorf/fahrenheit * 100 - 32;
cout << fahrenheit << " degrees Fahrenheit is " << celsius << " degrees Celsius." << endl;
system("PAUSE" );
return 0;
}
}
Jul 15, 2011 at 1:55am UTC
Use single quotes around a character literal, like 'c' and 'f'.
Jul 15, 2011 at 1:55am UTC
You are using char type for the input. Replace the double quotes around c and f with single quotes.
Jul 15, 2011 at 1:56am UTC
Zhuge, thank you very much. I had no idea I have to use single quotes. =D
Jul 15, 2011 at 2:03am UTC
If cin gets a data type it doesn't expect, it will fail. You can check this with cin.fail().
1 2 3 4 5
if (cin.fail())
{
cin.clear();
cout << "Please enter a number!\n" ;
}
Jul 15, 2011 at 3:35am UTC
You need to make sure about case as well meaning
1 2
if ( degree =='c' || degree =='C' )
{....}
same thing with
Fahrenheit .
I also want to point that you need your variable type as either
float or
double .
Topic archived. No new replies allowed.