Why does it skip if and just print it?

closed account (iG3TqMoL)
When ever I run it skips checking and just runs the cout what do I do?

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
  #include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <ctime>
#include <cstdlib>



using namespace std;

int main(){
int health = 10;
int location [2] = {0,0};
bool monsterTile = false;
string direction;
int loop = 1;
int Mspawn [2] = {(rand () % 10+1 ),(rand () % 10+1 ) };

cout << "Your standing in a massive room \n";
cin >> direction;
// main loop for movement
while (loop == 1){
	// If they don't enter a recognised command
if (direction != "Forward","forward"){
	cout << "Please enter something valid"<< endl;
	cin >> direction;
}
// Movement loop
if (direction == "Forward","forward"){
	cout << "You move one tile " << direction << " \n Now which way?" << endl;
	cin >> direction;
	
}
	// If they don't enter a recognised command
if (direction != "Forward","forward"){
	cout << "Please enter something valid"<< endl;
	cin >> direction;
}
}

return 0;


} 
Last edited on
You can't just put a comma and expect direction to compare to both string literals.

Try: if (direction != "Forward" && direction != "forward")
Last edited on
Your code is misusing the comma operator.

This
if (direction != "Forward","forward")
should be rewritten as
if (direction != "Forward" && direction !="forward")


When two expressions are separated by a comma, only the right-most expression is considered as the resulting value.
The original code thus simplifies to if ("forward"), and since any non-zero value is considered true, the if condition is always true.

see http://www.cplusplus.com/doc/tutorial/operators/
closed account (iG3TqMoL)
Now when I run it when I enter forward it just freezes and stops running 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

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <ctime>
#include <cstdlib>



using namespace std;

int main(){
int health = 10;
int location [2] = {0,0};
bool monsterTile = false;
string direction;
int loop = 1;
int Mspawn [2] = {(rand () % 10+1 ),(rand () % 10+1 ) };

cout << "Your standing in a massive room \n";
cin >> direction;
// main loop for movement
while (loop == 1){
	// If they don't enter a recognised command
if (direction != "Forward" && direction != "forward" ){
	cout << "Please enter something valid"<< endl;
	cin >> direction;
}
// Movement loop
if (direction == "Forward" && direction == "forward"){
	cout << "You move one tile " << direction << " \n Now which way?" << endl;
	cin >> direction;
	
}
	// If they don't enter a recognised command
if (direction != "Forward" && direction != "forward" ){
	cout << "Please enter something valid"<< endl;
	cin >> direction;
}
}

return 0;


} 

Hi,

It's generally not a good idea to ask for a word as input, as it becomes difficult or unnecessarily long winded to verify it's validity. What if the user typed fOrward, or forWard, or ForwarD, how many combinations of mixed case are there for this word ?

So, consider asking for a single char as input, or present a small menu.

You also might want to avoid having the same code 3 times over - and that is just 1 direction.

I am absolutely not a fan of constructs like this:

if (direction != "Forward" && direction != "forward" ){

There is almost always a better way.

You could have a switch, which has a case: for each direction, and a default: case for bad input, put the whole thing in a bool controlled while loop.

Good Luck !!
closed account (iG3TqMoL)
Thanks man gotta work out some glitches but I'll probably figure it out. If I can't I'll just another thread thx man heres my re-work so far

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
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <ctime>
#include <cstdlib>



using namespace std;

int main(){
int health = 10;
int location [2] = {0,0};
bool monsterTile = false;
char direction;
string Dprint;
int loop = 1;
int Mspawn [2] = {(rand () % 10+1 ),(rand () % 10+1 ) };

cout << "Your standing in a massive room \n";
cin >> direction;
// main loop for movement
while (loop == 1){
	if (direction == 'w' || direction == 'W' ){
		Dprint = "forward";
		
	}
	if (direction == 'a' || direction == 'A' ){
		Dprint = "left";
		
	}
	if (direction == 'd' || direction == 'D'){
		Dprint = "right"; 
	}
	if (direction == 's' || direction == 'S'){
		Dprint = "backwards";
		
	}
	// if didnt enter valid movement command
	if (direction != 'w' || direction != 's' || direction != 'a' || direction != 'd'){
		cout << "Please enter something valid ";
		cin >> direction;
	}
	// movement loop
	if (direction == 'w' || direction == 's' || direction == 'a' || direction == 'd'){
		cout << "You move one tile " << Dprint;
		cin >> direction;
	}
	
	
	
}

return 0;


} 

Hi,

You can also make use of the tolower function, and do one comparison. Line 41 isn't necessary, just use an else statement, if it is not one of the other options then anything else is wrong.


http://www.cplusplus.com/reference/locale/tolower/




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
DirectionLC = std::tolower(direction);
bool Quit = false;

while (!Quit) {
   // call a function to display menu

   // investigate switch instead of if ,else if, else
   if (direction == 'd'){

   }

   else if (direction == 'a'){

   }

.
.
.
  else if (direction == 'q') { // user wants to quit
    Quit = true ;
  }
  
  else {
    std::cout << "Invalid option try again";
  }

}


Note that it is a good idea to always have an else statement or default case to catch bad input.
Line 41:
 
if (direction != 'w' || direction != 's' || direction != 'a' || direction != 'd')

That condition is always going to be true. You want &&, not ||.
 
if (direction != 'w' && direction != 's' && direction != 'a' && direction != 'd')



Everybody what do you say about Lets Enjoy Cpp?
Lets Enhoy Cpp is doing great he is explaining C++ in amazing way. It is very useful he explains the thing in very easy & fun way I like it you may also like his project.
I think you must visit it for one time http://www.letsenjoycpp.tk
closed account (iG3TqMoL)
works fine now except if there is more then one char typed into direction then it outputs multiple couts'

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
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <ctime>
#include <cstdlib>



using namespace std;

int main(){
int health = 10;
int location [2] = {0,0};
bool monsterTile = false;
char direction;
string Dprint;
int loop = 1;
int Mspawn [2] = {(rand () % 10+1 ),(rand () % 10+1 ) };

cout << "Your standing in a massive room \n";
cin >> direction;
// main loop for movement
while (loop == 1){
	if (direction == 'w' || direction == 'W' ){
		Dprint = "forward";
		
	}
	if (direction == 'a' || direction == 'A' ){
		Dprint = "left";
		
	}
	if (direction == 'd' || direction == 'D'){
		Dprint = "right"; 
	}
	if (direction == 's' || direction == 'S'){
		Dprint = "backwards";
		
	}
	
	// movement loop
	if (direction == 'w' || direction == 's' || direction == 'a' || direction == 'd'){
		cout << "You move one tile " << Dprint;
		cin >> direction;
	} else {
		cout << "Please enter something valid ";
		cin >> direction;
	}
	
	
	
}

return 0;


} 

@AbstractionAnon

Hi and I hope you have had an excellent festive season :+)

I have a great respect for all of your knowledge and the amount of help you have given over the years, but I just wanted to mention one small thing: that I don't think the if statement you mentioned is at all necessary; an else is all that is required. Moreover, I think such statements should be avoided - I personally find them ugly , error prone and non scalable. It would be great IMO if we could get the OP to use a switch instead.

I am absolutely sure you know all this exceedingly well, just that I didn't want the OP thinking that such statements were a good idea, and changing the code to go back to using them, or using them again in the future.

Anyway, kind regards :+)
Topic archived. No new replies allowed.