Need help with my do while loop problem. :)

I have been experiencing difficulties finishing my function. To those who have time, please take a look.

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
void prefix(){
	int a;
	system("cls");
	
	do{
		
		cout << "[1] Start\n[2] Return\n";
		cin >> a;
		switch (a){
		case 1:
		{
				  char exp[STACK_SIZE], prefixExp[STACK_SIZE];
				  int j = 0;
				  cout << "Enter the expression: ";
				  cin.getline(exp, STACK_SIZE);
				  _strrev(exp);
				  strcat_s(exp, "(");
				  npush(')');


				  for (int i = 0; i < strlen(exp); i++){
					  if (isalnum(exp[i]))
						  prefixExp[j++] = exp[i];
					  else if (exp[i] == '+' || exp[i] == '-'){
						  while (stack[top] == '*' || stack[top] == '/' || stack[top] == '^')
							  prefixExp[j++] = npop();
						  npush(exp[i]);
					  }
					  else if (exp[i] == '(')
					  {
						  while (stack[top] != ')')
							  prefixExp[j++] = npop();
						  npop();
					  }
					  else
						  npush(exp[i]);
				  }
				  prefixExp[j] = '\0';
				  _strrev(prefixExp);
				  cout << prefixExp;

		}
			break;
		case 2:
			break;
		default:
			cout << "Invalid Input";
			break;
		}
	} while (a != 2);
}


The line cout << "[1] Start\n[2] Return\n"; keeps executing even though I'm in the "enter the expression" process.

EDIT:

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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <iostream>
#define STACK_SIZE 5
#include<cstring>
#define QUEUE_SIZE 5
#define NSTACK_SIZE 100
using namespace std;
int queueArr[QUEUE_SIZE];
int front = -1;
int rear = -1;	
int stack[STACK_SIZE];
int top = -1;
char nstack[NSTACK_SIZE];

void npush(int x)
{
	nstack[++top] = x;
}

char npop()
{
	return nstack[top--];
}


void push(int value)
{
	if (top == (STACK_SIZE - 1))
	{
		cout << "Stack is full!";
	}
	else
		stack[++top] = value;
}

void pop()
{
	if (top == -1)
	{
		cout << "Stack is empty!";
	}
	else
		cout << "You removed " << stack[top--] << endl;
}

void display()
{
	if (top == -1)
	{
		cout << "Stack is empty!";
	}
	else
	for (int i = top; i >= 0; i--)
		cout << stack[i] << "\t";
}
void enqueue(int x){
	if (front == -1)
		front++;
	if (rear == (QUEUE_SIZE - 1)){
		cout << "Queue is full!";
	}
	else

		queueArr[++rear] = x;
}

void displayq() {
	for (int i = front; i <= rear; i++)
		cout << queueArr[i] << " ";


}
void dequeue(){

	if (front == QUEUE_SIZE){
		cout << "Queue is empty!";
		front = -1;
		rear = -1;
	}
	else
		cout << "You removed " << queueArr[front++] << endl;

}

void stacks(){
	int choice;
	do{
		system("cls");
		int value;
		cout << "[1] Push\n[2] Pop\n[3] Display\n[4] Return\n";
		cout << "Enter your choice: ";
		cin >> choice;

		switch (choice)
		{
		case 1:
			cout << "Enter value: ";
			cin >> value;
			push(value);
			break;
		case 2:
			pop();
			cin.ignore();
			cin.get();
			break;
		case 3:
			display();
			cin.ignore();
			cin.get();
			break;
		case 4: 
			break;
		default:
			cin.ignore();
			cin.get();
			cout << "Invalid Input";

		}
	} while (choice != 4);
}
void queues(){
	int choice, item;
	do{
		system("cls");
		cout << "[1] Enqueue";
		cout << "\n[2] Dequeue";
		cout << "\n[3] Display";
		cout << "\n[4] Return";
		cout << "\nEnter your choice:";
		cin >> choice;
		switch (choice){

		case 1:
			cout << "Enter item to add:";
			cin >> item;
			enqueue(item);
			system("pause");
			break;
		case 2:
			dequeue();
			system("pause");
			break;
		case 3:
			displayq();
			system("pause");
			break;

		case 4:
			break;
		default:
			cout << "Invalid input";
		}
	}
		while (choice != 4);
}
void prefix(){
	
	system("cls");
	int a;
	int j = 0;
	
	do{
		cout << "[1] Start\n[2] Return\n";
	cin >> a;
		if (a == 1){
			char exp[NSTACK_SIZE], prefixExp[NSTACK_SIZE];

			cout << "Enter the expression: ";
			cin.getline(exp, NSTACK_SIZE);
			_strrev(exp);
			strcat_s(exp, "(");
			npush(')');


			for (int i = 0; i < strlen(exp); i++){
				if (isalnum(exp[i]))
					prefixExp[j++] = exp[i];
				else if (exp[i] == '+' || exp[i] == '-'){
					while (nstack[top] == '*' || nstack[top] == '/' || nstack[top] == '^')
						prefixExp[j++] = npop();
					npush(exp[i]);
				}
				else if (exp[i] == '(')
				{
					while (nstack[top] != ')')
						prefixExp[j++] = npop();
					npop();
				}
				else
					npush(exp[i]);
			}
			prefixExp[j] = '\0';
			_strrev(prefixExp);
			cout << "Prefix Notation: " << prefixExp << endl;
		}
	} 
	while (a != 2);
}
void postfix(){
	system("cls");
	int a;
	do{
		cout << "[1] Start\n[2] Return\n";
		cin >> a;
		switch (a){
		case 1:
		{
				  char exp[NSTACK_SIZE], postExp[NSTACK_SIZE];
				  int j = 0;
				  cout << "Enter the expression: ";
				  cin.getline(exp, NSTACK_SIZE);

				  strcat_s(exp, ")");
				  npush('(');


				  for (int i = 0; i < strlen(exp); i++){
					  if (isalnum(exp[i]))
						  postExp[j++] = exp[i];
					  else if (exp[i] == '+' || exp[i] == '-'){
						  while (nstack[top] == '+' || nstack[top] == '-' || nstack[top] == '*' || nstack[top] == '/' || nstack[top] == '^')
							  postExp[j++] = npop();
						  npush(exp[i]);
					  }
					  else if (exp[i] == '*' || exp[i] == '/'){
						  while (nstack[top] == '*' || nstack[top] == '/' || nstack[top] == '^')
							  postExp[j++] = npop();
						  npush(exp[i]);
					  }
					  else if (exp[i] == '^'){
						  while (nstack[top] == '^')
							  postExp[j++] = npop();
						  npush(exp[i]);
					  }
					  else if (exp[i] == ')')
					  {
						  while (nstack[top] != '(')
							  postExp[j++] = npop();
						  npop();
					  }
					  else
						  npush(exp[i]);
				  }
				  postExp[j] = '\0';

				  cout << postExp;
		}
			break;
		case 2:
			break;
		default:
			cout << "Invalid Input";
		}
	} while (a != 2); 
}

