Variables problem

Hi ive been making my C++ program for several hours and I cant seem to make it work.


heres the 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <conio.h>
#include "again.h"

void main()
{
	int x,y;
	char cmd;

	printCommand();
	cmd = getch();


	switch(cmd){

	case 'a'
		:initialize(&x,&y);
		 printf("\nx=%d,y=%d",x,y);
            break;
	case 'b'
		:printLocation(x,y);
			break;
	case 'c'
		:move(&x,&y);
		printf("\nThe current location is x=%d,y=%d",x,y);
			break;

	
		exit();
		printf("GOODBYE:");
    }


}



char printCommand(void){

printf("Enter the command you want to execute\nA.]Origin\nB.]Location\nC.]MOVE\nD.]EXIT\n");
return 0;

}

void initialize(int *x,int *y){

	*x=0;
	*y=0;

}

void printLocation (int x, int y){

	
	printf("\nthe current location is %d,%d",&x,&y);



}

void move(int *x, int *y){

	int direction,points;

	printf("\nEnter the direction you want to move: ");
	scanf("%d",&direction);
	printf("\nhow many points you want to move?:");
	scanf("%d",&points);


		if(direction==1)
			*x+=points;
		else if(direction==2)
			*y+=points;
		else if(direction==3)
			*x-=points;
		else if(direction==4)
			*y-=points;
		else

		printf("\nINVALID DIRECTION!!!:");

}


void exit(void)
{
	

	return 0;
}






Its almost complete but theres a bit problem in the variables. it gave me some warnings stating that "local variable 'y' used without having been initialized".

if i remove the case switch it doesnt gave me any errors and it runs okay. but i need to make it a switch statement. Ive been grinding my brain for several hours and i cant figure out how to solve it. if anyone can help me thanks
1.) Use int main()
2.) You need to initialize x/y before you use them (i.e. assign them a value like 0 or something). (Although that *is* just a warning)
thank you it works but I have still a one or two problem, the main problem is that the fuction printLocation it doesnt return the correct value of x and y instead it gave me a very huge amount, the other problem is how can i terminate a program using a fuction. thanks in advance.

heres the change 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

#include <stdio.h>
#include <conio.h>
#include "again.h"

int main()
{
	int x=0,y=0;
	char cmd;
	do{
	printCommand();
	cmd = getch();

	switch(cmd){

	case 'a'
		:initialize(&x,&y);
		 printf("\nx=%d,y=%d",x,y);
            break;
	case 'b'
		:printLocation(x,y);
			break;
	case 'c'
		:move(&x,&y);
		printf("\nThe current location is x=%d,y=%d",x,y);
			break;

	
	}		
	}
	while(cmd!='d');
	printf("Goodbye:");
		exit();
		
		
    
}

char printCommand(void){

printf("Enter the command you want to execute\nA.]Origin\nB.]Location\nC.]MOVE\nD.]EXIT\n");
return 0;

}

void initialize(int *x,int *y){

	*x=0;
	*y=0;

}

void printLocation (int x, int y){

	
	printf("\nthe current location is %d,%d",x,y);



}

void move(int *x, int *y){

	int direction,points;

	printf("\nEnter the direction you want to move: ");
	scanf("%d",&direction);
	printf("\nhow many points you want to move?:");
	scanf("%d",&points);


		if(direction==1)
			*x+=points;
		else if(direction==2)
			*y+=points;
		else if(direction==3)
			*x-=points;
		else if(direction==4)
			*y-=points;
		else

		printf("\nINVALID DIRECTION!!!:");

}


void exit(void)
{

}





Last edited on
printf() does not take the addresses of the variables, just the values. Remove the &s.
Yeah thank you very much.
Topic archived. No new replies allowed.