0

How does one parse a single string such as "ducks hollow23 !" into three different tokens. This string is read into a single string called input and needs to be checked if it is valid. Basically, the function is similar to this:

int inputString(string name, string keyWord, bool trueFalse){
     string input;
     cin >> input;

     // input = "ducks hollow23 !"

     // how to I put "ducks hollow23 !" into name, keyWord, trueFalse and then 
     // check if name is a valid name and if keyWord is valid or if not needed, and trueFalse
     // is valid

}

3 Answers3

0

An example code snippet given below:

char str[] = input.c_str(); // e.g. "ducks hollow23 !"
char * pch;
pch = strtok (str," ");
string str[3];
int i = 0;
while (pch != NULL) {
  if (i > 2)
    break; // no more parsing, we are done
  str[i++] = pch;
  pch = strtok(NULL, " ");
}
// str[0] = "ducks", str[1] = "hollow23", str[2] = "!"
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
0

your example is not fair - string will be just "ducks" in your setting, which you can interpret as name other >> will give you more tokens. Also you need references & to get values back

int inputString(string& name, string& keyWord, bool& trueFalse) {
    string flag;
    cin >> input >> keyWord >> flag;
    trueFalse = (flag == "!");
}
Peter K
  • 1,787
  • 13
  • 15
0

The easiest way is probably to use istringstream.

I'm not sure what you consider valid input so the only error checking I've used is that the istringstream is in a good state.

I've modified inputString() to take the full input string, which you would get from cin.

#include <iostream>
#include <sstream> // for std::istringstream
using namespace std;

// Note call by reference for the three last parameters
// so you get the modified values
int inputString(string input, string &name, string &keyWord, bool &trueFalse){
    std::istringstream iss(input); // put input into stringstream

    // String for third token (the bool)
    string boolString;

    iss >> name; // first token

    // Check for error (iss evaluates to false)
    if (!iss) return -1;

    iss >> keyWord; // second token

    // Check for error (iss evaluates to false)
    if (!iss) return -1;

    iss >> boolString; // third token

    // Check for error (iss evaluates to false)
    if (!iss) return -1;

    if (boolString == "!") trueFalse = false;
    else trueFalse = true;

    return 0;
}

int main() {
    string input, name, keyWord;
    bool trueFalse;
    //cin << input;

    // For this example I'll just input the string
    // directly into the source
    input = "ducks hollow23 !";

    int result = inputString(input, name, keyWord, trueFalse);

    // Print results
    cout << "name = " << name << endl;
    cout << "keyWord = " << keyWord << endl;

    // Use std::boolalpha to print "true" or "false"
    // instead of "0" or "1"
    cout << "bool result = " << boolalpha << trueFalse << endl;

    return result;
}

The output is

name = ducks
keyWord = hollow23
bool result = false
Null
  • 1,950
  • 9
  • 30
  • 33