How to accept only a single character?

How can I get only one character from the cin for a char?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  char PromptToContinue()
{
	int dobreak = 0;
	char choice;

	do
	{
		cout << endl << endl << "Would you like to enter another equation? (Y/N) ";
		cin >> choice;
		choice = tolower(choice);

		switch (choice)
		{
		case 'y':
			dobreak = 1;
			break;

		case 'n':
			dobreak = 1;
			break;

		default:
			cout << choice << " is not a valid input. Please try again.";
		}
		
	} while (dobreak == 0);
	cout << endl;
	return choice;


So what I need to do is only accept one character from the cin. When the user inputs x characters, it does the cout in the default x amount of times. How do I make it not do this?

Thanks.
You could have the loop ignore any other characters after the first that are left in the input buffer; that way, they will not be processed on the next iteration of the while loop.

After reading in one character, ignore all characters until the enter key the user pressed to send them:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')
Thank you!

If I understand correctly std::numeric_limits<std::streamsize>::max(), is just the maximum numeric value meaning it will clear the input buffer no matter how large. Is that correct?
Topic archived. No new replies allowed.