I am trying to write a program that produces a table thatshows the conversions f
ID: 3617600 • Letter: I
Question
I am trying to write a program that produces a table thatshows the conversions from Fahrenheit to Celsius for values from 0degrees F to 100 degrees F, and I need a line for every 5 degreeincrement. I have the Fahrenheit side set up fine, but all myvalues of Celsius come out as -32. I have thefollowing:#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main () { double TF=0, TC; cout<<"****Fahrenheit toCelsius*****"<<endl; cout<<" "<<endl; cout<<"Fahrenheit "<<"Celsius"<<endl; while(TF<=99) { TF+=5; TC=(5/9)*TF-32; cout<<" "<<TF<<setw(20)<<TC<<endl; } system("pause"); return 0; }
any ideas? I am trying to write a program that produces a table thatshows the conversions from Fahrenheit to Celsius for values from 0degrees F to 100 degrees F, and I need a line for every 5 degreeincrement. I have the Fahrenheit side set up fine, but all myvalues of Celsius come out as -32. I have thefollowing:
#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main () { double TF=0, TC; cout<<"****Fahrenheit toCelsius*****"<<endl; cout<<" "<<endl; cout<<"Fahrenheit "<<"Celsius"<<endl; while(TF<=99) { TF+=5; TC=(5/9)*TF-32; cout<<" "<<TF<<setw(20)<<TC<<endl; } system("pause"); return 0; }
any ideas? #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main () { double TF=0, TC; cout<<"****Fahrenheit toCelsius*****"<<endl; cout<<" "<<endl; cout<<"Fahrenheit "<<"Celsius"<<endl; while(TF<=99) { TF+=5; TC=(5/9)*TF-32; cout<<" "<<TF<<setw(20)<<TC<<endl; } system("pause"); return 0; }
any ideas?
Explanation / Answer
In your line "TC=(5/9)*TF-32;" Try instead "(5.0/9.0)*TF-32.0;" You need to use float values, not integer values (otherwise thecompiler just assumes an int, and rounds down). As a side note, if you had a variable which contains an integer,you can go float(variablename) and have it treated as a float (soyour result isn't rounded down).
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.