Hi - This is likely to be a simple fix. But I am tasked with creating a treasure hunt game Where the Player must enter a X value and a Y value. The number must be between 0-8 and ask the Player for another value if not 0-8. My problem is that the program picks up if there is an incorrect value entered but carries on with the program rather than stay in the loop... Please help? Thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14
cout << "Player 1 - Hide the treasure" << endl;
cout << "Enter X & Y values for hidden treasure (0-8)" << endl;
cout << "X:";
cin >> inputCol;
if (inputCol > 8 || inputCol < 0) {
cout << "Please enter a valid number (0-8)" << endl;
cin.clear();
cin.ignore();
cout << "X: ";
}
else {
break;
}
[posted this without having seen response from coder777]
Your while loop is only going to execute once at most, and then carry on with the code beneath it, whatever that is, and you don't have any inputs in the loop itself. Not quite sure what else you're expecting it to do.
Albeit imperfect, I thought that the code I posted above, with the input inside the loop, is a little closer to how you might want to try it. (you can run the code by clicking the little wheel).
With your code, you print a couple of lines of prompt, get an input, check if it's >8 or <0, print a message if it's not between 1 and 8, and then carry on with the program whether it is or not.