-
Code:
while (true) {
// Display the players location and possible movements.
cout << "------------------" << endl;
cout << "Location: " << NewUser.location->name << endl;
cout << "\nDescription: " << NewUser.location->description << endl;
if (NewUser.location->north)
cout << "(N)orth to: " << NewUser.location->north->name << endl;
if (NewUser.location->south)
cout << "(S)outh to: " << NewUser.location->south->name << endl;
if (NewUser.location->east)
cout << "(E)ast to: " << NewUser.location->east->name << endl;
if (NewUser.location->west)
cout << "(W)est to: " << NewUser.location->west->name << endl;
cout << "(Q)uit" << endl;
// Get input and decide where to go next.
char input;
cin >> input;
if (input == 'n' && NewUser.location->north)
cout << quizMaster.poseQuestion().c_str() << endl;
getline(cin, answer);
if (quizMaster.isCorrectAnswer(answer))
cout << "Thats right" << endl;
else
cout << "Better luck next time !" << endl;
NewUser.location = NewUser.location->north;
if (input == 's' && NewUser.location->south)
cout << quizMaster.poseQuestion().c_str() << endl;
getline(cin, answer);
if (quizMaster.isCorrectAnswer(answer))
cout << "Thats right" << endl;
else
cout << "Better luck next time !" << endl;
NewUser.location = NewUser.location->south;
if (input == 'e' && NewUser.location->east)
cout << quizMaster.poseQuestion().c_str() << endl;
getline(cin, answer);
if (quizMaster.isCorrectAnswer(answer))
cout << "Thats right" << endl;
else
cout << "Better luck next time !" << endl;
NewUser.location = NewUser.location->east;
if (input == 'w' && NewUser.location->west)
cout << quizMaster.poseQuestion().c_str() << endl;
getline(cin, answer);
if (quizMaster.isCorrectAnswer(answer))
cout << "Thats right" << endl;
else
cout << "Better luck next time !" << endl;
NewUser.location = NewUser.location->west;
if (input == 'q')
break;
}
So basically it could be done like this?
-
I think a issue I need to address is how the loop is structured. so that moving to the loacation is not a part of the else
-
The logic could be reworked. Like so:
Code:
while (true) {
// ... display possible directions for travel
// Get input and decide where to go next.
char input;
cin >> input;
if (input == 'q')
break;
cout << quizMaster.poseQuestion().c_str() << endl;
getline(cin, answer);
if (quizMaster.isCorrectAnswer(answer)) {
cout << "Thats right" << endl;
if (input == 'n' && NewUser.location->north)
NewUser.location = NewUser.location->north;
if (input == 's' && NewUser.location->south)
NewUser.location = NewUser.location->north;
// ... other directions ...
} else {
cout << "Better luck next time !" << endl;
}
}
-
by doing it that way, when the user takes a direction, it does not move, it repeats the same room.
-
Can you tell why it's staying in the same room? You can use a debugger to find out exactly what code is being run when. If you pay careful attention to the output, the exact point at which something unexpected happens is usually a clue as to what is wrong.
Does the application ask for a direction choice?
Is the input getting read? (you can cout the input back to the user to confirm)
Does it ask a test question?
Does it correctly evaluate the answer to the test question? (display: That's right, or Better luck next time)
Is it moving, but to the wrong room? Are rooms connected properly?
-
I believe it is something to do with the input, it does not ask for it and defaults to incorrect, hence not moving anywhere.
-
This could be because there is leftover in the input buffer after the cin >> input; line.
Using cin for anything other than strings can be unreliable. You could try changing the line cin >> input; to this:
Code:
char input;
string inputString;
cin >> inputString; // Grab everything in the buffer, even if it's more than one character.
input = inputString[0]; // Only use the first character, discard the rest.
-
yeah I noticed that over the time I have been using c++, before I changed it to getline(cin,input) through out my program for example I had , Username, Age, Pet, This, That, as input for the user. The user could enter all of them in 1 line by putting a space between.
But you cannot use the getline function for char right?
-
Not directly, but you can use a string inbetween:
Code:
char input;
string inputString;
getline(cin, inputString);
input = inputString[0];
-
Yep, I just figured that I needed a getline instead of the cin in between.
Thanks alot for your help, I can put all my elements together now I got that troublesome part out the way!
-
Why do you need a char in the first place?