基本上,我希望程序允许用户输入say,2个单词,然后将其拆分为向量。就像,如果用户输入:“Hello world”,程序将采用“Hello”和“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
有人能向我解释我做错了吗?
答案 0 :(得分:1)
更改以下代码行
cin >> userIn;
要
getline(cin, userIn);