why won't my pause function work with my getChar function?

Hi,

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.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <limits>
using namespace 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( const char legalChar[], const int 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;
}


/Skorpan
Hmm... you need another cin.ignore() in your pause function.

-Albatross
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.

/Skorpan
If you use before anything that uses cin, then indeed you'll need to press Enter twice. That said, how about having pause take a boolean argument?

(There has to be an easier way).

-Albatross
Hmm I found one way to make it work. If I put this line:

cin.ignore(numeric_limit<streamsize>::max(), '\n');

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.

-Albatross
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.

Learn more here http://www.cplusplus.com/forum/beginner/22128/#msg116074
(Your getchar() corresponds to std::cin >> foo and your pause() corresponds to std::getline().)

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.

Hope this helps.
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.

/Skorpan
Last edited on
Topic archived. No new replies allowed.