Need Suggestions/Comments

As a beginner in programming, I appreciate any comments/suggestions that could improve this program or make is more simpler and clean.

This program output a random number (0-100) and user will be prompt to input numbers including operator

somewhat like this:

SUM = ADDEND + ADDEND
DIFFERENCE = MINUEND - SUBTRAHEND
PRODUCT = FACTOR * FACTOR
QUOTIENT = DIVIDEND / DIVISOR

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
  /* ADDEND + ADDEND = SUM 
MINUEND - SUBTRAHEND = DIFFERENCE 
FACTOR x FACTOR = PRODUCT 
DIVIDEND / DIVISOR = QUOTIENT 
*/

#include <iostream>
#include <cstdlib>
#include <ctime>

double x;
double y;
double z;
char xOp; // Operator


// operator functions
int add()
{
	z = x + y;
	std::cout << z << "\n" << std::endl;
	return z;
}

int sub()
{
	z = x - y;
	std::cout << z << "\n" << std::endl;
	return z;
}

int mult()
{
	z = x * y;
	std::cout << z << "\n" << std::endl;
	return z;
}

int div()
{
	z = x / y;
	std::cout << z << "\n" << std::endl;
	return z;
}


// program main loop

int main()
{
	srand((time(0))); // seed random number

	while (true)
	{
		std::cout << " a = addition\ts = subtraction\t  m = mulplication\td = division\n\n";

		int rNumber = rand() % 100; // random number from 0-100
		
		char process[4] = { 'a', 's', 'm', 'd' };

		char processlist = process[rand() % 4];

		xOp = processlist;

		std::cout << rNumber;	// output a random number and process
		std::cout << "(" << processlist << ")\n"; 

		while (!(std::cin >> x)) // checking x
		{
			std::cout << "not integer\n";  // ignoring non-integer
			std::cin.clear();	// clearing the loop
			std::cin.ignore();	
		}

		std::cin >> xOp;			// checking Operator
		
		while (!(std::cin >> y)) // checking y
		{
			std::cout << "not integer\n";
			std::cin.clear();
			std::cin.ignore();
		}

		// checking operator
		if (processlist == 'a') // checking the Process Type
		{
			while (!(xOp == '+')) // checking if operator match to the process type
			{
				if (xOp == '-') { sub(); }
				else if (xOp == '*') { mult(); }
				else if (xOp == '/') { div(); }
				else { std::cout << "Operator not recognized\n"; }
				std::cout << " Process is not expected\n";
				goto SYSTEM_CLEAR;
			}

			add();	//function call

			if (z == rNumber ) // check if answer is equal to the random number
			{
				std::cout << z << " = " << rNumber << std::endl;
			}
			else {
				std::cout << z << " is not equal to " << rNumber << std::endl;
			}
		}
		else if (processlist == 's')
		{
			while (!(xOp == '-'))
			{
				if (xOp == '+') { sub(); }
				else if (xOp == '*') { mult(); }
				else if (xOp == '/') { div(); }
				else { std::cout << "Operator not recognized\n"; }
				std::cout << "Process is not expected\n";
				goto SYSTEM_CLEAR;
			}

			sub();
			
			if (z == rNumber ) // check if answer is equal to the random number
			{
				std::cout << z << " = " << rNumber << std::endl;
			}
			
			else {
				std::cout << z << " is not equal to " << rNumber << std::endl;
			}
		}
		else if (processlist == 'm')
		{
			while (!(xOp == '*'))
			{
				if (xOp == '-') { sub(); }
				else if (xOp == '+') { mult(); }
				else if (xOp == '/') { div(); }
				else { std::cout << "Operator not recognized\n"; }
				std::cout << "Process is not expected\n";
				goto SYSTEM_CLEAR;
			}
			
			mult();
			
			if (z == rNumber ) 
			{
				std::cout << z << " = " << rNumber << std::endl;
			}
			
			else {
				std::cout << z << " is not equal to " << rNumber << std::endl;
			}
		}
		else if (processlist == 'd')
		{
			
			while (!(xOp == '/'))
			{
				if (xOp == '-') { sub(); }
				else if (xOp == '*') { mult(); }
				else if (xOp == '+') { div(); }
				else { std::cout << "Operator not recognized\n"; }
				std::cout << "Process is not expected\n";
				goto SYSTEM_CLEAR;
			}

			div();

			if (z == rNumber ) 
			{
				std::cout << z << " = " << rNumber << std::endl;
			}
			
			else {
				std::cout << z << " is not equal to " << rNumber << std::endl;
			}
		}
		else
		{
			std::cout << "invalid operator";
		}

	SYSTEM_CLEAR:				// clear console 
		std::cin.get();
		std::cin.get();

		std::system("cls"); 
	}
	
}

Do it again, but now without the global variables and without goto.
Hi,

I had read some comments that using "goto" is bad programming because it will make a program hard to follow.
However, I been trying that before posting but failed.

It seems that goto is the easiest to solve this problem but knowing its bad I guess I need to avoid it..

Can you give me an alternative to goto statement?
On a quick look, it looks like you are using goto in the same way that a function is used.
http://www.cplusplus.com/doc/tutorial/functions/
Scroll down to Functions with no type. The use of void and you will find a way to replace it.
Topic archived. No new replies allowed.