Error C2440, Cannot convert from void to float

I'am just learning c++ (which is why Im in the beginner's section =P) so please pardon any nonconventional coding style I might have. I'm attempting to write a program that passes data by reference and I keep getting an error that disallows me to "convert from void to float" and I've no clue how to fix it. I found a similar post on the subject on this forum, but the user asking for assistance was using an array and that is over my head, so the help he received was non-transferable to me. =[

Any help would be much appreciated. Below is the code and errors. Im using Visual Studio 2008 Professional, if that matters any.
Thank you!

Oops I forgot to mention the error code in Visual Studio is pointing to lines 23 thru 26. ><

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
#include <iostream>
#include <cmath>
using namespace std;

//functin prototypes
void getData(float & num1, float & num2);
void doTheMath(float num1, float num2, float & sumAdd, float & sumSub, float & multRes, float & divRes);
void displayResults(float num1, float num2, float & sumAdd, float & sumSub, float & multRes, float & divRes);

//==== main ==================================================================
//
//============================================================================ 
int main ()
{
	float num1=0;
	float num2=0;
	float sumAdd=0;
	float sumSub=0;
	float multRes=0;
	float divRes=0;

	getData(num1, num2);
	sumAdd=doTheMath(num1, num2, sumAdd, sumSub, multRes, divRes);
	sumSub=doTheMath(num1, num2, sumAdd, sumSub, multRes, divRes);
	multRes=doTheMath(num1, num2, sumAdd, sumSub, multRes, divRes);
	divRes=doTheMath(num1, num2, sumAdd, sumSub, multRes, divRes);
	displayResults(num1, num2, sumAdd, sumSub, multRes, divRes);
	
	return 0;
} // end of "main"
//============================================================================

//==== getData ===============================================================
//
//============================================================================
void getData(float & num1, float & num2)
{
	cout<<"Please enter two integer values: /n";
	cin>>num1;
	cin>>num2;
} // end of "getData"
//=============================================================================

//==== doTheMath ==============================================================
//
//=============================================================================
void doTheMath(float num1, float num2, float & sumAdd, float & sumSub, float & multRes, float & divRes)
{
	sumAdd=num1+num2;
	sumSub=num1-num2;
	multRes=num1*num2;
	divRes=num1/num2;
} // end of "doTheMath"
//=============================================================================

//==== displayResults =========================================================
//
//=============================================================================
void displayResults(float num1, float num2, float & sumAdd, float & sumSub, float & multRes, float & divRes)
{
	if (num2==0){
	cout<<"The sum of "<<num1<<" and "<<num2<<" is "<<sumAdd<<".\n";
	cout<<"The difference, ("<<num1<<" - "<<num2<<") is "<<sumSub<<".\n";
	cout<<"The product of "<<num1<<" and "<<num2<<" is "<<multRes<<".\n";
	cout<<"Cannot divide an integer by zero.";
	cout<<endl;}

	else{
	cout<<"The sum of "<<num1<<" and "<<num2<<" is "<<sumAdd<<".\n";
	cout<<"The difference, ("<<num1<<" - "<<num2<<") is "<<sumSub<<".\n";
	cout<<"The product of "<<num1<<" and "<<num2<<" is "<<multRes<<".\n";
	cout<<num1<<" divided by "<<num2<<" is "<<divRes<<".\n";
	cout<<endl;
	}
} // end of "displayResults"
//======================================================================== 

Error 1 error C2440: '=' : cannot convert from 'void' to 'float'

^ That is the error code I keep getting.
Last edited on
I tried changing the function type from void to float, but then the function has to return a value. My understanding, limited by lack of experience, is that a function can only return one value and I need to return multiple, which is why I was trying to "pass by reference" (I think that is the proper term, please correct me if Im wrong).

Is there a way to pass by reference the data calculated in the doTheMath function to displayResults function?
Because that would be wonderful =P

Thank you!
Last edited on
Since you pass your arguments as reference you don't need the sumAdd= part on lines 23-26, remove that.
You actually need to call doTheMath only once
That worked! Thank you so much for your help.
Topic archived. No new replies allowed.