gets()函数行为

时间:2016-10-11 16:29:36

标签: c++ string gets

在此代码段中:

char name[25];
int a;
cout<<"Enter number: ";
cin>>a;
cout<<"Enter name: ";
gets(name);

在我甚至可以给字符串名称[]任意值之前,它会自动跳过它。 但是当我改变代码并将字符串输入放在整数输入之前时,

char name[25];
int a;
cout<<"Enter name: ";
gets(name); 
cout<<"Enter number: ";
cin>>a;

它完美无缺。为什么gets()函数的行为如此? 我可以使用任何其他字符串流功能作为替代它吗?

0 个答案:

没有答案