cin.getline() error

If i set "n" on any value, that for{} allows me to set only n-1 values for buffer. Can anyone explain to me what's wrong? Thanks.
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
header:
struct Element
{
	char *name;
	Element *succ;
};

typedef Element* Head;
void InsertFront(Head &l, char *sir);
void Print(Head l);

main:
#include<iostream>
#include"Jospeh_header.h"
#include<conio.h>

using namespace std;

int main(void)
{
	Head L=nullptr;
	int n;
	cout<<"Number of players: ";
	cin>>n;
	char buffer[100];
	cout<<"Payers names:\n";
	
	for(int i=0;i<n;i++)
	{
		cin.getline(buffer,99);
		InsertFront(L,buffer);	
	}

	Print(L);

	_getch();
	return 0;

}
Last edited on
there is a newline left in the buffer after operator >> on cin you must ignore that newline before calling cin.getline since cin.getline reads until the newline.
Problem solved. Thanks
Topic archived. No new replies allowed.