Passing variables by reference problem

I am having a problem understanding and implementing pass by reference with my function showDegreesK.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  #include "stdafx.h"
#include<iostream>

using namespace std;
using namespace System;




double FtC(double t);
double CtF(double t);
void showDegreesK(double& t, double& s);
char ansk;

int main()
{

	double temp, answer,answer1;
	int ans;



	cout << "Welcome to my temperature converter." << endl << endl;

	do
	{
		cout << "Would you like to see the temperature in Kelvin? y for yes, any other key for no." << endl;
		cin >> ansk;


		cout << "Which conversion type would you like to do:" << endl;
		cout << "1. F to C \n2. C to F \n0. End Program" << endl;
		cin >> ans;


		if (ans == 1)
		{
			cout << "Please input your desired temperature in F to be converted:" << endl;
			cin >> temp;
			answer = FtC(temp);

			cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
			if (ansk == 'y')
			{
				showDegreesK(answer,answer1);
				cout << "The temperature in Kelvin is " << answer << endl;
			}
			else
			{
				cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
			}



		}

		else if (ans == 2)
		{
			cout << "Please input your desired temperature in C to be converted: " << endl;
			cin >> temp;
			answer = CtF(temp);
			cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
			if (ansk == 'y')

			{
				showDegreesK(answer,answer1);
				cout << "The temperature in Kelvin is " << answer1 << endl;
			}

			else
			{
				cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
			}


		}
		else
		{
			
			break;
		}

	} while (ans != 0);

	cout << "Thank you for using my temperature converting program!" << endl;
	system("Pause");
	return 0;
}
double FtC(double t)
{
	double a;

	a = (5.0 / 9.0)*(t - 32.0);
	return a;
}
double CtF(double t)
{
	double a;

	a = ((9.0 / 5.0*t)) + 32;
	return a;
}

void showDegreesK(double& t, double& s)
{
	char ansk;
	double answer;
	double answer1;

	if (ansk == 1)
		 answer = t + 273.15;
	else  
		answer1 = (t + 459.67)*(5.0 / 9.0);
	
	if (answer||answer1<0)
		cout << "That temperature is currently impossible" << endl;
	

}
As it stands now, the function does not modify the values of the parameters, so there is currently no reason to pass them by reference. Could you provide more information about what your assignment requires you to do?
Sorry, I am obvously new to programming. I am supposed to create a program that converts the given temperature (C or F) into the other. Also, it is supposed to include the option of showing/having it converted into Kelvin as well. I should also have a statement to show if the temperature is possible (i.e. negative kelvin). I modified it a little bit, but I am still unsure of pass by reference or passing two variables back to the main.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
 #include "stdafx.h"
#include<iostream>

using namespace std;
using namespace System;




double FtC(double t);
double CtF(double t);
void showDegreesK(double& t, double& a, double& b);
char ansk;

int main()
{

	double temp, answer,temp1,temp2;
	int ans;



	cout << "Welcome to my temperature converter." << endl << endl;

	do
	{
		cout << "Would you like to see the temperature in Kelvin? y for yes, any other key for no." << endl;
		cin >> ansk;


		cout << "Which conversion type would you like to do:" << endl;
		cout << "1. F to C \n2. C to F \n0. End Program" << endl;
		cin >> ans;


		if (ans == 1)
		{
			cout << "Please input your desired temperature in F to be converted:" << endl;
			cin >> temp;
			answer = FtC(temp);

			cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
			if (ansk == 'y')
			{
				showDegreesK(temp,temp1,temp2);
				cout << "The temperature in Kelvin is " << temp1 << endl;
			}
			else
			{
				cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
			}



		}

		else if (ans == 2)
		{
			cout << "Please input your desired temperature in C to be converted: " << endl;
			cin >> temp;
			answer = CtF(temp);
			cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
			if (ansk == 'y')

			{
				showDegreesK(temp,temp1,temp2);
				cout << "The temperature in Kelvin is " << temp2 << endl;
			}

			else
			{
				cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
			}


		}
		else
		{
			
			break;
		}

	} while (ans != 0);

	cout << "Thank you for using my temperature converting program!" << endl;
	system("Pause");
	return 0;
}
double FtC(double t)
{
	double a;

	a = (5.0 / 9.0)*(t - 32.0);
	return a;
}
double CtF(double t)
{
	double a;

	a = ((9.0 / 5.0*t)) + 32;
	return a;
}

