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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <limits>
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;
}


Thanks for any help you can provide.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <limits>
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <sstream>

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.