Error reading in Enter key stroke

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void BookInfoData::Descend(){
        bool junk;
        string key;

        do{
                cin.ignore(256, '\n');
                stop = bList.end();
                current = bList.begin();
                while(current != stop){
                        junk = Print();
                        cin.ignore(256, '\n');
                        current++;
                }
                cout << "No more book records are in the requested file" << end$
                cout << "(0 to return to menu)" << endl;
                getline(cin, key);
        }while(key != "0");
}


Using this code I'm displaying information on a linked list, but when it reaches the end of the list is displays a message to the screen stating that no more records are availible. The user and input a 0 and go to the Parent method or if they input enter it redisplays the information in the linked list. I believe garbage input is ignored until (0 and then ENTER is pressed) or (ENTER is pressed by itself).

BookCode 6128
Title Evil Under the Sun
Publisher Code PB
Type MYS
Price $4.45
Paperback Y


BookCode 6908
Title Using Microsoft Access
Publisher Code AW
Type CS
Price $20.50
Paperback Y


BookCode 7443
Title Carrie
Publisher Code MP
Type HOR
Price $6.75
Paperback Y


No more book records are in the requested file
(0 to return to menu)
*
*
BookCode 0189
Title Kane and Able
Publisher Code BB
Type PSY
Price $7.65
Paperback Y


If you notice in the output about I input a enter and it just went to the next line and waited for more input and it doesn't matter if I put 0 on the next line because it redisplays the linked list as if only the keystroke, after the immediate display of the end of list message, is read and the second ignored.

The *'s above are where I pressed the ENTER key.


Please help... I finished the program but this is annoying because I can't figure it out.
I tried making something similar and had the same double enter thing happen, I'm not sure why exactly it's doing it, but I replaced cin.ignore(256, '\n') with cin.sync() and it went away.
I replaced both cin.ignore(256, '\n') with cin.sync() and it ran straight through the display... the problem with that is I'm suppose to pause and display the next bit of info. after the enter key stroke(ignoring input).

BookCode 6128
Title Evil Under the Sun
Publisher Code PB
Type MYS
Price $4.45
Paperback Y

BookCode 6908
Title Using Microsoft Access
Publisher Code AW
Type CS
Price $20.50
Paperback Y

BookCode 7443
Title Carrie
Publisher Code MP
Type HOR
Price $6.75
Paperback Y

No more book records are in the requested file
(0 to return to menu)


It didn't wait between each line for a key stroke
It seems like you don't want the first cin.ignore(256, '\n' then. If you want it to pause before showing the first time, it should be before the do while loop.
if you are working on windows then a new line is a combination of \r\n!!!
\n only works on linux/unix
Topic archived. No new replies allowed.