Program Finding Temperature using loops and counters

I need a program that finds the temperature that is the same in both Celsius and Fahrenheit. The formula to convert from Celsius to Fahrenheit is :

F = ((9 x C) / 5 )+ 32

Initialize the temperature to 100 degrees C. In a loop, decrement the Celsius value and compute the corresponding temperature in Fahrenheit until the two values are the same.
The formula may not give an exact result for every Celsius because you are working with integers.

It has to have a loop watchdog timer. Using a ‘const’ declaration for your maximum loop counter

This is what I got so far & no idea how to continue:


----------------------------------------------------------------------------------------------

//make Celcius (cels) & Farentheight (fahr) degree until they equal
//each other programs & must stop when both are equal...loop it while
//they are not equal.

#include <iostream>

using namespace std;

int main()
{
int lcv = 0;
int cels = 100;
int fahr;

int maxCount = 500;

while ( (fahr != cels ) && ( lcv < maxCount ) )
{
fahr = ( ( 9 * cels ) / 5 ) + 32;

-- cels;
}


if ( lcv = maxCount )
{
cout << "Bad loop \n";
}


cout << "\n\n";

system ("pause");
return 0;
}
Last edited on
Nothing to tell you but,you should select your code and press '<>' button,expert will answer u.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

static int convertToCels(int fahr)
{
    return ((fahr - 32) * 5)/ 9;
}
int main(){
    int fahr = 0;
    while(true){
        if (convertToCels(fahr) == fahr)
            break;
        ++fahr;
    }
    std::cout << "The answer is: " << fahr << '\n';
}

If you can see, the break statement is never reached.

Because from your formula(F = ((9 x C) / 5 )+ 32 ) there is minimal difference 32 degree, so they can't be equal
Actually the formula is correct and there IS a temperature that is exactly the same number in C an in F.

There are two problems with your code. Look carefully at lines 11 and 12. Put a cout << fahr in the loop to probe the variable as the loop runs...

You'll figure it out!
Sorry rapdudty. I thought Denis had reposted your code with code tags but it's different.

So, of course, you have different problems starting with the fact that fahr is not initialized to anything before your while loop starts.

Also, don't forget to increment your lcv in the loop or it doesn't do anything. AND, be careful when you check it for a 'bad loop'. = and == are NOT the same!

Again, put a statement like 'cout<< cels << " " << fahr << endl;' inside your loop to probe the values on each iteration. It helps expose problems when a loop is giving you unexpected behavior.

It's -40, by the way. Now you can verify whether your program is correct.

EDIT: Oh, and one more thing, just in case you want to do it the smart way. For
f(x)=a*x+b
g(x)=c*x+d
The intersection of f(x) and g(x) is (d-b)/(c-a). Obviously, if c==a, there is no intersection.
Last edited on
By the way,your code looks shitty .Use the "code" and "/code"(both in [] ) commands before and after the code - it will look more readable.And why don`t you try to do it with doubles ?It will be more accurate-when you do something , do it correctly.
Last edited on
Topic archived. No new replies allowed.