Can someone explain what cin.getline() does?

I've read the page on cin.getline(); in my book as well as the cplusplus references page.

But someone recently fixed part of my program so I would be able to input without the output skipping a line.

My question is how do I fix that problem in the rest of my code to let it work?
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
char AiDee[8], AitemName[20], AitemProducer[20];
int number;
     cout<<"Enter ID: ";
     cin >> AiDee;
     cin.ignore(1);// This is what someone told me to add before to make it run the way I want it to
     cout<<"Enter Item Name: ";
     cin.getline(AitemName,20);
     cout<<"Enter Producer: ";
     cin.getline(AitemProducer,20);
     cout<<"Enter Quantity: ";
     cin>>number;   
     createItem(inv.items[inv.total], AiDee, AitemName, AitemProducer,number);
     inv.total ++; 

This code was fixed so I my output wouldn't be
Enter Item Name: Enter Producer:


Now I tried using the same method here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 int find = 0;
    char nameofItem[20];
    cout<<"Enter search name: ";
   // cin>> nameofItem;
    cin.getline(nameofItem, 20);
    cin.ignore(1);
    for (int k = 0; k < inv.total ; k++){
    if (strcmp(nameofItem, inv.items[k].ProductName)) ;     
     else{find = 1;
    cout<<"Record #"<<k<<endl;
     cout<<"  ID: "<<inv.items[k].ID<<endl;
     cout<<"  Item Name: "<<inv.items[k].ProductName<<endl;
     cout<<"  Producer: "<<inv.items[k].Producers<<endl;
     cout<<"  Quantity: "<<inv.items[k].Quantity<<endl<<endl;}   
     }     
     if (find != 1) cout<<"Name not found"<<endl<<endl;


but if I search in my struct if that name is there, I get an endless loop (regardless if it's one or two words)
and I need to be able to type in the
words with spaces therefore I don't think I can use cin.

So what do I do to fix this? And how does it work?
Last edited on
Can you give me an example. i don't understand .

Here is your code have a mistake
if (strcmp(nameofItem, inv.items[k].ProductName)) ; ( ;?)



example:
int k=10;
if(k>10);
{
cout<<"Greater than 10";
}

output : Greater than 10 :(
Last edited on
Topic archived. No new replies allowed.