Problems with if / else statement

Hello,

maybe someone can help me with my code.

This is my code:
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
#include <iostream>
#include <stdlib.h> // wegen abs(F)
using namespace std;
struct GEP // Gegenüberliegender Eckpunkt
	{
		int Punkt1;
		int Punkt2;
		int Punkt3;
	};
int VQ (const GEP& A,const GEP& B) /*
Für die Berechnung eines Quaders ist es ausreichend, wenn man von
einem beliebigen Eckpunt als zweiten Punkt den am weitest
entfernten Eckpunkt wählt und die Differenz der Koordinaten bildet.
Folglich kann man das Volumen dadurch bestimmen, dass man die
Grundfläche mit der Höhe multipliziert.
*/
	{
		int C, D, E, F;		
		C = A.Punkt1 - B.Punkt1;
		D = A.Punkt2 - B.Punkt2;
		E = A.Punkt3 - B.Punkt3;
		F = C * D * E;
		return abs(F);
	}
int GV (int m, int n)
	{
		
		int a, b;
		if ( a < b )
			{
				return 1;
			}
		else if ( a == b )
			{
				return 0;
			}
		else
			{
				return -1;
			}
	}
int main()
	{
		int x, y;		
		GEP P1={-1, -1, -1};
		GEP P2={-3, -3, 3};
		x = VQ(P1, P2);
		cout << VQ(P1, P2) << endl;
		GEP P3={2, 2, 2};
		y = VQ(P2, P3);
		cout << VQ(P2, P3) << endl;
		cout << GV(x, y) << endl;
		return 0;
	}	
}	


When I try to compile it, I get the following error message:

tobias@Lonestar:~/c++$ g++ en1.cpp -Wall
en1.cpp: In function ‘int GV(int, int)’:
en1.cpp:29:3: warning: ‘a’ is used uninitialized in this function [-Wuninitialized]
en1.cpp:29:3: warning: ‘b’ is used uninitialized in this function [-Wuninitialized]

What is wrong with my If/else statement?

How can I fix it?

Thanks for your help.

Tobias
You are using uninitialized variables. You have to give them some value before you can use them.
Fair enough. I wanted to compare the volume of the structures (VQ(P1, P2) and VQ(P2, P3)). So i handed over the volume to the function (GV) with the variables X and Y. My hope was that the function is going to use the values of X and Y. Do you know what I mean?
The problem is with the a and b variable in GV. Is it m and n you want to use instead?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int GV (int m, int n)
	{
		
		int a, b;
		if ( a < b )
			{
				return 1;
			}
		else if ( a == b )
			{
				return 0;
			}
		else
			{
				return -1;
			}
	}


You are using a and b instead of m and n that you are passing in.
change ab to mn and you should be fine.
remove ab
No, that doesnt work as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

int GV (int m, int n)
	{
		
		int m, n;
		if ( m == n )
			{
				return 0;
			}
		else if ( m > n )
			{
				return 1;
			}
		else
			{
				return -1;
			}
	}


I get the following error message:

tobias@Lonestar:~/c++$ g++ en1.cpp -Wall
en1.cpp: In function ‘int GV(int, int)’:
en1.cpp:28:7: error: declaration of ‘int m’ shadows a parameter
en1.cpp:28:10: error: declaration of ‘int n’ shadows a parameter

Any other ideas?
You initialize local variables now. Remove row 5 in last post.
This was likely a step in the right direction. Now it compiles without an error. BUT, the result is still wrong.

tobias@Lonestar:~/c++$ g++ en1.cpp -Wall
tobias@Lonestar:~/c++$ ./a.out
16
25
0

16 is smaller than 25 and therefore i want (-1) as a result and not (0).

Sorry, this was my fault! Now it works just fine! Thank you to everyone that helped me!

Topic archived. No new replies allowed.