Connecting Tech Pros Worldwide Forums | Help | Site Map

program converting Celsius to Fahrenheit

Newbie
 
Join Date: Apr 2008
Posts: 16
#1: Apr 9 '08
Hi
I was doing this program on Visual Studio 2005 where the program converts C to F and vise versa but after debugging it, it doesnt give the expected result. Could someone tell me what is wrong with my code???


#include <iostream>
#include <iomanip>

using namespace::std;
using std::fixed;

int main()
{
int conversionType = 0;
double temp = 0.0;
double result = 0.0;

cout << "Enter -1 (F to C) or 2 (C to F): ";
cin >> conversionType;
cout << "Enter temperature: ";
cin >> temp;

if (conversionType == '-1')
{
result = (temp - 32) * 5 / 9 ;
}
else if (conversionType == '2')
{
result = temp * 9/5 + 32;

} //end if

cout << "Result: " << result << endl;

return 0;
} //end of main function

Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#2: Apr 9 '08

re: program converting Celsius to Fahrenheit


Note that in C++ (and most programming languages), integer division is different than floating point division. Only integers are returned, so 9/5 is 1, and 5/9 is 0. You should replace those with 9.0/5.0 and 5.0/9.0, respectively.
vhm vhm is offline
Newbie
 
Join Date: Apr 2008
Location: Houston
Posts: 4
#3: Apr 14 '08

re: program converting Celsius to Fahrenheit


Quote:

Originally Posted by zelmila19

Hi
I was doing this program on Visual Studio 2005 where the program converts C to F and vise versa but after debugging it, it doesnt give the expected result. Could someone tell me what is wrong with my code???


#include <iostream>
#include <iomanip>

using namespace::std;
using std::fixed;

int main()
{
int conversionType = 0;
double temp = 0.0;
double result = 0.0;

cout << "Enter -1 (F to C) or 2 (C to F): ";
cin >> conversionType;
cout << "Enter temperature: ";
cin >> temp;

if (conversionType == '-1')
{
result = ((temp - 32) * 5) / 9 ;
}
else if (conversionType == '2')
{
result = ((temp * 9) /5) + 32;

} //end if

cout << "Result: " << result << endl;

return 0;
} //end of main function



Thats right Gagnon. Here you have something a litle more polished but very faithfull to the original.


<Full code solution removed>
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,195
#4: Apr 14 '08

re: program converting Celsius to Fahrenheit


vhm, please note we do not allow the posting of full code solutions on this site for a number of reasons.

I suggest you read our Posting Guidelines

Banfa
Administrator
Member
 
Join Date: Mar 2008
Posts: 109
#5: Apr 14 '08

re: program converting Celsius to Fahrenheit


If i were you, i would use
C = F - 32 / 1.8
and
F = C + 32 * 1.8

solves all the problems.
Member
 
Join Date: Jun 2007
Posts: 67
#6: Apr 15 '08

re: program converting Celsius to Fahrenheit


Quote:

Originally Posted by SpecialKay

If i were you, i would use
C = F - 32 / 1.8
and
F = C + 32 * 1.8

solves all the problems.

Except that those are actually wrong. They should be:

C = (F - 32) / 1.8
The parentheses are important, other wise it divides 32 by 1.8 and subtracts that from F, and the second one should be

F = C * 1.8 + 32
Reply