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