相当于 C++ 中的 Console.ReadLine()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12806450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:38:36  来源:igfitidea点击:

equivalent of Console.ReadLine() in c++

c++stringscanfreadline

提问by AntoineLev

screenshotMy 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++.

截屏我的老师刚刚给了我一个 C++ 作业,我试图用 scanf 获取一个字符串,但它只获取输入的最后一个字符。有人可以帮我吗?我在 C++ 中寻找相当于 console.readline() 的东西。

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.

所以图片显示了当前在后台运行的代码,它应该停在 No Guarantee maladie : 并等待输入但它跳过了它。

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

getline(cin, ptrav->nam); 有效,但由于某种原因它跳过了一行......

回答by David Heffernan

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

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

#include <string>
std::string str;
std::getline(std::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 getlinewhich consumes the newline. And then you call getlineagain which fetches the string.

更新:看着你更新的问题,我可以想象发生了什么。读取选项(即数字 1、2 等)的代码不是读取换行符。然后你调用getline它消耗换行符。然后你getline再次调用它获取字符串。

回答by rubber boots

According to MSDN, Console::ReadLine:

根据MSDN, Console::ReadLine

Reads the next line of characters from the standard input stream.

The C++-Variant (no pointers involved):

C++-Variant(不涉及指针):

#include <iostream>
#include <string>

 int main()
{
 std::cout << "Enter string:" << flush;
 std::string s;
 std::getline(std::cin, s);
 std::cout << "the string was: " << s << std::endl;
}



C-Variant(带有缓冲区和指针)也适用于 C++ 编译器,但不应使用:

 #include <stdio.h>
 #define BUFLEN 256

 int main()
{
 char buffer[BUFLEN];   /* the string is stored through pointer to this buffer */
 printf("Enter string:");
 fflush(stdout);
 fgets(buffer, BUFLEN, stdin); /* buffer is sent as a pointer to fgets */
 printf( "the string was: %s", buffer);
}



根据您的代码示例,如果您有一个结构patientpatient(在 David hefferman 的评论后更正):

struct patient {
   std::string nam, nom, prenom, adresse;
};

Then, the following should work (added ios::ignoreafter additional problem has been solved by DavidHeffernanby logical thinking). Please DO NOTuse scanfin your code AT ALL.

然后,以下应该起作用(ios::ignoreDavidHeffernan通过逻辑思维解决了其他问题后添加)。请不要使用scanf在你的代码在所有

...
std::cin.ignore(256); // clear the input buffer

patient *ptrav = new patient;

std::cout << "No assurance maladie : " << std::flush;
std::getline(std::cin, ptrav->nam);
std::cout << "Nom : " << std::flush;
std::getline(std::cin, ptrav->nom);
std::cout << "Prenom : " << std::flush;
std::getline(std::cin, ptrav->prenom);
std::cout << "Adresse : " << std::flush;
std::getline(std::cin, ptrav->adresse);
...