Reading From Different Inputs?

I have a piece of a code that has 3 different inputs

M <row> <col>
U <row> <col>
Q

So examples the user would have to type in would be:

M 0 1
U 1 2
U 4 5
Q

Except when someone enters in 'Q,' it expects rows and cols for the user to input, which I do not want. How do I differentiate between the two?

Here is the snippet of code I have currently:

1
2
3
4
5
6
cout << SetColor(GREEN) << "Current Board: " << endl;
		printBoard(board);
		printPlayerMenu();
		cin >> choice >> row >> col;
		choice = toupper (choice);
		if (choice == QUIT_PL_MENU)
Read entire lines using getline, then decide what to do with it based on the contents.
Thanks of the response :)!

So I ended up using

1
2
3
4
5
6
7
8
9
10
11
12
string input;
char choice;
int row = 0;
int col = 0;

cout << SetColor(GREEN) << "Current Board: " << endl;
		printBoard(board);
		printPlayerMenu();
		cin >> choice;
                getline(cin,input);
		choice = toupper (choice);
		if (choice == QUIT_PL_MENU)


So, when a person types in:

M 5 4

It evaluates the first letter, then getlines the rest of the line. How would go about assigning the value 5 and 4 to row and col?

Last edited on
> I'm not sure on how to do this, as they are all different types.

Read them separately, with a check for exit in between.
1
2
3
4
5
6
if( ( std::cin >> choice ) && ( std::toupper(choice) != QUIT_PL_MENU ) )
{
       std::cin >> row >> col ;

       // ...
}

char choice;

It can store only one char no more, Instead use

char choice[5]

choice[0] //M

choice[2] //5

choice[4] //4

then it would be easier,

1
2
3
row = std::atoi(choice[2]); //put the integer equivalent of choice[2] in row

col = std::atoi(choice[4]); //put the integer equivalent of choice[4] in col 


by the way you would need

#include <cstdlib> //for atoi() function
Last edited on
Yet another easier way could be

1
2
3
4
5
6
std::cout << "Input choice";
std::cin >> choice;
std::cout << "Input row";
std::cin >> row;
std::cout << "Input col";
std::cin >> col;
Topic archived. No new replies allowed.