void showDegreesK(double& t, double& a, double& b)
{
	char ansk;
	

	if (ansk == 1)
		 a = t + 273.15;
	else  
		b = (t + 459.67)*(5.0 / 9.0);
	
	if (a||b<0)
		cout << "That temperature is currently impossible" << endl;
	

}
I meant, why are you trying to use pass-by-reference here? Does your assignment require it? I was hoping for more details from the assignment than just "you have to use pass-by-reference", because that's a very generic requirement.
No it does not require it. I just don't know how else to pass one variable to a function and have that function pass two back to the main. ( One to showDegreesK from main, two back to main from showDegreesK)
Ah, so you have free reign here, I understand now.

I recommend having one function per conversion - don't try to bundle two or more conversions into a single function. E.g. have one celsius to fahrenheit function, one fahrenheit to celsius function, one celsius to kelvin function, one kelvin to celsius function, and you're done. With those functions you can convert from any scale to any other scale, no need to pass by reference. Each function would take one parameter by value and return one value.
Ok, I can do that, that is a whole lot easier for me. In that assignment it just sounds like the professor is implying that there should be one "showDegreesK" function, as he refers to it as "the" function or "it". It is an online class so it is very vague.

Would you happen to have a simple pass by reference program handy or know where I could find one online? The one in my book is not so very good at putting/showing things in laymans terms.
I think the showDegreesK function should just take either celsius or fahrenheit (you pick one and stick to it in your code) and call the conversion function to convert it to kelvin and print the result.

Here's some useful info on passing by reference:
http://www.cplusplus.com/doc/tutorial/functions/#reference
Last edited on
Thank you. I understand what i have to do, I just don't know how to code it. How does the showDegreesK function know whether it is C or F, and how does it switch between the two?

