string uInput; int i,x; //Ask user for input for(i=0; i >=0; ++i) { cout << "Move (N)orth, (S)outh, (W)est, (E)ast, (Q)uit? "; cout << endl; cin >> uInput;// stores user input // stores the user Input in a variable X in order to make decisions later... if (uInput == "n" || "N"){ x = 0; } else if (uInput == "e" || "E"){ x = 1; } else if (uInput == "w" || "W"){ x = 2; } else if (uInput == "s" || "S"){ x = 3; } else if (uInput == "q" || "Q"){ cout << "Exiting..."; b
When I press "q" after the prompt nothing happens..
string uInput; int i,x; //Ask user for input for(i=0; i >=0; ++i) { cout << "Move (N)orth, (S)outh, (W)est, (E)ast, (Q)uit? "; cout << endl; cin >> uInput;// stores user input // stores the user Input in a variable X in order to make decisions later... if (uInput == "n" || "N"){ x = 0; } else if (uInput == "e" || "E"){ x = 1; } else if (uInput == "w" || "W"){ x = 2; } else if (uInput == "s" || "S"){ x = 3; } else if (uInput == "q" || "Q"){ cout << "Exiting..."; break; } else{ cout << "please insert a valid answer";} }
/*############### ### 6/13/13 ##### ## Navigator #### ### 2.7.2 ####### ###############*/ /* Write a program that displays a menu which allows the user to move one unit north, east, south or west. Each time the player enters a selection, update the coordinates of the user and output the current position. Start the player at the origin of the coordinate system. Your program’s output should look like the following: */ #include <iostream> #include <string> using namespace std; int main() { int nPos[4][2]= // Represents positions or coordenates { {0,0}, //N {0,1}, //E {1,0}, //W {1,1} //S }; string uInput; int i,x; //Ask user for input for(i=0; i >=0; ++i) { cout << "Move (N)orth, (S)outh, (W)est, (E)ast, (Q)uit? "; cout << endl; cin >> uInput;// stores user input // stores the user Input in a variable X in order to make decisions later... if (uInput == "n" || "N"){ x = 0; } else if (uInput == "e" || "E"){ x = 1; } else if (uInput == "w" || "W"){ x = 2; } else if (uInput == "s" || "S"){ x = 3; } else if (uInput == "q" || "Q"){ cout << "Exiting..."; break; } else{ cout << "please insert a valid answer";} cout << "Current position: ("; for(i=0; i <2; ++i){ cout << nPos[x][i]; cout << ","; } cout << ")"; cout << endl; } }
that is the whole code..
Seems like none of it is actually inputting anything. Still looking. Point of interest, why are you using i for two loops when one loop is inside the other?
one loops if for constantly ask the user the same question.. the other is to output the matrix
ohh u mean the same variable in both loops?
nice observation.. I changed the second loop and used j variable instead just for avoid future problems, thanks.
DOH! I see it... gonna test to be sure. || misuse.
"n" is always true...
This will hold the code for an hour... far better than posting directly here.... had to reformat the code because copy/paste in OS is a bit b0rken. http://pastebin.com/3uLMWLXW
You will notice I also cleaned up the cin >>
1 is true, "A" is true, etc. but x==1 is only true if x is 1 and test=="A" is only true if test is A. if (test="a" || "A") literally means: if est is a or TRUE.
wait... so (uInput "n" || "N") n is always true.. but (uInput "n" || uInput "N") is different? O.o .. I need better logic for this stuff... >>
Because the first is giving a literal on one side of the or pipes, it always has or TRUE. So yes, C++ is different from what you know. But if it was all worked out and simple, it would not be one of the older languages still used. C was designed to write operating systems without the use of assembly. C++ was made to objectify C and do a few other improvements. C is circa 1972. C++ came out in 1985. In contrast, Python came out in 1991 and benefited from seeing issues that had been found in other languages.
And don't ask how many times you will forget the ; at the end of the line... we all do.
the code is working T,T ... I have been working for two day on this.. Thanks!!
Here is a tip: Test output. It has been s whole since I did C++, so I did not see it at first. But I changed the code a little. First I did this: cout << endl; cin >> uInput;// stores user input cout << uInput; to make sure I was getting a value in. I was. So I erased that and I did this: if (uInput == "n" || "N"){ x = 0; cout << uInput;} To see what happened when I hit n, e, w, s.... and no matter what I hit, I saw a letter print. Then I remembered how || works in C++! Little tests and output like this can help a lot with debugging. Have fun with your code!
...been a while... not s whole. LOL
thanks again.. this is one of the exercicies in gameinstitute.com Im not takin C++ in college yet.. the first course of programming was in a different school and the language they used was VB. At that time I was studying Java.. but I left Java recently to study C++ for a huge number of reason. Nice to have you here. Thanks for the help.. now Im gonna get working in the next exercise.
Join our real-time social learning platform and learn together with your friends!