从行首开始,以空格结束的输入
String Input starting at start of a line and terminates with space
如何在c++中像下面这样接受字符串输入:从行首开始,在第一个空格处结束,忽略该行第一个空格旁边的内容,对于第二个字符串,则转到下一行,输入来自标准输入
的例子:
this is a sample input
here are few lines
looknospaces!
字符串数组应该包含
string[0]=this
string[1]=here
string[2]=looknospaces!
可能一个好的方法是使用getline然后提取每行中的第一个单词,否则我不太喜欢的解决方案是(ab)使用getline分隔符
#include <string>
#include <iostream>
int main()
{
std::string name, discard;
std::getline(std::cin, name, ' '); // Takes just the first word (if present)
std::getline(std::cin, discard); // Takes the rest until n, discard this
,然后你可以加入你想要的
相关文章:
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 为什么我的程序在输入某个形状的面积的测量值后没有结束?
- 在Visual Studio中运行/调试C ++时,如何在结束时等待输入
- 确定用户字符输入 C++ 的结束
- 如何使用Chrono或ctime libaray输入设置的开始和结束时间
- 通过终端在文件中输入时检测EOF(文件结束)时出现问题
- 使用分隔符结束用户输入的循环
- 程序的执行在 C++ 中输入 char 变量后结束
- 虽然带有 getline 的循环不会因用户输入而结束
- C++程序在用户输入之前结束
- 根据特定的用户输入量结束 while 循环
- 如果用户想要输入 x 数量的数字,我如何设法跟踪它然后结束程序?
- 用户输入以结束关闭控制台
- C 读取输入,直到线char数组的结束
- 如何在输入结束时修复预期}错误
- C 链接的列表用户输入限制验证导致程序结束
- 是否有输入类型在输入一定数量的字符后自动结束
- 输入文件读取错误的值,eof 控制循环不会结束
- 文件结束输入不起作用
- C++ getline() 不结束输入