getline(); Error

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
void LList::TitleS(int counting){
        //PRE:
        //POST:

        string key = "blank";
        int position;
        string use;
        while(key != "exit" && key != "EXIT"){
                bool found = false;
                cout << "Enter the title to search for or type (exit) to return ";
                cout << "to search menu: ";
                getline(cin, key);
                if(key != "exit" && key != "EXIT"){
                        listnode * temp;
                        temp = head;
                        for(int i = 0; i < counting && !found; i++){
                                use = temp->Ltitle;
                                if(use != key)
                                        temp = temp->next;
                                else{   //if(use == key)
                                        found = true;
                                        position = 1;
                                }
                        }
                        if(found)
                                Display(position);
                        else
                                cout << "Book Title NOT found" << endl;
                }
                else
                        ;
        }
}



Using the code above I'm recieving some weird occurances in my execution. For some reason it's reading in before I input any information. What happens is below.


Search for book code (b) or title (t) or enter (end) to end the program: t
Enter the title to search for or type (exit) to return to search menu: Book Title NOT found
Enter the title to search for or type (exit) to return to search menu:
Last edited on
I think this is the problem of getline. Flush the buffer before taking another input.
I had to use this in a program where I had a getline and then I had to use another getline directly after that. I only see one getline in your code, but if I missed another getline in there here is some code. Also, the variable "use" is not initialized anywhere which may cause some conflict.

1
2
cin.ignore();		//  Needed in order to allow new input.
getline(cin, key);
I knew that I was forgetting ignore for some reason... Thanks man. Much needed.

Also I assigned 'use' before I used it. Location being line 17 above.

use = temp->Ltitle;

Also I did notice that I had to correct

1
2
3
4
                                else{   //if(use == key)
                                        found = true;
                                        position = 1;
                                }


to ->

1
2
3
4
                                else{   //if(use == key)
                                        found = true;
                                        position = i;
                                }
cin.ignore(); // Needed in order to allow new input.


While this is true, but you cannot use it everytime before getline
Topic archived. No new replies allowed.