cin>> not work with getline()

这一生的挚爱 提交于 2019-11-28 14:42:21

You still have a newline in the stream after cin>>age;, which is giving you an empty string for the name.

You could solve it by just adding another getline() call after getting the age and throwing away the result. Another options is to call cin.ignore(BIG_NUMBER, '\n');, where BIG_NUMBER is MAX_INT or something.

Bajinga

getline() won't work with an int, or any number for that matter. It is defined as such:

istream& getline (char* s, streamsize n );

istream& getline (char* s, streamsize n, char delim );

So, it takes in strings and char*'s; not digits.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!