获得与 C++ 良好合作的空间输入流

发布于 2024-09-29 09:02:23 字数 542 浏览 0 评论 0原文

首先考虑这个示例 C++ 代码:

std::string input1, input2, input3;
std::cout << "Enter Input 1: ";
std::cin >> input1;
std::cout << std::endl << "Enter Input 2: ";
std::cin >> input2;
std::cout << std::endl << "Enter Input 3: ";
std::cin >> input3;

如果对于 input1,我输入类似“Good day Neighbors”的内容,则 input1 设置为“Good”,input2 设置为“day”,输入 3 设置为“neighbors”。我什至没有机会为 input2 和 input3 设置值。

所以我的问题是:如何将包含空格的文本字符串输入到单个字符串中,而不使其(由于缺乏更好的术语)分解并溢出到对输入流的后续调用中?

预先感谢收到的所有答复。

First consider this sample C++ code:

std::string input1, input2, input3;
std::cout << "Enter Input 1: ";
std::cin >> input1;
std::cout << std::endl << "Enter Input 2: ";
std::cin >> input2;
std::cout << std::endl << "Enter Input 3: ";
std::cin >> input3;

If for input1 I enter something like "Good day neighbors" then input1 is set to "Good", input2 is set to "day" and input 3 is set to "neighbors". Im not even given the opportunity to set values for input2 and input3.

So my question is: How can I input a string of text that include spaces into a single string without it (for lack of better terminology) breaking up and overflowing into subsequent calls to the input stream?

Thanks in advance to any and all answers received.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尝蛊 2024-10-06 09:02:23

您可以使用 std::getline :

std::getline(std::cin, input1);
...
std::getline(std::cin, input2);
...
std::getline(std::cin, input3);

You can use std::getline:

std::getline(std::cin, input1);
...
std::getline(std::cin, input2);
...
std::getline(std::cin, input3);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文