我制作了这个程序,它获取用户的地址、姓名和工作。然后将它们组合成一个字符串并输出该字符串。(我知道有更好的方法来做到这一点)
char str[600];
char adrs[200];
char name[10];
char wrk[200];
cout<<"Enter your name and press ENTER: ";
cin.getline(name,10);
cout<<"\nEnter your adress and press ENTER:";
cin.getline(adrs,200);
cout<<"\nEnter your workplace and press ENTER:";
cin.getline(wrk,200);
strcpy(str,"My name is ");
strcat(str,name);
strcat(str,"\nand i live at ");
strcat(str,adrs);
strcat(str, "\nI also work at ");
strcat(str, wrk); strcat(str, "\n\n");
cout<<str<<endl;
当我输入一个超过10个字符的名称时,程序确实按照我的预期使用用户输入的前9个字符,但在此之后,它跳过了程序中所有下一个
cin.getline()
,直接输出str
并结束程序。为什么会这样?怎样解决这个问题?
100
,但却写成了10
? - DGHgetline
,使用自由函数std::getline
和std::string
。 - Kerrek SB