Help with Loop

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;
		}
Where is the loop?
Not quite sure what else you're doing, without seeing the loop code, but maybe something like this is what you're looking to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
    int inputCol;

	cout << "Player 1 - Hide the treasure" << endl;
	cout << "Enter X & Y values for hidden treasure (0-8)" << endl;
	    
	do {
	    cout << "X: ";
	    cin >> inputCol;
	
	      if (inputCol > 8 || inputCol < 0) {
		cout << "Please enter a valid number (0-8), or -1 to exit" << endl;
		cin.clear();
		cin.ignore();
	      }
	} while (inputCol!=-1);

return 0;
}

Last edited on
Apologies , Here is the updated code:

1
2
3
4
5
6
7
8
9
10
11
cout << "Player 1 - Hide the treasure" << endl;
		cout << "Enter X & Y values for hidden treasure (0-8)" << endl;
		cout << "X:";
		cin >> inputCol;
		while (inputCol > 8 || inputCol < 0) {
			cin.clear();
			cin.ignore();
			cout << "Please enter a valid number (0-8)" << endl;
			cout << "X: ";
			break;
		}


I want the program to keep asking the user for a value for X until they get it correct - Thanks
Last edited on
The line cin >> inputCol; is missing within the loop:
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;
		while (inputCol > 8 || inputCol < 0) {
			cin.clear();
			cin.ignore();
			cout << "Please enter a valid number (0-8)" << endl;
			cout << "X: ";
		cin >> inputCol; // Note
			break; // Remove this break. Otherwise there is basically no loop
		}

	
[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.
Last edited on
Thank you for your help - Clearly, I'm a noob to coding...
coder777, I went for your method - Thank you
Topic archived. No new replies allowed.