0

I am making a text adventure game in C++. Right now I am getting the input like this:

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

An example input could be

goto store

Right now, to quit you would have to type quit and any other text to quit.

How can I make it so the input is separated by a space and I can tell if the second string is empty.

UPDATE

I tried the first answer, and i get this error on windows:

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

Click OK to terminate the program.
Sheikh
  • 539
  • 1
  • 7
  • 29

3 Answers3

0

I think this is what you're looking for. I don't know how parsing works, but this is how I would solve the problem

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

By asking for the input separately, you are able to insert this if statement. This if checks to see if the first string of input is "quit", ignores the second piece, and runs the quit code. If the first piece of input is not "quit", it will then ask for the second piece of input.

Towell
  • 180
  • 1
  • 7
0
 #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;
}

This function std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) can split a string with an user-define delimiter,and stores each string to a vector.Note that if a separated string is empty, it will not join the string array.

This function std::vector<std::string> split(const std::string &s, char delim)is one of the last function wrapper, so you only need to pass two parameters, convenient for you to use.

use split function like this:

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

check strings size you could know many words there are. If strings size equals 1,so your second string is empty.

Ron Tang
  • 1,532
  • 12
  • 20
-2

Using this code, I take the whole input in one string called InputText and analyze it char by char in a loop. I stored the chars in a string called Ch to avoid common issues of it showing the ASCII code instead of my char when I declare it as a char. I keep appending chars to a temp string called Temp. I have an int called Stage which decides where my temp string should go. If the Stage is 1 then I store it in word1, whenever I reach the first space I increase the Stage to 2 and reset the temp; therefore starts storing in word2 now.. In case if there were more than 1 space, you can show an error message at the default of my switch. In case there was only one word and no spaces at all, you can know since I initialized word2 = "" and it remains that way after the loop.

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
}
Remon Ramy
  • 167
  • 1
  • 11