void notation(){
	int i;
	system("cls");
	do{
		cout << "[1] Infix to Prefix\n[2] Infix to Postfix\n[3] Return\n";
		cin >> i;
		switch (i){
		case 1:
			prefix();
			break;
		case 2:
			postfix();
			break;
		case 3:
			break;
		default:
			cout << "Invalid Input";
		}
	} while (i != 3);
}

int main()
{
	system("cls");
	int i;
	while (1){
		cout << "[1] Stacks\n[2] Queues\n[3] Notation\n[4] Exit\n";
		cin >> i;
		switch (i){
		case 1:
			stacks();
			break;
		case 2:
			queues();
			break;
		case 3:
			notation();
			break;
		case 4:
			exit(1);
			break;
		default:
			cout << "Invalid Input.";
			break;
		}
	}
	system("pause");
	return 0;
}


Notice when you enter number 3, it will show a menu consisting of infix to postfix and infix to prefix. Both of those programs have an error in their respective functions. They will both repeat the line cout <<"[1] Start\n[2] Return\n";
Last edited on
Its a do-while loop. First It prints out

cout << "[1] Start\n[2] Return\n"; Then it takes input cin >> a;.

Now. It enters the switch statement. Say case 1 happens. Now all the stuff inside of case 1 happens, and then it comes back to the switch statement and goes down to the end

1
2
3
	}
	} while (a != 2);
}


Then it sees the while statement, saying, run this as long as while is not 2. And then it remembers, ooohhh... a is equal to 1, becuase the user entered 1, not 2, so I will keep going!!!

Now, it goes all the way back to the beginning of the do-while loop. And what does it see first? You guessed it.

This cout << "[1] Start\n[2] Return\n";.

You will need to move this statement outside of the do-while loop if you dont want it to repeat. Also, I might have gotten your question wrong, but this is how I took it from the information you've given. If I did get your question wrong, please elaborate on what is wrong and be more specific.
I don't know how to write my question properly. I'll just post the whole source code and I guess you'll understand what I mean when you run it. Sorry for the hard time, and a million thank yous.
Sorry but, you'll have to ask a specific question, I cant go through all of the code like that. Whats going wrong? What are you inputting, what output are you expecting, what output are you actually getting? What line of code? What function, etc.
When I run my code, and I reach the "[1] Start\n[2] Return\n"; part, if I press 1, this line should execute
1
2
cout << "Enter the expression: ";
				  cin.getline(exp, NSTACK_SIZE);
instead, I get an output of:

[1] Start
[2] Return
Enter the expression: [1] Start
[2] Return


What I'm expecting is:
[1] Start
[2] Return
<cls>
Enter the expression: <user input here>
Prefix Notation: <the prefix of what inputted>
Last edited on
Much better question. Its because you're switching between cin and getline. cin lives a buffer or some shit.

Add a cin.ignore() after the cin >> a; in both prefix and postfix.

1
2
cin >> a;
cin.ignore();


Also, in the postfix, add an endl after you print out the postExp.
cout << postExp << endl;
Thank you very much!! Solved my problem. Have a great day man, thanks! :)
Topic archived. No new replies allowed.