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