Creating a temperature converter using two void functions

Jun 8, 2017 at 7:44pm
/*Hi, everyone. I am having trouble making a converter that allows the user to input a number that then translates that number into either Celsius or Fahrenheit. So if I enter 100 and choose "F" it should view 100 as Celsius and convert it to Fahrenheit. I was able to get Fahrenheit to Celsius to work, but not Celsius to Fahrenheit.*/

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
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <iomanip>
using namespace std;

//function prototypes
void calcCelsius(double tempF, double &tempC);
void calcFahrenheit(double tempC, double &tempF);

int main() {

	double fahrenheit = 0.0;
	double celsius = 0.0;
	double tempTotal = 0.0;
	char temp = ' ';

	cout << "Enter temperature: ";
	cin >> fahrenheit;
	cout << "Do you want to convert to fahrenheit or celsius?" << endl;
	cin >> temp;
	
	calcCelsius(fahrenheit, celsius);
	calcFahrenheit(celsius, fahrenheit);
	

	if (toupper(temp) == 'C') {
		cout << fixed << setprecision(0);
		cout << "Celsius temperature: " << celsius << endl;
	}
	else if (toupper(temp) == 'F') {
		
		cout << fixed << setprecision(0);
		cout << "Fahrenheit temperature: " << fahrenheit << endl;
	}

	


	return 0;
} //end of main function

//function definitions
void calcCelsius(double tempF, double &tempC) {
	tempC = 5.0 / 9.0 * (tempF - 32.0);
}

void calcFahrenheit(double tempC, double &tempF) {
	tempF = (1.8 * tempC) + 32.0;
}
Last edited on Jun 8, 2017 at 7:59pm
Jun 8, 2017 at 7:55pm
Please, always use codetags (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
Jun 8, 2017 at 7:59pm
Thanks, chicofeo. First time poster here!
Jun 8, 2017 at 8:10pm
Nevermind, guys. I actually figured it out. Instead of using Fahrenheit as my input, I created a generic temperature input and plugged it in to the void functions. Here is my updated code. If there is any way for me to make my code more efficient or easier to read please let me know. :)

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
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <iomanip>
using namespace std;

//function prototypes
void calcCelsius(double tempG, double tempF, double &tempC);
void calcFahrenheit(double tempG, double tempC, double &tempF);

int main() {

	double fahrenheit = 0.0;
	double celsius = 0.0;
	double tempGen = 0.0;
	char temp = ' ';

	cout << "Enter temperature: ";
	cin >> tempGen;
	cout << "Do you want to convert to fahrenheit or celsius?" << endl;
	cin >> temp;
	
	calcCelsius(tempGen, fahrenheit, celsius);
	calcFahrenheit(tempGen, celsius, fahrenheit);
	

	if (toupper(temp) == 'C') {
		cout << fixed << setprecision(0);
		cout << "Celsius temperature: " << celsius << endl;
	}
	else if (toupper(temp) == 'F') {
		
		cout << fixed << setprecision(0);
		cout << "Fahrenheit temperature: " << fahrenheit << endl;
	}

	


	return 0;
} //end of main function

//function definitions
void calcCelsius(double tempG, double tempF, double &tempC) {
	tempC = 5.0 / 9.0 * (tempG - 32.0);
}

void calcFahrenheit(double tempG, double tempC, double &tempF) {
	tempF = (1.8 * tempG) + 32.0;
}
// end of function definitions 
Jun 8, 2017 at 8:13pm
Both conversion functions are called right now, even though the person just wants to do one of them. The function call could be part of the if {} block of code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    double fahrenheit = 0.0;
    double celsius = 0.0;
    double input = 0.0;
    char temp = ' ';
    
    cout << "Enter temperature: ";
    cin >> input;
    cout << "Do you want to convert to fahrenheit or celsius?" << endl;
    cin >> temp;
    
    if (toupper(temp) == 'C') {
    calcCelsius(input, celsius);
    cout << fixed << setprecision(0);
    cout << "Celsius temperature: " << celsius << endl;
    }
    else if (toupper(temp) == 'F') {
    calcFahrenheit(input, fahrenheit);
    cout << fixed << setprecision(0);
    cout << "Fahrenheit temperature: " << fahrenheit << endl;
    }
Last edited on Jun 8, 2017 at 8:14pm
Jun 8, 2017 at 8:16pm
Thanks a lot for replying so quickly, wildblue. I just discovered this site and it is really awesome. I have only been programming in C++ for a little while but I really like it. I actually figured this problem out a few minutes ago, essentially doing what you just did. But thanks anyway for responding so fast!
Topic archived. No new replies allowed.