Take in character followed by co-ordinate (white-space)?

Hi there. I'm trying to work on this program and I need help for getting a way for inputting characters like so:

"Press G followed by the grid position(row, col) to guess.
Press F followed by the grid position(row, col) to flag a mine. Press Q to
quit
G 1 1
1 * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *"

I want to be able to take in the character followed by the two integers for the position in my grid in one input line, but can't quite figure it out.
My code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void MineSweeper::flag()
{
	char playInput = 0;

	while (playInput == 0)
	{
		std::cout << "Press G followed by the grid position (row, column) to guess." << std::endl;
		std::cout << "Press F followed by the grid position (row, column) to flag a mine." << std::endl;
		std::cout << "Press Q to quit" << std::endl;
		std::cin >> playInput;
		if (std::cin.fail())
		{
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}


	}
	//set up the menu for the user actually playing the game
	//if there's a bomb, fail
	//if theres a flag, place flag
	//if a flag has been placed, will need to adjust the remaining bomb count when updated
}


Any help would be appreciated!
Last edited on
You need a variable for row and one for col
1
2
3
4
char playInput = 0;
int row = 0, col = 0;

cin >> playInput >> row >> col;
Thanks, it sort of works, but what ways are there of limiting the user so that they can have just G or F inputted, and also to have it so that there is just ways of stopping invalid data? I've already used the cin.fail, but I'm not sure what other levels of error check I could also use on top.
Basically you can't prevent the user to hit any key on the keyboard. You can only afterward check if the input is valid or not.
1
2
3
4
5
6
7
8
9
10
11
12
13
char playInput = 0;
int row = 0, col = 0;

cin >> playInput >> row >> col;
playInput = toupper(playInput);
if (playInput == 'G' || playInput == 'F')
{
   // valid
}
else
{
   // handle invalid input
}
Yeah I understand all about it not checking until after input, but I need to have this not continue unless they enter correct input. I'll try what you have there, but what exactly does a "toupper()" function do?
toupper converts a character to uppercase.
http://www.cplusplus.com/reference/locale/toupper/

You can get the input in a loop that exists only when the input is valid.
Topic archived. No new replies allowed.