Need help converting Celcius to Fahrenheit - Homework

Hello. I am currently taking an introduction to C++ programming class at my college, and we have to put together the most basic 2-Choice Temperature Conversion program.

The program works great for choice 1, "Fahrenheit to Celcius Converter". However, it doesn't work the other way around for choice 2 "Celcius to Fahrenheit".

Here is my 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
33
34
35
36
37
38
39
#include <iostream>

using namespace std;

int main() {

	// Fahrenheit to Celcius
	// or
	// Celcius to Fahrenheit Converter
	double fahrenheit;
	double celcius;
	int choice;

	cout << "1. Fahrenheit to Celcius Converter" << endl;
	cout << "2. Celcius to Fahrenheit Converter" << endl;
	cout << "Enter choice: ";
	cin >> choice;

	if (choice == 1) {
		cout << "1. Fahrenheit to Celcius Converter" << endl;
		cout << "Enter temperature in Fahrenheit: ";
		cin >> fahrenheit;
		fahrenheit = (5.0 / 9.0)*(fahrenheit - 32.0);
		cout << "Converted Celcius: " << fahrenheit << endl;
	}

	else if (choice == 2) {
		cout << "1. Celcius to Fahrenheit Converter" << endl;
		cout << "Enter temperature in Celcius: ";
		cin >> celcius;
		celcius = (9.0 / 5.0)*(fahrenheit + 32.0);
		cout << "Converted Fahrenheit: " << celcius << endl;
	}
	else {
		cout << "Invalid choice" << endl;
	}
	system("pause");
	return 0;
}


I just can't figure out what I am doing wrong. It is required we use "Doubles" for Fahrenheit and Celcius. I have seen people use "Floats", but my professor hasn't gone over that in full. I even tried replacing (9.0 / 5.0) with just (1.8), but I am still getting incorrect calculation results. What can I do to adjust this?
Celcius to Fahrenheit uses an incorrect calculation (line 31)
 
    celsius = (9.0 / 5.0)*(fahrenheit + 32.0);

it should be
 
    fahrenheit = 32 + (9.0 / 5.0)*celsius;






It's messing up where your formulas are. Make new doubles such as "double convertedF"
or "double convertedC" and rewrite the formulas:

convertedC = (9.0 / 5.0)*(fahrenheit + 32.0);

and the same for the fahrenheit formula.
Hi. I changed the formula as suggested, but it still doesn't seem to work; it's inputting and outputting the same number for choice 2.

For example,

"Enter temperature in Celcius: 60
Converted Fahrenheit: 60"

"Enter temperature in Celcius: 82.22
Converted Fahrenheit: 82.22"

Here's how I typed the code:
1
2
3
4
5
6
7
else if (choice == 2) {
		cout << "1. Celcius to Fahrenheit Converter" << endl;
		cout << "Enter temperature in Celcius: ";
		cin >> celcius;
		fahrenheit = 32 + (9.0 / 5.0)*celcius;
		cout << "Converted Fahrenheit: " << celcius << endl;
}
Last edited on
The cout statements use the wrong variables.

Line 24
 
    cout << "Converted Celcius: " << fahrenheit << endl;

should be:
 
    cout << "Converted Celsius:    " << celsius << endl;


and line 32
 
    cout << "Converted Fahrenheit: " << celcius << endl;

should be
 
    cout << "Converted Fahrenheit: " << fahrenheit << endl;


Using meaningful variable names such as celsius and fahrenheit is much better than using anonymous letters such as p and q. But it seems you still got them in the wrong context several times.
Thank you guys! I got it to work!

However, Chervil, I had to leave choice 1 as is and your suggestion for Choice 2 fixed my problem!

It's weird how both cout statements require Fahrenheit as their variables, but I ran it in my compiler and got the correct results!

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
37
38
39
#include <iostream>

using namespace std;

int main() {

	// Fahrenheit to Celcius
	// or
	// Celcius to Fahrenheit Converter
	double fahrenheit;
	double celcius;
	int choice;

	cout << "1. Fahrenheit to Celcius Converter" << endl;
	cout << "2. Celcius to Fahrenheit Converter" << endl;
	cout << "Enter choice: ";
	cin >> choice;

	if (choice == 1) {
		cout << "1. Fahrenheit to Celcius Converter" << endl;
		cout << "Enter temperature in Fahrenheit: ";
		cin >> fahrenheit;
		fahrenheit = (5.0 / 9.0)*(fahrenheit - 32.0);
		cout << "Converted Celcius: " << fahrenheit << endl;
	}

	else if (choice == 2) {
		cout << "1. Celcius to Fahrenheit Converter" << endl;
		cout << "Enter temperature in Celcius: ";
		cin >> celcius;
		fahrenheit = 32 + (9.0 / 5.0)*celcius;
		cout << "Converted Fahrenheit: " << fahrenheit << endl;
	}
	else {
		cout << "Invalid choice" << endl;
	}
	system("pause");
	return 0;
}


It's weird how both cout statements require Fahrenheit as their variables,

That's what I was trying to say:
chervil wrote:
Using meaningful variable names such as celsius and fahrenheit is much better than using anonymous letters such as p and q. But it seems you still got them in the wrong context several times.


19
20
21
22
23
24
25
    if (choice == 1) {
        cout << "1. Fahrenheit to Celcius Converter" << endl;
        cout << "Enter temperature in Fahrenheit: ";
        cin >> fahrenheit;
        fahrenheit = (5.0 / 9.0)*(fahrenheit - 32.0);
        cout << "Converted Celcius: " << fahrenheit << endl;
    }

Line 23/24 are incorrect. It doesn't make sense to say that fahrenheit = (some conversion factor) fahrenheit.

It should be:
19
20
21
22
23
24
25
    if (choice == 1) {
        cout << "1. Fahrenheit to Celcius Converter" << endl;
        cout << "Enter temperature in Fahrenheit: ";
        cin >> fahrenheit;
        celcius= (5.0 / 9.0)*(fahrenheit - 32.0);
        cout << "Converted Celcius: " << celcius<< endl;
    }


Also, I deliberately used the same spellings as the original code here - but note that Celsius - after the Swedish astronomer Anders Celsius is spelled with just one 'c' and two 's'es.

Last edited on
Topic archived. No new replies allowed.