Stop program at a certain temperature

I should write a program that asks for temperature and say that a temperature of 42 will stop the program. this is what i have so far i am not sure how that is done using a do while loop

//Display welcome message
#include <iostream>
using namespace std;


float tempCtoFarenheit(float temperatureC1)
{

return 32+(temperatureC1*9)/5;
}


int main ()
{ float temperatureC,temperatureF;

cout<<"Welcome to my second exercise"<< endl;

cout << " please input temperature in degrees celsius :";
cin>>temperatureC;

cout<<temperatureC<< "degrees celsius"<< endl;

temperatureF=tempCtoFarenheit(temperatureC);
cout<<"temperature in farenheit is:"<<temperatureF<<endl;

if (temperatureC <= 0)
cout<<"...but it is freezing!";
else
if(temperatureC > 39 && temperatureC <100)
cout <<"...but it is very hot!";
else
if (temperatureC >= 100)
cout<<"... but it is boiling !";
else
cout<<temperatureC << " degrees Celsius"<<endl;
return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
while(true)
{
  // everything you want to loop in here

  if (temperatureC  == 42)
  {
     break;  // break out of while loop
   }
}
Please explain in lay terms i am new to programming
Explain what? What is it you don't understand?
@RahRah, you might want to familiarize yourself with this material
http://www.cplusplus.com/doc/tutorial/control/
what i dont understand is do i do this in main or in the function


while(true)
{
// everything you want to loop in here

if (temperatureC == 42)
{
break; // break out of while loop
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

double to_farenheit( int celcius ) { return 32 + celcius * 9.0 / 5.0 ; }

int main()
{
    int celcius ;

    while( std::cout << "please input temperature in degrees celsius (enter 42 to quit): " &&
           std::cin >> celcius && celcius != 42 )
    {
        if( celcius < -273 ) std::cout << "invalid temperature (below absolute zero)\n" ;

        else
        {
            std::cout << "temperature in fahrenheit is " << to_farenheit(celcius) << " degrees\n" ;

            if( celcius < 0 ) std::cout << "but it is freezing!\n" ;
            // etc.
        }
    }
}
what i dont understand is do i do this in main or in the function

That's a design decision for you to make. How do you want your program to work? What do you want your function to do? Do you want a function that performs the entire loop? Or do you want a function that just converts one temperature to Fahrenheit, like the one you've originally written?


Last edited on
A loop, like the
1
2
3
4
while ( condition )
{
  // statements
}

repeats the statements as many times as the condition remains true.

Note that the statements should affect the condition somehow. If not, then the condition can never change from true to false and the loop will repeat forever.
1
2
3
4
while ( true )
{
  // statements
}

Is special: it has a condition that cannot be changed by statements. true is true. The break and return can jump out from such loop. The return exits the whole function. If that function is main(), then the program ends right then and there.


Where to put the loop then? That depends on what operations you need to repeat with the loop.

The main() can have loops in it. Other functions can have loops in them. You can have loops inside loops (aka "nested").
Last edited on
> what i dont understand is do i do this in main or in the function

As a general principle, a function that converts temperature from one scale to another should do just that much; nothing less and nothing more.
Topic archived. No new replies allowed.