I have made this program, It get the users adress, name and work. Then It puts it all into one string and outputs that string. (I know there are better ways to do this)
char str[600];
char adrs[200];
char name[10];
char wrk[200];
cout<<"Enter your name and press ENTER: ";
cin.getline(name,10);
cout<<"
Enter your adress and press ENTER:";
cin.getline(adrs,200);
cout<<"
Enter your workplace and press ENTER:";
cin.getline(wrk,200);
strcpy(str,"My name is ");
strcat(str,name);
strcat(str,"
and i live at ");
strcat(str,adrs);
strcat(str, "
I also work at ");
strcat(str, wrk); strcat(str, "
");
cout<<str<<endl;
Here when I write a name that exceeds 10 chars The program does take the first 9 chars the user entered as I expected but after that It skips all the next cin.getline()
That are in the program and goes to output str
and ends the program.
Why does this happen and how to fix it?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…