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:
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
}
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.
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?