Hi again everybody!

This post is about another easy c++ task you can do with the basic operators you have used before. The program has to convert from fahrenheit degrees to celsius, and tell you if water boils or not at the temperature you typed.

Some stuff you should know before:

  • The variable type float is used to introduce or operate with a number with decimals.
  • Include the library <iomanip> to use the function setprecision  which lets you set the number of decimals that you’ll use with the float variable.
  • Use the command “<<fixed <<setprecision(n) <<x; cout<<“. Being n = the number of decimals you want; and x = the float variable.

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

int main (){
float f, c;
char ans;
do  {
cout << “What is the temperature in Fahrenheit?” <<endl;
cin>> f;
c=(5.0 * (f-32) / 9.0);
cout<< “The temperature of “<<fixed <<setprecision(1) <<f; cout<< ” degrees Fahrenheit is ” <<c;         cout<< ” degrees in Celsius.” <<endl;

  if (c >= 100) {
cout<<“Water does boil at this temperature”<<endl;
}

  else {
cout<<“Water does not boil at this temperature(under typical conditions)”<<endl;
}

cout<< “Do you want to convert again? (y/n)”;
cin>> ans;
}
while(ans==’y’);
return 0;
}

As a plus, I added the option to run the program again, without typing the ./a.out (./a.exe in Windows) just by answering the question: “Do you want to convert again? (y/n)”. I did this using:

do {/* code */}

while(/* condition */);

Check my code and if you have a question please comment or ask me by twitter.

Thanks!

CC BY 4.0 #WSQ05 Temperature by eduardomacielm is licensed under a Creative Commons Attribution 4.0 International License.