Trouble passing by reference

The following program is supposed to use passing by reference and return to order to convert Celsius into Fahrenheit. I know there are more efficient ways to do so, but I'm doing this for a class and we are required to follow this format in order to learn the above.

Can you spot the error (please)?
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;

float celsius_to_fahrenheit(float celsius);
float getcelsius(float &celsius);

int main()
 {
  float fahrenheit;
  float celsius;
  
  celsius = getcelsius(celsius);

  fahrenheit = celsius_to_fahrenheit(celsius);

  cout << celsius << " C = " << fahrenheit << " Fn";


  cin.ignore(80, 'n');
  cin.get();
  
  return 0;
 } 

float getcelsius(float &celsius)
 {
      cout << "Enter celsius amount."<<endl;
      cin >> celsius;
 }

float celsius_to_fahrenheit(float celsius)
 {
  return(celsius * (9.0/5.0) + 32.0);
 }
 
 


You're not actually passing a pointer to your function when you use them on either line 13 or line 15.

EDIT: Also your function declarations aren't written to except pointers either.
Last edited on
@ Computergeek01: He says he's trying to pass by reference, which he is almost doing properly. He didn't say anything about passing pointers.

@OP: You're assigning the return value of getcelsius() to celsius, but you don't return anything in that function. You need to declare that function as void and not try to assign anything to celsius. The variable will change within the function.
Last edited on
Ah, good catch. My bad.
Thank you Computergeek01! I understand now where the error is and how to fix it.

Program is now working properly.

Thanks again.
Topic archived. No new replies allowed.