How to fix non-terminating loop?

Hi guys,

I am continuing to work on my refresher project for my entry level C++ software engineer interview. I have ran into another snag however. I am creating a text based game and there is a do-while loop that is supposed to terminate when the player chooses one of three answers. However, the loop does not exit. I was wondering if you could tell me what is wrong with the code?

P.S. I would also appreciate your feedback as well on the WIP game and the code in general. I am a computer science graduate with 0 years of professional experience so try not to be too critical!

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
  #include "stdafx.h"
#include <iostream>
#include <string>
#include <Windows.h>

using namespace std;

class Actor {
protected:
	int hp;
	int attack;
	int speed;
	int defense;
	string name;

public:
	virtual void takeDamage(int a) = 0;
};


class Player:protected Actor {
public:
	void takeDamage(int a) {
		hp = hp - (a - defense);
	}
	void getName() {
		cout << "Please enter a name." << endl;
		getline(cin, name);
	}

	void viewHealth() {
		cout << name << " HP: " << hp << endl;
	}

	int getAttack() {
		return attack;
	}

	string viewName() {
		return name;
	}

	Player() {
		hp = 100;
		attack = 50;
		defense = 20;
		speed = 30;
		getName();
	}
};

class Mummy : protected Actor {
public:
	
	void takeDamage(int a) {
		hp = hp - (a - defense);
	}
	
	int getAttack() {
		return attack;
	}

	Mummy() {
		hp = 20;
		attack = 30;
		defense = 5;
		speed = 25;
	}
};

class Werewolf : protected Actor {
public:
	void takeDamage(int a) {
		hp = hp - (a - defense);
	}

	int getAttack() {
		return attack;
	}

	Werewolf() {
		hp = 50;
		attack = 40;
		defense = 40;
		speed = 40;
	}
};

class Frankenstein : protected Actor {
public:
	void takeDamage(int a) {
		hp = hp - (a - defense);
	}

	int getAttack() {
		return attack;
	}

	Frankenstein() {
		hp = 150;
		attack = 80;
		defense = 20;
		speed = 25;
	}
};

class Dracula : protected Actor {
public:
	void takeDamage(int a) {
		hp = hp - (a - defense);
	}

	int getAttack() {
		return attack;
	}

	void setHp(int a) {
		hp = hp + a;
	}

	void setAtk(int a) {
		attack = attack + a;
	}

	void setDef(int a) {
		defense = defense + a;
	}

	void setSpeed(int a) {
		speed = speed + a;
	}

	Dracula() {
		hp = 200;
		attack = 200;
		defense = 200;
		speed = 200;
	}
};

int main()
{
	cout << "Welcom to Dracula's Revenge!" << endl;

	Player p1;
	Mummy m;
	Werewolf w;
	Frankenstein f;
	Dracula d;

	int c = 0;

	cout << p1.viewName() <<"!" << endl;
	Sleep(500);
	cout << "Your family name is known far and wide for slaying evil and saving humainity from the brink of chaos for generations." << endl;
	Sleep(2500);
	cout << "Now Dracula has returned once more to drive humanity into eternal darkness." << endl;
	Sleep(2500);
	cout << "Your time has come to venture forth to save humanity and fulfill your family legacy!" << endl;
	Sleep(2500);
	cout << "With your sword, Blessed Europa, and a wooden stake you set out to confront the Prince of Darkness in his domain." << endl;
	cout << "" << endl;
	Sleep(2500);

	cout << "As you approach the castle gate, a mysterious voice calls to you: "<<endl;
	Sleep(2500);
	cout << "You who would stand between darkness and humanity, if you would proceed any further then answer me: " << endl;
	Sleep(2500);
	cout << "Tutorial: Throughout your quest you will come across various questions that are related to the tales of Dracula." << endl;
	cout << "You will still proceed normally if you answer incorrectly, but the final confrontation will prove to be more difficult." << endl;
	Sleep(4000);

	do {
		cout << "What form is Dracula most known for transforming into?" << endl;
		cout << "" << endl;
		Sleep(2500);
		cout << "1: Bat" << endl;
		cout << "2: Wolf" << endl;
		cout << "3: Fog" << endl;
		
		cin >> c;

		switch (c) {
		case(1):
			cout << "The power of darkness wanes...." << endl;

			d.setHp(-20);
			d.setAtk(-20);
			d.setDef(-20);
			d.setSpeed(-20);
			Sleep(2500);
			break;

		case(2):
			cout << "The power of darkness grows!" << endl;
			d.setHp(20);
			d.setAtk(20);
			d.setDef(20);
			d.setSpeed(20);
			Sleep(2500);
			break;

		case(3):
			cout << "The power of darkness grows!" << endl;
			d.setHp(20);
			d.setAtk(20);
			d.setDef(20);
			d.setSpeed(20);
			Sleep(2500);
			break;

		default:
			cout << "Please enter a number corresponding with the choices." << endl;
			break;
		}

	} while (c != 1 || c != 2 || c != 3);
	

	return 0;
}
closed account (SECMoG1T)
repeat if c is not either of {1,2,3} , you need && instead of ||

1
2
while (c != 1 && c != 2 && c != 3);///since you want all the 3 conditions to be true for it to loop again
Last edited on
Topic archived. No new replies allowed.