将用户输入拆分为向量中的两个单词
Split user input into two words in a vector
基本上,我希望程序允许用户输入两个单词,然后将其分割成一个向量。例如,如果用户输入:"Hello world",程序将获取单词"Hello"answers"world",并将它们存储在一个向量中。问题是,我对矢量不是很有经验,所以我不知道从哪里开始。下面是我到目前为止的代码:
#include <iostream>
#include <vector>
using namespace std;
void command_case();
string userIn;
int x = 0;
int main()
{
while(userIn != "QUIT")
{
cout << "What shall I do?" << endl;
cin >> userIn;
cout << "Your raw command was: " << userIn << endl;
command_case();
}
}
void command_case()
{
vector<char> Search;
vector<string> command;
char space = ' ';
for(int i = 0; i < userIn.size(); ++i)
{
if(userIn[i] != space)
{
userIn[i] = toupper(userIn[i]);
}
if(userIn[i] == space)
{
Search.push_back(userIn[i]);
}
}
command.push_back(userIn);
cout << command[x] << endl;
}
如果我输入"Hello world",我得到这个:
What shall I do?
Hello world
Your raw command was: Hello
HELLO
What shall I do?
Your raw command was: world
WORLD
但我想找这个:
What shall I do?
Hello world
Your raw command was: Hello world
HELLO WORLD
谁能给我解释一下我做错了什么? 更改以下代码行
cin >> userIn;
getline(cin, userIn);
相关文章:
- 如何阅读两个单词,然后阅读C++中的一行?
- 用c++交换短语中单词的前两个字母
- 从文件 (C++) 输入两个单词字符串
- 通过单词列表将两个字符串移动以比较每个单词
- 如何在两个单词之间添加空格
- 如何识别输入的输入字符串在 c++ 中是两个还是多个单词
- 从C 中的两个字符串中找到唯一的常见单词
- 如何计算两个文件中字符单词和行的总数,并将它们相加以得出总数
- C++ 具有两个类的拼写检查程序;字典和单词
- 如何使用C 中的Regex在给定句子中的多个空格隔开的两个单词匹配
- 找出两个给定单词和字典之间最短的单词阶梯
- 从两个单词之间获取字符串
- 从文件中阅读句子,并将每个句子分成两个单词短语
- C++ 计算文件中两个单词之间的单词
- C++如何在两个单词之间获取字符串/字符
- 提取两个单词之间的域
- 使用boost::regex来匹配两个完整的单词
- 如何使用 getche() 计算短语的单词并忽略 C++ 中带有两个字符的单词
- 将用户输入的前两个单词分隔一个空格,然后存储剩余短语(C++)
- 试图创建一个函数,将两个大小不等的字符串向量中的单词连接在一起