### Fahrenheit to Conversion Help

I am in class and my teacher seems to refuse to help me like he has all semester. Could anyone help me out with this?

 Loop 4 times to do the following
o Prompt the user to enter a degree in Fahrenheit.
o Convert the degree from Fahrenheit to Celsius.

Sample Output
CSCI 193 Program Two, Fall 2012
----------------------------------
Fahrenheit Values entered by user and their equivalent Celsius Values
Iterations Fahrenheit = Celsius
========== ========== =======
1 108.0 42.2
2 24.0 -4.4
3 50.0 10.0
4 32.0 0.0

I cant seem to get it to show multiple iterations. This is my current code:
 1234567891011121314151617181920212223242526272829303132 #include #include #include #include #include using namespace std; int main() { float cels, fahr; char Fahrenheit, Celsius, Iterations; int MIN_NUMBER = 1, MAX_NUMBER = 4; int iter; for (iter = MIN_NUMBER; iter <= MAX_NUMBER; iter++) { cout << "Please enter the Celsius Degree you would like to convert: "; //Looped question cin >> fahr; cels = (fahr - 32) * 5 / 9 ; } cout << "\t CSCI 193 Program Two, Fall 2012\n"; //Class Format cout << "______________________________________________________________________________\n"; cout << "Fahrenheit Values entered by user and their equivalent Celsius Values:\n"; //Statement of Information cout << "Iterations\t Celsius\t Fahrenheit\n"; //format for output cout << "======================================\n"; cout << iter << "\t\t" << cels << "\t\t" << fahr << endl; system ("PAUSE"); return 0; }

What you're printing out at the end is simply the current values of your iterator, cels and fahr variables. You need to keep track of the variables during each iteration. Most likey using some containers. You would then either manually write out 4 more print statments for each iteration, or create a another for loop that would iterate through and print out the values in each container.
Last edited on
I tried adding a loop similar to the one I already had but I just got the last temperature for all 4 iterations.

 123456789101112131415161718192021222324252627282930313233343536 #include #include #include #include #include using namespace std; int main() { float cels, fahr; char Fahrenheit, Celsius, Iterations; int MIN_NUMBER = 1, MAX_NUMBER = 4; int iter; for (iter = MIN_NUMBER; iter <= MAX_NUMBER; iter++) { cout << "Please enter the Celsius Degree you would like to convert: "; //Looped question cin >> fahr; cels = (fahr - 32) * 5 / 9 ; } cout << "\t CSCI 193 Program Two, Fall 2012\n"; //Class Format cout << "______________________________________________________________________________\n"; cout << "Fahrenheit Values entered by user and their equivalent Celsius Values:\n"; //Statement of Information cout << "Iterations\t Celsius\t Fahrenheit\n"; //format for output cout << "======================================\n"; for (iter = MIN_NUMBER; iter <= MAX_NUMBER; iter++) { cout << iter << "\t\t" << cels << "\t\t" << fahr << endl; } system ("PAUSE"); return 0; }
How about using an array to store the four inputs & outputs?
I think the use of an array is a reasonable suggestion.

I just tested this program, and dutifully entered the Celsius values as prompted:
"Please enter the Celsius Degree you would like to convert: "
But it seems the input should be in Fahrenheit. Maybe the prompt message should guide the user so they know what is required of them?

Back to the array. You could use a pair of arrays, one each for F and C.

Or instead, use a stringstream. Example:
 1234567891011121314151617181920212223242526272829303132 #include #include #include #include #include using namespace std; int main() { stringstream sout; for (int i=0; i<=20; i++) { sout << " " << setw(2) << i; sout << " " << setw(4) << i * i; sout << " " << sqrt((double) i) << endl; } cout << "_____________________________________\n"; cout << " Table of Squares and Square Roots \n"; cout << "\n"; cout << " Number Square Sq. Root \n"; cout << "=====================================\n"; string line; while (getline(sout,line)) { cout << line << endl; } return 0; }
Last edited on
Topic archived. No new replies allowed.