Need Help Using Do Loops and While Loops in C++

What am I doing wrong?
I have been doing super simple C++ for a while but when I got the idea to actually try and make something with it, I wasn't sure where to start. But it's very basic on what it's got to do, I just don't have the knowledge to fix it and my book that I'm learning from doesn't help me.
Here's 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
  // Code Of Conduct.cpp
//

#include "stdafx.h"

#include <iostream>

using namespace std;

int swingSword = 1;

int stabSword = 2;

int dodge = 3;

int kick = 4;

int enemyHealth = 100;

int playerHealth = 100;


int main()
{
	char move;
	do
	{
		cout << "What's your move?\n";
		cout << "1 - Swing Sword\n";
		cout << "2 - Stab with Sword\n";
		cout << "3 - Dodge enemy attack\n";
		cout << "4 - Kick enemy\n";
		cout << "Your move: ";
		cin >> move;
		if(move = 1)
		{
			cout << "You swung your sword at your enemy.\n";
			cout << "Your enemy takes 20 damage!\n";
			enemyHealth -= 20;
		}
		if(move = 2)
		{
			cout << "You stabbed at your enemy with your sword.\n";
			cout << "Your enemy takes 18 damage!\n";
			enemyHealth -= 18;
		}
		if(move = 3)
		{
			cout << "You dodged your enemy's attack.\n";
			cout << "Your enemy cunningly cut your side and you took 20 damage and began bleeding\n";
			playerHealth -= 20;
			playerHealth = --playerHealth;
		}
		if(move = 4)
		{
			cout << "You kicked your enemy.\n";
			cout << "Your enemy stumbles back but takes no damage.\n";
		}
		else
		{
			cout << "You made an illegal move.\n";
		}

		if(playerHealth == 0)
		{
			cout << "You died. Game Over.\n";
			return 0;
		}
	} while (move != 4 ||move != 3 ||move != 2 ||move != 1);

	return 0;

}


The problem is in the end of the 'do' loop where I have tried to end it with a 'while' loop. I try to run the program as I have it but no matter what the user input is, everything repeats.

Thanks!
while (move != 4 ||move != 3 ||move != 2 ||move != 1);
This statement will always evaluate to true, because one of the conditions that you're testing for will always be true. There is no way that move can be equal to 1 and 2 and 3 and 4 all at the same time, so move will always be not equal to some of those values.

Did you mean to use && instead of || ?

since move isn't an integer variable, you can't access it like an integer, thus accessing it would be '1' i.e.
1
2
3
4
if (move=='1')
//follow these instructions
else if (move=='2')
//do all these 
Did you mean to use && instead of || ?


Yes, because I think that || means the left statement or the right statement.

What I intended to do was say that if move is not equal to 4, 3, 2, or 1 then it will simply ask what your move is again.
I don't see a statement for when the enemy is dead. Could this info be helpful?
@SuperRazor

I personally dislike do loops, and I really hate constructs like this:

while (move != '4' && move != '3' && move != '2' && move != '1');

These are just plain ugly and not really scalable - what if you had 10 menu options?

The fact that a do loop always executes at least once should not be the motivation for preferring them over a while or for loop IMHO. K&R (who invented C) don't like them either. There are some situations where they are valid, but I don't think this is one of them. All 3 types of loops can be re-written in one of the others forms, so that is what I prefer to do with do loops - sometimes at the price of 1 extra variable.

I would have a boolean controlled while loop with a switch inside which has a quit case and default clause to deal with bad input:

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
void ShowMenu(); 

bool Quit = false;
char move = 'z';

while(!Quit) {
      if(playerHealth == 0) {
			cout << "You died. Game Over.\n";
			return 0;
      }
      if (enemyHealth == 0) {
             //your code here
      }

     ShowMenu();
     cin >> move;

     switch (move) {
            case '1':
                cout << "You swung your sword at your enemy.\n";
		cout << "Your enemy takes 20 damage!\n";
		enemyHealth -= 20;
                break;

            case '2':
                 //your code here
                 break;

            case '3':
                 //your code here
                 break;

            case '4':
                 //your code here
                 break;

            case '5': //user wants to quit
                 Quit = true;  //execution continues after the switch
                  //or do this to end program
                 //return 0;
                 break;

            default:
                  std::cout << "Bad Input, Try again " << std::endl;
                  break;
     }

}

return 0;
} //end of main

void ShowMenu() {
cout << "What's your move?\n";
		cout << "1 - Swing Sword\n";
		cout << "2 - Stab with Sword\n";
		cout << "3 - Dodge enemy attack\n";
		cout << "4 - Kick enemy\n";
                cout << "5 - Quit\n\n";
		cout << "Your move: " << endl; //flush buffer with endl occasionally

}


Seen as there is only a small amount of code in the switch cases, I just left it there, but normally one would call a function from each of the cases if there was more than say 10LOC.

Hope all goes well.
@TheIdeasMan

I'm not very far in C++ so I'm not familiar with using 'void' things.

But I noticed when I tried to see what your code did, Visual Studio 10 had complaints about declarations.
Was there supposed to be #include <iostream> or using namespace std;?

The 'switch' is something I have learned recently and have a better understanding of it than things I have learned earlier. I understood what it was doing but as I have said in my second paragraph;
complaints about declarations


Can anyone help me get my original code to a format I can understand, or do I need to go further into the book?
I didn't post all of the code - just enough to give you an idea. Clearly there has to be include's & main etc. I leave it up to you to insert my code into yours & make any necessary changes, although you should note that lines 10 -16 in your code won't be needed. Line 1 of my needs to go before main() in your code.

void is a return type and just means the function doesn't return anything. The ShowMenu function just prints out the menu, so doesn't need to return any value.

You are nearly there, have a go at inserting my code, then post it so we can see how you went.
@ superRazor:
First things first. I definitely prefer the coding of TheIdeasMan.
But you wanted keep your original coding, this is how I changed/editied your code:

HTH


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
int move;
	do
	{
		cout << "What's your move?\n";
		cout << "1 - Swing Sword\n";
		cout << "2 - Stab with Sword\n";
		cout << "3 - Dodge enemy attack\n";
		cout << "4 - Kick enemy\n";
		cout << "Your move: ";
		cin >> move;
		if(move == 1)
		{
			cout << "You swung your sword at your enemy.\n";
			cout << "Your enemy takes 20 damage!\n";
			enemyHealth -= 20;
		}
		if(move == 2)
		{
			cout << "You stabbed at your enemy with your sword.\n";
			cout << "Your enemy takes 18 damage!\n";
			enemyHealth -= 18;
		}
		if(move == 3)
		{
			cout << "You dodged your enemy's attack.\n";
			cout << "Your enemy cunningly cut your side and you took 20 damage and began bleeding\n";
			playerHealth -= 20;
			playerHealth = --playerHealth;
		}
		if(move == 4)
		{
			cout << "You kicked your enemy.\n";
			cout << "Your enemy stumbles back but takes no damage.\n";
		}
		else
		{
			cout << "You made an illegal move.\n";
		}

		if(playerHealth == 0)
		{
			cout << "You died. Game Over.\n";
			return 0;
		}
	} while (move == 1 || move == 2 || move == 3 || move == 4);

Topic archived. No new replies allowed.