Where to begin with loop problem.

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.
As i have understood you need something as

1
2
celsius = 100;
while ( toFahrenheit( celsius ) != celsius ) celsius--; 
I have gotten such temperatures

celsius = -40, Fahrenheit = -40


#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.
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

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??
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
#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.
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 );

Read here http://www.cplusplus.com/forum/beginner/1988/
Topic archived. No new replies allowed.