C++ program converts fahrenheit to celsius
Can someone help me understand why this gives an output of 0?
#include <iostream>
using namespace std;
int main() {
float celsius;
float fahrenheit;
cout << "Ent开发者_运维知识库er Celsius temperature: ";
cin >> celsius;
fahrenheit = (5/9) * (celsius + 32);
cout << "Fahrenheit = " << fahrenheit << endl;
return 0;
}
(5/9)
will by default be computed as an integer division and will be zero. Try (5.0/9)
Fahrenheit to celsius would be (Fahrenheit - 32) * 5 / 9
In C++, 5/9 computes the result as an integer as both the operands are integers. You need to give an hint to the compiler that you want the result as a float/double. You can do it by explictly casting one of the operands like ((double)5)/9;
EDIT
Since it is tagged C++, you can do the casting bit more elegantly using the static_cast
. For example: static_cast<double>(5)/9
. Although in this particular case you can directly use 5.0/9 to get the desired result, the casting will be helpful when you have variables instead of constant values such as 5.
In your code sample you are trying to divide an integer with another integer. This is the cause of all your trouble. Here is an article that might find interesting on that subject.
With the notion of integer division you can see right away that this is not what you want in your formula. Instead, you need to use some floating point literals.
I am a rather confused by the title of this thread and your code sample. Do you want to convert Celsius degrees to Fahrenheit or do the opposite?
I will base my code sample on your own code sample until you give more details on what you want.
Here is an example of what you can do :
#include <iostream>
//no need to use the whole std namespace... use what you need :)
using std::cout;
using std::cin;
using std::endl;
int main()
{
//Variables
float celsius, //represents the temperature in Celsius degrees
fahrenheit; //represents the converted temperature in Fahrenheit degrees
//Ask for the temperature in Celsius degrees
cout << "Enter Celsius temperature: ";
cin >> celsius;
//Formula to convert degrees in Celsius to Fahrenheit degrees
//Important note: floating point literals need to have the '.0'!
fahrenheit = celsius * 9.0/5.0 + 32.0;
//Print the converted temperature to the console
cout << "Fahrenheit = " << fahrenheit << endl;
}
Best way would be
#include <iostream>
using namespace std;
int main() {
float celsius;
float fahrenheit;
cout << "Enter Celsius temperature: ";
cin >> celsius;
fahrenheit = (celsius * 1.8) + 32;// removing division for the confusion
cout << "Fahrenheit = " << fahrenheit << endl;
return 0;
}
:)
Mine worked perfectly!
/* Two common temperature scales are Fahrenheit and Celsius.
** The boiling point of water is 212° F, and 100° C.
** The freezing point of water is 32° F, and 0° C.
** Assuming that the relationship bewtween these two
** temperature scales is: F = 9/5C+32,
** Celsius = (f-32) * 5/9.
***********************/
#include <iostream> // cin, cout
using namespace std; // System definition of cin and cout commands,
// if not, programmer would have to write every
// single line as: std::cout or std::cin
int main () // Main function
{
/* Declare variables */
double c, f;
cout << "\nProgram that changes temperature from Celsius to Fahrenheit.\n";
cout << "Please enter a temperature in Celsius: ";
cin >> c;
f = c * 9 / 5 + 32;
cout << "\nA temperature of " << c << "° Celsius, is equivalent to "
<< f << "° Fahrenheit.\n";
return 0;
}
The answer has already been found although I would also like to share my answer:
int main(void)
{
using namespace std;
short tempC;
cout << "Please enter a Celsius value: ";
cin >> tempC;
double tempF = convert(tempC);
cout << tempC << " degrees Celsius is " << tempF << " degrees Fahrenheit." << endl;
cin.get();
cin.get();
return 0;
}
int convert(short nT)
{
return nT * 1.8 + 32;
}
This is a more proper way to do this; however, it is slightly more complex then what you were going for.
It is the simplest one I could come up with, so wanted to share here,
#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type float
float cel, fah;
//Input the Temperature in given unit save them in ‘cel’
cout<<”Enter the Temperature in Celsius”<<endl;
cin>>cel;
//convert and save it in ‘fah’
fah=1.8*cel+32.0;
//show the output ‘fah’
cout<<”Temperature in Fahrenheit is “<<fah;
//get character
getch();
}
Source: Celsius to Fahrenheit
精彩评论