Somethin simple for ya

I been programming all day and I'm failing to see why the code below wont ever exit loop, even if the user enters the correct character.

1
2
3
4
5
	char move = 'z';
	while (move != 'w' || move != 's' || move != 'a' || move != 'd') {
		cin >> move;
		pos_player = pos_player + 1;
	}


I enter w and it gives me a new input line... same with s, a, and d?? Am I using the wrong quotes or format for the characters in the conditional?
Last edited on
closed account (3CXz8vqX)
Change the || to &&....it's because you're using != rather than == so logic is reversed.
You're using the || operator instead of the && operator. The way you have it set up is if the move does not equal ALL of the possibilities then the loop will keep evaluating to true.

 
while (move != 'w' && move != 's' && move != 'a' && move != 'd')
omg.. are you serious... Alright, thanks!
An example is:'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int pos_position = 0;
    char move1 = 'w';
    while(move1 == 'w' || move1 == 's' || move1 == 'a' || move1 == 'd')
    {
        move1 = getch();
        cout << move1 << " -";
        pos_position = pos_position + 1;
        cout << pos_position << " ";
    }
    return 0;
}
New question: I've created a game in console but am having trouble deciding what symbol to make the player. Is the smiley face (alt+1) a universal symbol? Should I use that as the player symbol? If so how, if not, what should I use?
Last edited on
while (move != 'w' && move != 's' && move != 'a' && move != 'd')

I personally hate dislike constructs like that - they are just ugly & error prone & not scalable.

I much prefer a switch to deal with this:

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
#include <locale> //for the tolower function

char MoveIput = 'w';
bool NeedInput = true;

while(NeedInput) {

    std::cout << "Enter Move direction - w (UP), a (LEFT), s (DOWN), d (RIGHT) " << std::endl;
    std::cin >> MoveIput;

MoveIput = std::tolower(MoveIput);

  switch(MoveInput) {

      case 'w':
          MoveUp();
          NeedInput = false;
          break;
      case 'a':
          MoveLeft();
          NeedInput = false;
          break;
      case 's':
          MoveDown();
          NeedInput = false;
          break;
      case 'd':
          MoveRight();
          NeedInput = false;
          break;
     default:
          std::cout << "Error bad move input" << std::endl;
          NeedInput = true;
          break;
  }//end switch
}//end while 
HELP

How can I turn line 4 into something that will test all the elements in the traps array instead of just checking static traps[0], 1, 2, 3, and 4 elements. The number of traps is actually defined to preprocessor as QTY_TRAPS.


1
2
3
4
5
6
7
8
9
10
// Lay treasure and traps on board
if (treasure == place){
	symbol = '$';
}else if (traps[0] == place || traps[1] == place || traps[2] == place || traps[3] == place || traps[4] == place){
	symbol = '*';
}else if (pos_player == place){
	symbol = 'u';
}else{
	symbol = '.';
}
Last edited on
How can I turn line 4 into something that will test all the elements in the traps array instead of just checking static traps[0], 1, 2, 3, and 4 elements.


Use a for loop.

If you want to test ALL of them rather than ANY of them, use another variable to count how many qualify.
nm got it.
Last edited on
OK
Topic archived. No new replies allowed.