【问题标题】:Separating input C++分离输入 C++
【发布时间】:2015-03-10 00:46:34
【问题描述】:

我正在用 C++ 制作一个文字冒险游戏。现在我得到这样的输入:

string word1, word2;
cin >> word1 >> word2;
parse(word1, word2);

一个示例输入可以是

goto store

现在,要退出,您必须输入 quit 和任何其他文本才能退出。

我怎样才能使输入由空格分隔,我可以判断第二个字符串是否为空。

更新

我尝试了第一个答案,我在 windows 上得到了这个错误:

The instruction at 0x00426968 referenced memory at 0x00000000. 
The memory could not be read.

Click OK to terminate the program.

【问题讨论】:

标签: c++ c++11 input io


【解决方案1】:
 #include <vector>
 #include <string>
 #include <sstream>
 std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
        std::stringstream ss(s);
        std::string item;
        while (std::getline(ss, item, delim)) {
            if(!item.empty()) //item not elems
            elems.push_back(item);
        }
        return elems;
    }


std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    split(s, delim, elems);
    return elems;
}

这个函数std::vector&lt;std::string&gt; &amp;split(const std::string &amp;s, char delim, std::vector&lt;std::string&gt; &amp;elems)可以用用户定义的分隔符分割一个字符串,并将每个字符串存储到一个向量中。注意如果分隔的字符串为空,它不会加入字符串数组.

这个函数std::vector&lt;std::string&gt; split(const std::string &amp;s, char delim)是最后一个函数包装器之一,所以你只需要传递两个参数,方便你使用。

像这样使用拆分功能:

string line;
getline(std::cin,line);
auto strings =split(line,' ');

检查字符串大小你可以知道有很多单词。 如果strings size 等于 1,那么你的第二个字符串是空的。

【讨论】:

  • @KevinBrown - 谢谢,等一下
  • 我收到此错误:0x00426968 处的指令引用了 0x00000000 处的内存。无法读取内存。单击“确定”终止程序。
  • 我的文件开头有using namespace std;
  • @theTechnoKid - 没关系,您的编译器会正确解决问题
【解决方案2】:

我认为这就是您要寻找的。我不知道解析是如何工作的,但这就是我解决问题的方法

string word1, word2;
cin >> word1;
if(word1 == "quit"){
    //quit code
}
else
    cin >> word2;

通过单独询问输入,您可以插入此 if 语句。 if 检查输入的第一个字符串是否为“quit”,忽略第二个部分,并运行退出代码。如果第一条输入不是“退出”,那么它将要求第二条输入。

【讨论】:

  • 感谢您的建议,我已将其更改为更有帮助
  • 这会让用户按两次回车。我希望他们能够在一行上写goto store
【解决方案3】:

使用这段代码,我将整个输入放在一个名为InputText 的字符串中,并在循环中逐个字符地分析它。我将字符存储在一个名为Ch 的字符串中,以避免在我将其声明为字符时显示ASCII 代码而不是我的字符的常见问题。我不断将字符附加到一个名为Temp 的临时字符串中。我有一个名为Stage 的int,它决定了我的临时字符串应该放在哪里。如果Stage 为1,那么我将其存储在word1 中,每当我到达第一个空间时,我将Stage 增加到2 并重置温度;因此现在开​​始存储在word2 中。如果有超过1 个空格,您可以在我的switchdefault 处显示错误消息。如果只有一个单词并且根本没有空格,您可以知道,因为我初始化了word2 = "",并且在循环之后它仍然保持这种状态。

string Ch, InputText, word1 = "", word2 = "", Temp = "";
unsigned short int Stage = 1;
cin >> InputText;
for(int i = 0; i < InputText.length(); i++){
    Ch = to_string(InputText[i]);
    if (Ch == " "){
        Stage++;
        Temp = "";
    }
    else{
        switch (Stage){
        case 1:
            Temp.append(Ch);
            word1 = Temp;
            break;
        case 2:
            Temp.append(Ch);
            word2 = Temp;
            break;
        default: //User had more than 1 space in his input; Invalid input.
        }
    }
}
if (word1 == "quit" && word2 == ""){
    //Your desired code
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    相关资源
    最近更新 更多