抛砖引玉我先去掉了前面两个正常运行的fragment,单独测试最后一个,程序正常。因此怀疑是standard in部分的问题。原因是上面输入1,2的以后有个enter,这个'\n'被存在了standard input stream里面,因此getline首先search到的是这个enter,也就是说end of line,自然不会继续读取input了。
为了验证这个猜想,我把getline的parameter改成了(str,100,' '),也就是说用space来结束,程序正常。
遗憾的是用c里面的fflush(stdin)没有用,不知道是不是VC++的问题,那位在unix上面试试看?
另外,我改成cin>>str,结果正常。
有不对之处,盼指正啊!
Yes, it is caused by the return character.
To ignore the return character, you can add the following line in you code before input.
cin.ignore();
This line is to remove the return character from the std input stream.
I happened to have tried this today.
Any comment is welcome.
cin.ignore();
This line is to remove the return character from the std input stream.
I happened to have tried this today.
Any comment is welcome.