c++ - 将多个字符串作为一个字符串读取? C++

标签 c++ string cin getline

<分区>

免责声明:我是 C++ 的新手; Java 是我的技能组合。

在我写的程序中,我需要比较两个字符串,如下:

#include<string>
#include<iostream>

using namespace std;

int main()
{
  string full_name = "John Doe";
  string find_name;

//User inputs "John Doe"

  cout << "Enter the name of the person to search for:" << endl;      
  **cin >> find_name;//THIS IS THE ISSUE I HAVE**

  if(find_name == full_name) //or some other compare function. NOT THE ISSUE.
      action_do_something;


return 0;
}

我知道缓冲区只接受“John”,而“Doe”是第二个不相关的命令。如何阻止缓冲区切断第二个名字? (有的名字有 5 个名字,有的只有 1 个)

我一直在研究 getline(),但我想我还没有完全理解它——它不等待输入就继续进行。

提前致谢!

最佳答案

使用标准函数std::getline。例如

std::getline( std::cin, find_name );

关于c++ - 将多个字符串作为一个字符串读取? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26623076/

相关文章:

c++ - 在内存映射文件时写入内存映射文件

c++ - (匿名)命名空间、函数和头文件

c++ - 为什么 cin 在包含字符串头后接受字符串输入

c++ - 使用 std::cin 中的 std::setw 限制输入大小

c++ - 使用模板和多个构造函数

c++ - 在文本字段中设置插入符位置 (Win32API)

java - 如何在ScrollView的底部添加字符串

c# - 深入了解 .NET 框架中的 EqualsHelper 方法

c++ - 检查字符串中的唯一字符

c++ - 为什么 stringstream >> 在失败时更改目标值?