Void Functions and Error Message not working

Hi I have been spending several hours trying to debug this program but with no luck. I temporarily turned lines 74-76 into documentation and the program compiled when I did that. However, the error message pops up as a response whenever I enter C or F. Can anyone tell me where I'm going 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
#include <iostream>
using namespace std;


// Function prototypes
void Welcome();
char scale();
float temperature();
void celcius (char, float, float);
void fahrenheit(char, float, float);
void answer(float, float);


// Display welcome message
void Welcome()
{
  cout<< "This program performs Celcius/Fahrenheit conversions" << endl;
}


// Ask for initial temperature scale
char scale()
{
  char Temp;
  cout << "What's the initial temperature scale(F or C)?: ";
  cin >> Temp;
  /*if ((Temp != 'F') || (Temp != 'C'))
    cout<< "ERROR" <<endl;*/
}

// Ask for initial temperature in degrees
  float temperature()
{
  float Num;
  cout << "What is the temperature in degrees (enter numbers only)?: ";
  cin >> Num;
  return Num;
}


// Convert from Fahrenheit to Celcius
void celcius(float finalC, float Num, char Temp)
{
  if (Temp == 'F')
    finalC = (Num - 32)/(1.8);
}


// Convert from Celcius to Fahrenheit
void fahrenheit(float finalF, float Num, char Temp)
{
  if (Temp = 'C')
    finalF = (1.8 * Num) + 32;
}


// Display final output message
void answer(float finalF, float finalC)
{
  cout << "Your temperature reading converts as follows:" << endl << endl;
  cout << "Fahrenheit: " << finalF << endl;
  cout << "Celcius: " << finalC << endl;
}


// Main function executes other functions
int main()
{
  char Temp;
  float Num, finalF, finalC;
  Welcome();
  Temp = scale();
  Num =  temperature();
  fahrenheit(Temp);
  /*celcius(Temp, finalC);
  answer(finalF, finalC);*/
  return 0;
}
Last edited on
Hi,

When you have compiler errors, don't comment out the lines that cause them - try to understand & fix them. If not, then post them here in full.


In function 'char scale()': 
29:1: warning: no return statement in function returning non-void [-Wreturn-type] 
In function 'void celcius(float, float, char)': 
42:20: warning: parameter 'finalC' set but not used [-Wunused-but-set-parameter] 
In function 'void fahrenheit(float, float, char)': 
52:17: warning: suggest parentheses around assignment used as truth value [-Wparentheses] 
50:23: warning: parameter 'finalF' set but not used [-Wunused-but-set-parameter] 
In function 'int main()': 
74:18: error: no matching function for call to 'fahrenheit(char&)' 
74:18: note: candidates are: 10:6: note: void fahrenheit(char, float, float) 
10:6: note: candidate expects 3 arguments, 1 provided 
50:6: note: void fahrenheit(float, float, char) 
50:6: note: candidate expects 3 arguments, 1 provided 
75:23: error: no matching function for call to 'celcius(char&, float&)' 
75:23: note: candidates are: 
9:6: note: void celcius(char, float, float) 
9:6: note: candidate expects 3 arguments, 2 provided 
42:6: note: void celcius(float, float, char) 
42:6: note: candidate expects 3 arguments, 2 provided


So now we can go through and fix these up.

line 29, as it says the function must return something.

line 42, should be fixed if you make it a reference:

The calculations you do on lines 45 & 53 aren't visible in main - they are local to the function. Make the parameter a reference:

49
50
51
52
53
54
// Convert from Celcius to Fahrenheit
void fahrenheit(float finalF&, const float Num, const char Temp)
{
  if (Temp = 'C')
    finalF = (1.8 * Num) + 32.0;
}


Prefer double rather than float, the precision of float is easily exceeded, that's why double is the default. I also made some of the parameters const, this means the compiler can enforce the idea that we are not going to change those values inside the function.

Line 52, use == for comparison in a conditional, = is for assignment in an expression.

You have situations where the function call doesn't match the function definition, you must have the same number of arguments with the correct types.

I prefer to put forward declarations of the functions before main, with the function definitions after main. It just means that main is always at the top of the file. I was looking at some code the other day, eventually found main on line 285.

Also avoid declaring more than 1 variable per line of code - it will bite you one day. Also make sure you initialise all your variables. On lie 74 you have a function call with an un-initialised variable.With C++ you can delay declaration until you have a sensible value to assign to it. For example:

1
2
char TempScale = GettempScale(); // renamed these to something more meaningful
double InitialTemperature = GetIntialTemp(); 


Good luck !!
Last edited on
I already did my best going off of the errors the compiler spit out. I hit a dead end with that because I'm still learning C++ and I have a very poor understanding of it. What exactly do you mean when you say the function call doesn't match the function definition? Here are the modifications I made to the program. I am getting even more errors now than I did before.

#include <iostream>
using namespace std;


// Function prototypes
void Welcome();
char scale();
float temperature();
void celcius (char, float, float);
void fahrenheit(char, float, float);
void answer(float, float);


// Display welcome message
void Welcome()
{
cout<< "This program performs Celcius/Fahrenheit conversions" << endl;
}


// Ask for initial temperature scale
char scale()
{
cout << "What's the initial temperature scale(F or C)?: ";
cin >> Temp;
/*if ((Temp != 'F') || (Temp != 'C'))
cout<< "ERROR" <<endl;*/
}

// Ask for initial temperature in degrees
float temperature()
{
float Num;
cout << "What is the temperature in degrees (enter numbers only)?: ";
cin >> Num;
}


// Convert from Fahrenheit to Celcius
void celcius(float finalC&, const float Num, const char Temp)
{
if (Temp == 'F')
finalC = (Num - 32)/(1.8);
}


// Convert from Celcius to Fahrenheit
void fahrenheit(float finalF&, const float Num, const char Temp)
{
if (Temp == 'C')
finalF = (1.8 * Num) + 32;
}


// Display final output message
void answer(float finalF, float finalC)
{
cout << "Your temperature reading converts as follows:" << endl << endl;
cout << "Fahrenheit: " << finalF << endl;
cout << "Celcius: " << finalC << endl;
}


// Main function executes other functions
int main()
{
char Temp;
float Num, finalF, finalC;
Welcome();
Temp = scale();
Num = temperature();
fahrenheit(Temp);
celcius(Temp, finalC);
answer(finalF, finalC);*/
return 0;
}
With the code tags: Edit your post, select the code, then press the <> button on the format menu.

What exactly do you mean when you say the function call doesn't match the function definition?


The rest of that sentence:

... , you must have the same number of arguments with the correct types.


The function call:

fahrenheit(Temp);

The function declaration:
void fahrenheit(char, float, float);

The function definition:

1
2
3
4
5
void fahrenheit(float finalF&, const float Num, const char Temp)
{
if (Temp == 'C')
finalF = (1.8 * Num) + 32;
}


All 3 of those must match with the number of arguments/parameters and the types of those arguments/parameters and their order. Change the declaration to be the same as the definition, and call the function with 2 floats and a char.
Topic archived. No new replies allowed.