using sentinel in a while loop

I don't know why i keep getting the value of sentinel converted to Fahrenheit every time i enter the value -1111. The program suppose to finish instead.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
double tempCelsius,
tempFahrenheit;
cout<<"Enter a temperature in Celsius and the program TempLoop will convert it to Farenheit ,"
<<"then enter -1111 when finished."<<endl;

while(tempCelsius != -1111)
{
cout<<"What's the temperature in Celsius? ";
cin>>tempCelsius;

tempFahrenheit= (tempCelsius * 1.8) + 32;

cout<<fixed<<showpoint<<setprecision(1)<<endl;
cout<<tempCelsius<<" in celsius equals "<<tempFahrenheit<<" in Fahrenheit."<<endl;

}


cout<<"The program is finished, Thank you"<<endl;

return 0;

}
1
2
3
4
5
6
7
cout<<"What's the temperature in Celsius? ";
cin>>tempCelsius;

tempFahrenheit= (tempCelsius * 1.8) + 32;

cout<<fixed<<showpoint<<setprecision(1)<<endl;
cout<<tempCelsius<<" in celsius equals "<<tempFahrenheit<<" in Fahrenheit."<<endl;

This is the inside of your while loop. First you ask for the temperature, and then it does the conversion.
It isn't until the next iteration that it checks for the while loop condition.

You can change your logic to fit this pattern:

ask for temperature
get input for temperature
while (temperature != sentinel)
{
    do calculations
    display result
    
    ask for temperature
    get input for temperature
}
Thank you, it worked
Topic archived. No new replies allowed.