Defination in user defined function

When I tried to compile this code, I get an error that variable "position" has not been defined for my int main. My defination for the variable is in my first function and I'm unsure of how to fix it. I've tried to put the variable defination in different places but putting it inside my int main just results in the function not working probably since it's not defined properly in that case. Any help to solve this is appreciated. Thanks.

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
95
96
97
98
99
100
101
#include <iostream>
#include <iomanip>
using namespace std;

char getPosition (float, float);
void printPosition (float, float, char);

const char firstQuad = '1';
const char secondQuad = '2';
const char thirdQuad = '3';
const char fourthQuad = '4';
const char atX = 'x';
const char atY = 'y';
const char origin = 'o';

int main ()
{
	float x, y;
	cout << fixed << showpoint << setprecision (2);
	
	cout << "Enter x and y: ";
	cin >> x >> y;
	
	cout << "==> (" << x << ", " << y << ") ";
	printPosition (x, y, position);
}

char getPosition (float x, float y)
{
	char position;
   
	if ((x > 0) && (y > 0))
		position = firstQuad;
		
	else if ((x > 0) && (y < 0))
		position = fourthQuad;
	
	else if ((x < 0) && (y > 0))
		position = secondQuad;
		
	else if ((x < 0) && (y < 0))
		position = thirdQuad;
		
	else if ((x  = 0) && (y = 0))
		position = origin;
		
	else if (x = 0)
		position = atX;
		
	else position = atY;
   
	return position;
}

void printPosition (float x, float y, char position)
{
 	 switch (position)
		{
		case firstQuad: 
			
			cout << "is above X-axis" 
				 << "\n==> It is at first quadrant"
				 << endl;
				 break;
	    case secondQuad: 
			
			cout << "is above X-axis" 
				 << "\n==> It is at second quadrant"
				 << endl;
				 break;
		case thirdQuad:
			
			cout << "is below X-axis" 
				 << "\n==> It is at third quadrant"
				 << endl;
				 break;	   	   
		case fourthQuad: 
			
			cout << "is below X-axis" 
				 << "\n==> It is at fourth quadrant"
				 << endl;
				 break;	   	   
	 	case atX: 
			
			cout << "is at X-axis" 
				 << endl;
				 break;
		case atY:
		
			cout << "is at Y-axis"
				 << endl;
				 break;
		case origin:
		
			cout << "is at origin"
				 << endl;
	    	     break;
		}
}

Where do you place getPosition() in your main function ?
You just insert char position; in your main and have to put position = getPosition(x,y); somewhere. Maybe before printPosition(x,y,position);
Last edited on
Ah so that was it. Yeah it's working perfectly now. Thank you so much.
Topic archived. No new replies allowed.