相当于c ++中的Console.ReadLine() [英] equivalent of Console.ReadLine() in c++

查看:175
本文介绍了相当于c ++中的Console.ReadLine()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的老师刚刚给了我一个在c ++中的任务,我试图得到一个字符串与scanf,但它只获取最后输入的字符。任何人可以帮助我吗?我在c ++中寻找相当于console.readline()。



编辑:我还必须能够通过指针存储值。



所以图片显示的代码当前runnign在后台,它应该已经停止了无保证maladie:并等待输入,但它跳过它。



getline(cin,ptrav-> nam);但是它因为某种原因跳过一行...

解决方案

您正在寻找 std :: getline()。例如:

  using namespace std; 
....
string str
getline(cin,str);

我不知道你的意思是什么,当你说我也必须能够存储

更新:查看您更新的问题,我可以想象发生了什么。读取选项的代码(即数字1,2等)不会读取换行符。然后调用 getline ,这会占用换行符。然后再次调用 getline 来获取字符串。


My teacher just gave me an assignment in c++ and I am trying to get a string with scanf but it only get the last characters typed. Can anyone help me please? I am looking for the equivalent of console.readline() in c++.

edit : I must also be able to store the value through a pointer.

so the picture show the code currently runnign in the background and it should have stoped at No assurance maladie : and waited for an input but it skipped it.

getline(cin, ptrav->nam); works but it skip a line for some reason...

解决方案

You are looking for std::getline(). For example:

using namespace std;
....
string str;
getline(cin, str);

I've little idea what you mean when you say I must also be able to store the value through a pointer.

Update: Looking at your updated question, I can imagine what is happening. The code that reads the choice, i.e. the number 1, 2, etc. is not reading the newline. Then you call getline which consumes the newline. And then you call getline again which fetches the string.

这篇关于相当于c ++中的Console.ReadLine()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