Feb 8, 2011 at 10:03pm UTC
It compiled, but there's an error in the formula to convert farenheit to celcius. No matter what I put in, it always gives me
___ Degrees F = -0 Degrees C
The ___ is the input, not what it actually prints.
I have the code:
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
//convert temperature
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int choice;
double cel,far;
while (1)
{
cout << "Choice (1 = Farenheit to Celcius, 2 = Celcius to Farenheit, 3 = CLS) " << endl;
cin >> choice;
switch (choice)
{
case 1: // F > C
cout << "Degrees F: " ;
cin >> far;
cel = ((far-32)*(5/9));
cout << endl << far << " Degrees F = " << cel << " Degrees C" << endl;
break ;
case 2:
cout << " Degrees C: " ;
cin >> cel;
far = (cel*(9/5)+32);
cout << endl << cel << " Degrees C = " << far << "Degrees F" << endl;
break ;
case 3:
system("CLS" );
break ;
default :
cout << "Invalid input" << endl;
break ;
}
}
return 0;
}
EDIT:
Celcius to farenheit and the CLS option both work fine.
Last edited on Feb 8, 2011 at 10:14pm UTC
Feb 8, 2011 at 10:23pm UTC
Your calculations should be like this: (far - 32) * 5/9
Feb 8, 2011 at 10:30pm UTC
Thanks Tamao. It's working now. I should stop using so many parenthasis.
Feb 9, 2011 at 2:22am UTC
Just as an aside...
Some people like to put in extra parentheses to make order of operations explicit, and some don't.
I myself prefer minimal parens because I find over-parenthesizing an expression to detract from readability rather than enhance it.
Anyway, yes, in this case parenthesizing (5/9) causes the division to be performed in the integer domain since both divisor and dividend are integral (which is of course not what you wanted).