I'm fairly new to c++ and programming. In my program I've written a function pausing() (see below) which I was hoping I could use anywhere in my program to pause it until a user presses Enter (or return). For the most part it works fine but when I try to use it after calling my getChar function the program just skips the pause.
I've sought for a solution but haven't found one. I suspect that there is something with the inputstream not being cleared after the function's executed. It would be great if someone could take a look at my code below.
I should mention that I'm writing my code on a linux computer but it should also work on a windows computer, so platform specific solutions like system(PAUSE) or the like won't do the trick.
Any enhancements to the pausing or the getChar functions will be greatly appreciated.
#include <iostream>
#include <limits>
usingnamespace std;
// my pause function
void pausing()
{
// Print pause message
cout << "\npress ENTER to continue..." << endl;
// ignore extracts everything from input buffer untill a /n is found
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
// my getChar function let the user enter one of the legal chars and sends back the choice
// to caller
char getChar( constchar legalChar[], constint nrOfChars )
{
char fetchedChar; // to store the users choice
int controlStatus = 0; // 0 indicates that the character supplied is illegal, 1 indicates legal character
char userInput[2] = { 0, 0}; // to store and check user input
do
{
cin >> userInput; // get user input
char charToCheck = userInput[0]; // first char is the one of interest
int secondChar = userInput[1]; // this one should still be 0
// look for legal char
for( int i = 0; i < nrOfChars; i++)
{
// if first char is one of the legal char AND there is no second char
if( charToCheck == legalChar[i] && secondChar == 0)
{
// indicate that we found a legal char and this is the only input given
controlStatus = 1;
fetchedChar = charToCheck;
}
}
// if we didn't find a legal char
if( controlStatus != 1)
{
// prompt user to enter a legal char
cout << "Please enter one of the following characthers: ";
for( int j = 0; j < nrOfChars; j++)
{
cout << legalChar[j] << " ";
}
cout << "\n";
}
}while (controlStatus == 0);
return fetchedChar;
}
int main()
{
cout << "Choose one of these letters: y / n" << endl;
char legalChars[2] = {'y','n'};
char chosenChar = getChar(legalChars, 2);
cout << "\nYou chose: " << chosenChar << endl;
pausing();
cout << "\nGood Chioce =)" << endl;
return 0;
}
I thought that was what I did with cin.ignore(numeric_limit<streamsize>::max(), '\n');
I tried putting another cin.ignore() in the pause function and then it worked with the getChar function but if I used it (the pause function) after any other function I had to press twice before execution continued.
in the getChar function just before the return statement it works. I guess there is something (specifically a \n) left in the input stream after my getChar function?
Albatross I'm not sure about what you mean by letting the pause function take a boolean argument. How would I do that? (what I wrote will fix my problems but anything new I could learn would be great :) )
If your pause function took a boolean argument that if true would use cin.ignore() in your pause function as well as the long cin.ignore(limit) and defaulted to true... nah, never mind. It's just speculation.
The pause function itself was correct. Adding another cin.ignore() changes the behavior of the pause function -- you would have to press ENTER twice, instead of the expected once.
The problem is that getChar() extracts only one character from the input stream, leaving behind a newline. If you immediately call pause(), then the newline left behind from the last user input is used, and the program doesn't behave as expected.
Remember this cardinal rule:
The user will always press ENTER after every input.
After every user input, you must make sure to get rid of the ENTER key (newline character) left in the input, so that the input stream is synchronized with your program's input expectations.
ah but basically you mean that I should check if there is anything in the input stream and if there is i use the cin.ignore() as well as what I already got and if not I use the function as is?
Yeah I guess I could do something like that.
Thanks for your input Albatross!
EDIT: Thanks Duoas, I think that was pretty much what I found out when I put a cin.ignore(numeric...) at the end of my getChar function (although you put words to it :) ). Although that was a very good rule I didn't really think of it that way (it was more luck that I tried my solution). Also a great post you linked to, good example.