Sep 18, 2012 at 2:44am UTC
I must write a program that finds the temperature, as an integer, that is the same in both Celsius and Fahrenheit. The formula for this is
F=(9/5)C+32
The program should create two integer variables for the temperature in Celsius and Fahrenheit. Initialize the temperature to 100 degrees Celsius. In a loop, decrement the Celsius value and compute the corresponding temperature in Fahrenheit until the values are the same.
any help is appreciated, I don't really understand loops that well and I'm having trouble knowing where to start with this. thank you in advance.
Sep 18, 2012 at 3:16am UTC
#include <iostream>
using namespace std;
int main()
{
int celsius;
int fahrenheit;
celsius = 100;
while ( toFahrenheit( celsius ) != celsius ) celsius--;
F = (9/5)*C+32
I have this.. not sure where the equation comes into play.
Sep 18, 2012 at 3:22am UTC
now you need to define the function that will calculate the formula F = (9/5)*C+32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
int toFahrenheit( int celsius )
{
// here you shall do the calculations
}
int main()
{
int celsius;
int fahrenheit;
celsius = 100;
while ( ( fahrenheit = toFahrenheit( celsius ) ) != celsius ) celsius--;
std::cout << "celsius = " << celsius
<< ", fahrenheit = " << fahrenheit
<< std::endl;
return 0;
}
Last edited on Sep 18, 2012 at 3:39am UTC
Sep 18, 2012 at 3:46am UTC
int toFahrenheit( int celsius )
{
// here you shall do the calculations
^^^
So do I put
toFahrenheit = (9/5)*celsius+32
or
fahrenheit = (9/5)*celsius+32
}
you said define the function that will calculate the formula.. so do I have to use a function??
Sep 18, 2012 at 3:50am UTC
It will be simpler to write
1 2 3 4
int toFahrenheit( int celsius )
{
return ( ( 9.0 / 5 ) * celsius + 32 );
}
Now the program is ready.
Last edited on Sep 18, 2012 at 3:50am UTC
Sep 18, 2012 at 4:12am UTC
#include <iostream>
using namespace std;
int toFahrenheit( int celsius )
{
return ( ( 9 / 5 ) * celsius + 32 );
}
int main()
{
int celsius;
int fahrenheit;
celsius = 100;
while ( ( fahrenheit = toFahrenheit( celsius ) ) != celsius ) celsius--;
std::cout << "celsius = " << celsius
<< ", fahrenheit = " << fahrenheit
<< std::endl;
return 0;
}
not sure why but i'm not getting an output when i run the program.
thanks.
Sep 18, 2012 at 6:27am UTC
You forgot to set the line as he wrote it. change the line from:
return ( ( 9 / 5 ) * celsius + 32 );
to:
return ( ( 9.0 / 5 ) * celsius + 32 );
Sep 18, 2012 at 11:19am UTC
Read here http://www.cplusplus.com/forum/beginner/1988/