cin.getline() error
Nov 18, 2013 at 10:38pm UTC
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 Nov 18, 2013 at 11:08pm UTC
Nov 19, 2013 at 3:38am UTC
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.
Nov 19, 2013 at 7:39am UTC
Problem solved. Thanks
Topic archived. No new replies allowed.