Kind of side note, could you tell me specifically (If it isn't too much) what is wrong with this code? Or is that too much to ask?

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "stdafx.h"
#include<iostream>

using namespace std;
using namespace System;




double FtC(double t);
double CtF(double t);
double showDegreesK(double t);
char ansk;

int main()
{

	double temp, answer;
	int ans;



	cout << "Welcome to my temperature converter." << endl << endl;

	do
	{
		cout << "Would you like to see the temperature in Kelvin? y for yes, any other key for no." << endl;
		cin >> ansk;


		cout << "Which conversion type would you like to do:" << endl;
		cout << "1. F to C \n2. C to F \n0. End Program" << endl;
		cin >> ans;


		if (ans == 1)
		{
			cout << "Please input your desired temperature in F to be converted:" << endl;
			cin >> temp;
			answer = FtC(temp);

			cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
			if (ansk == 'y')
			{
				answer = showDegreesK(temp);
				cout << "The temperature in Kelvin is " << answer << endl;
			}
			else
			{
				cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
			}



		}

		else if (ans == 2)
		{
			cout << "Please input your desired temperature in C to be converted: " << endl;
			cin >> temp;
			answer = CtF(temp);
			cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
			if (ansk == 'y')

			{
				answer = showDegreesK(temp);
				cout << "The temperature in Kelvin is " << answer << endl;
			}

			else
			{
				cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
			}


		}
		else
		{
			cout << "You entered an invalid key." << endl;
			break;
		}

	} while (ans != 0);

	cout << "Thank you for using my temperature converting program!" << endl;
	system("Pause");
	return 0;
}
double FtC(double t)
{
	double a;

	a = (5.0 / 9.0)*(t - 32.0);
	return a;
}
double CtF(double t)
{
	double a;

	a = ((9.0 / 5.0*t)) + 32;
	return a;
}

double showDegreesK(double t)
{
	char ansk;
	double a;

	if (ansk == 1)
		a = t + 273.15;
	else
		a = (t + 459.67)*(5.0 / 9.0);
	if (a < 0 )
		cout << "That temperature is currently impossible" << endl;

	return a;

}


Jon15 wrote:
How does the showDegreesK function know whether it is C or F, and how does it switch between the two?
As I said, you need to pick one and always make sure your code gives it the temperature scale it expects. I recommend picking celsius, which means that the showDegreesK only accepts celsius and you have to remember to only give it celsius.
Jon15 wrote:
Kind of side note, could you tell me specifically (If it isn't too much) what is wrong with this code? Or is that too much to ask?
showDegreesK should return void, for one thing. You don't need ansk at all.

By the way, you can simplify these functions:
89
90
91
92
93
94
95
96
97
98
99
100
101
102
double FtC(double t)
{
	double a;

	a = (5.0 / 9.0)*(t - 32.0);
	return a;
}
double CtF(double t)
{
	double a;

	a = ((9.0 / 5.0*t)) + 32;
	return a;
}
It would be simpler to write them like this:
89
90
91
92
93
94
95
96
double FtC(double t)
{
	return (5.0 / 9.0)*(t - 32.0);
}
double CtF(double t)
{
	return ((9.0 / 5.0*t)) + 32;
}
I can't seem to get my main to reference the variables in my showDegreesK function, am i calling it wrong?

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "stdafx.h"
#include<iostream>

using namespace std;
using namespace System;




double FtC(double t);
double CtF(double t);
void showDegreesK (double& x, double& y);

double temp, answer;
int ans;





int main()
{

	char answer1;
	double a, b;



	cout << "Welcome to my temperature converter." << endl << endl;

	do
	{
		cout << "Would you like to see the temperature in Kelvin? y for yes, n for no" << endl;
		cin >> answer1;


		cout << "Which conversion type would you like to do:" << endl;
		cout << "1. F to C \n2. C to F \n0. End Program" << endl;
		cin >> ans;


		if (ans == 1)
		{
			cout << "Please input your desired temperature in F to be converted:" << endl;
			cin >> temp;
			answer = FtC(temp);

		
			if (answer1 == 'y')
			{	
				cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
				showDegreesK(a,b);
				cout << "The temperature in Kelvin is " << answer << endl; 
				
			}
			
			else
			{
				
				cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
			}



		}

		else if (ans == 2)
		{
			cout << "Please input your desired temperature in C to be converted: " << endl;
			cin >> temp;
			answer = CtF(temp);
			
			if (answer1 == 'y')

			{
				cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
				showDegreesK(a,b);
				cout << "The temperature in Kelvin is " << answer << endl;
			}

			else
			{
				cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
			}


		}
		else
		{
			cout << "You entered an invalid key." << endl;
			break;
		}

	} while (ans != 0);

	cout << "Thank you for using my temperature converting program!" << endl;
	system("Pause");
	return 0;
}
double FtC(double t)
{
	double a;

	a = (5.0 / 9.0)*(t - 32.0);
	return a;
}
double CtF(double t)
{
	double a;

	a = ((9.0 / 5.0*t)) + 32;
	return a;
}

void showDegreesK(double& x,double& y)
{
	
	double answer;
	int ans;

	if (ans = 1)
	{
		x = answer + 273.15;
		
	}

	else if (ans = 2)
		
		y = (answer + 459.67)*(5.0 / 9.0);

	if (y<0 || x<0)
		cout << "That temperature is currently impossible" << endl;

}
Jon15 wrote:
I can't seem to get my main to reference the variables in my showDegreesK function, am i calling it wrong?
Why are you trying to do that? Your showDegreesK should be defined as void showDegreesK(double celsius)
Hi,

Some other stuff to help you out a bit:

Have you learnt about switch yet ? IMO it would be better than the if statements you have. Each case in the switch could call a function to do that task (you should still do this with your code as it is), and the default: case catches bad input. One can have an exit / quit option, and the whole thing can be in a loop to run it all again.

Try to use good descriptive meaningful variable and function names. Variables like x or y in this context are not good. Consider names like CelsiusTemp instead. Don't be afraid to have reasonably longer variable & function names - I would rather have that, than those which could be cryptic.

In a similar way, names like answer, ans , a are also not good variable names in this context. Single char variable names have their place, such as in loop counters etc. Even then I often make them a small word, such as row or col - if that is what they are.

If the variables are to do with a mathematical formula, then single char names are OK - because they are easily recognised. For example y = r * sin(theta);. Even then, some might argue that this is more appropriate: YOrdinate = Radius * sin(ThetaCartesian);

The function showDegreesK does not show anything, how about CalcDegreesKelvin or calcDegreesKelvin, or some other variation on that?

So, if one has good variable & function names and comments are used to describe why & what & valid ranges of values, then the code should read like a story of what is going on. It is really good when someone else who doesn't know anything much about what you are doing, can figure out what your code is all about.

Try to avoid global variables, your last code has them - and they lead to trouble.

Always initialise your variables to something - preferably at declaration. This is a very common cause of problems.

Hope all goes well, and good luck :+)



I know about the switch statement (kind of) , but we haven't learnt it in class yet and I don't think I know enough to use it, really. Thanks for the input about naming variables, I always get mine mixed up while coding. Why is my showDegreesK function not changing anything on my code? I think I am calling it correctly, but it isn't performing what i want it to do. Not the only problem, but the one I am stuck on.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "stdafx.h"
#include<iostream>

using namespace std;
using namespace System;



double temp;
double FtC(double t);
double CtF(double t);
double showDegreesK (double t);






int main()
{

	char answerkelvin;
	double answer;
	int ans;


	cout << "Welcome to my temperature converter." << endl << endl;

	do
	{
		cout << "Would you like to see the temperature in Kelvin? y for yes, n for no" << endl;
		cin >> answerkelvin;


		cout << "Which conversion type would you like to do:" << endl;
		cout << "1. F to C \n2. C to F \n0. End Program" << endl;
		cin >> ans;


		if (ans == 1)
		{
			cout << "Please input your desired temperature in F to be converted:" << endl;
			cin >> temp;
			answer = FtC(temp);

		
			if (answerkelvin == 'y')
			{	
				cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
				double showDegreesK(answer);
				cout << "The temperature in Kelvin is " << answer << endl; 
				
			}
			
			else 
			{
				
				cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
			}



		}

		else if (ans == 2)
		{
			cout << "Please input your desired temperature in C to be converted: " << endl;
			cin >> temp;
			answer = CtF(temp);
			
			if (answerkelvin == 'y')

			{
				cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
				double showDegreesK(answer);
				cout << "The temperature in Kelvin is " << answer << endl;
			}
			
			else 
			{
				cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
			}

			
		}
		else if (ans==0)
		{
			
			break;
		}

		else
		{
			cout << "You entered an invalid key." << endl;
		}

	} while (ans != 0);

	cout << "Thank you for using my temperature converting program!" << endl;
	system("Pause");
	return 0;
}
double FtC(double t)
{
	double a;

	a = (5.0 / 9.0)*(t - 32.0);
	return a;
}
double CtF(double t)
{
	double a;

	a = ((9.0 / 5.0*t)) + 32;
	return a;
}

double showDegreesK(double t)
{
	
	double kelvin;
	int ans;

	if (ans = 1)
	{
		kelvin = temp + 273.15;
		
	}

	else if (ans = 2)
	{
		kelvin = (temp + 459.67)*(5.0 / 9.0);
	}
	if (kelvin<0)
		cout << "That temperature is currently impossible" << endl;

	return kelvin;

}
i fixed, it, stupid mistake
Thanks for the help everyone
Topic archived. No new replies allowed.