Complier Error on void function?

Can't seem to determine whats wrong with my void function. I keep getting a complier error. Anyone tell me whats wrong with this 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
  void areatriangle (int base, int height, int &result)
{
	result=(base*height)/2;
	return;
}

int _tmain(int argc, _TCHAR* argv[])
{
	cout<<"Area of Triangle: ";
	//Declare variables for base height and answer

	int base1, int base2, int answer;

	base1=2;
	base2=3;

	areatriangle(base1, base2, answer);

	cout<<"\nThe Area of triangle is: " <<answer;
	_getch;
	return 0;
}

The neat thing about compiler errors are they say what is wrong. Could you copy/paste it? Also they are functions not void functions. Void is the return type telling the compiler there is nothing being returned. Which also means you should remove the return on line 4 it is doing absolutely nothing.

*on a side note you are also trying to do integer division on line 3.
Last edited on
closed account (18hRX9L8)
Please post the full error (Line, CharAt, e.What).

EDIT:
Okay I see... You need to change the ampersand on line 1 to a * because you need a pointer. Then you need to switch result= on line 2 to *result= (because your pointer is really a normal int). Also, you need to dereference answer on line 17 to call it in as an int pointer. Fixed code shown below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void areatriangle (int base, int height, int *result) {
	*result=(base*height)/2;
}

int _tmain(int argc, _TCHAR* argv[]) {
	cout<<"Area of Triangle: ";
	//Declare variables for base height and answer

	int base1, int base2, int answer;

	base1=2;
	base2=3;

	areatriangle(base1, base2, &answer);

	cout<<"\nThe Area of triangle is: " <<answer;
	_getch;
	return 0;
}
Last edited on
This is what I get on my console.

>c:\users\pc\desktop\c++ programming\void function area\void function area\void function area.cpp(21): error C2062: type 'int' unexpected
1>c:\users\pc\desktop\c++ programming\void function area\void function area\void function area.cpp(24): error C2065: 'base2' : undeclared identifier
1>c:\users\pc\desktop\c++ programming\void function area\void function area\void function area.cpp(26): error C2065: 'base2' : undeclared identifier
1>c:\users\pc\desktop\c++ programming\void function area\void function area\void function area.cpp(26): error C2065: 'answer' : undeclared identifier
1>c:\users\pc\desktop\c++ programming\void function area\void function area\void function area.cpp(28): error C2065: 'answer' : undeclared identifier
1>c:\users\pc\desktop\c++ programming\void function area\void function area\void function area.cpp(29): warning C4551: function call missing argument list
closed account (18hRX9L8)
See my edited post.
Wait, we haven't covered pointers yet in class though. Is there not another way usandfriends to fix this issue?
Wait, we haven't covered pointers yet in class though. Is there not another way usandfriends to fix this issue? Also, when I click on debut, I get a message from this popupbrowser saying "the system cannot find the file specified
He ampersand is for reference. It is better to pass by reference than pointer for this case usandfriends.

Also those errors don't look remotely like the code you provided are you sure you rebuilt it before running?
I did that right now and still getting the same error.
Ok, I don't want the conflict was here, but I decided to restart it and open a new project. Made the same exact code and this time it worked O_o
Oh lol I wasn't pay attention I thought you had semi colons on line 9 but you have commas. When you do a list like that using the comma operator you only put the type for the left most variable.

so it should be

int base1 , base2 , answer;

Other than that and the extra return statement that you should remove the code looks good.

On a side note you can initialize when you declare a variable:

 
int base1 = 2 , base2 = 3 , answer;
Thanks for the side note, especially listing same types of variables like that :D :) I will keep that in mind :)
closed account (18hRX9L8)
Oh woops! Glad you found the problem
giblit
and
fahmankhan75
.
Topic archived. No new replies allowed.