Naphstor
Naphstor

Reputation: 2496

getline() omitting the first letter of my output string

Hi i am a newbie in c++ and was doing some basic exercise. my code takes user input and feeds them in an array. Now i am using getline() to get the input string. my code is as follows:

cin.getline(cb[0].name, 200).get();        // Cadburry
cin.getline(cb[1].name, 200).get();        // Snickers
cin.getline(cb[2].name, 200);              // Milky Bar

But when i output the strings, the first getline() seems to be fine but the other two are omitting the first letter of the string. So the output in this case is :

Cadburry
nickers
ilky Bar 

can anyone please tell me why is it so?

Upvotes: 2

Views: 2212

Answers (1)

Jesus Ramos
Jesus Ramos

Reputation: 23266

The get() calls are consuming the S and the M, remove those and it will work. getline() already consumes the \n

Upvotes: 5

Related Questions