-2

I am trying to get rid of the comma and store the second word in secondWord, and then output secondWord.

my code:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main() 
{
    istringstream inSS;       
    string lineString;        
    string firstWord;         
    string secondWord;       
    int i;
    bool correct = false;

    cout << "Enter input string:" << endl;

    while (!correct) 
    {
        // Entire line into lineString
        getline(cin, lineString);

        // Copies to inSS's string buffer
        inSS.clear();
        inSS.str(lineString);

        // Now process the line
        inSS >> firstWord;

        // Output parsed values
        if (firstWord == "q")
        {
            cout << "q" << endl;
            correct = true;
        }

        inSS >> secondWord;
        if(secondWord[i] != ',')
        {
            cout<<"Error: No comma in string."<<endl;
        }

        else if (secondWord[i] == ',')
        {

            cout << "First word: " << firstWord << endl;
            cout << "Second word: " << secondWord << endl;
            cout << endl;
        }
    }

    return 0;
}

acceptable inputs: Jill, Allen Jill , Allen Jill,Allen

expected output

The code produces the comma as the second word, but I would like to get rid of the comma and space and out the second word.

Glovern
  • 9
  • 5
  • seems like i is uninitialized – lamandy Nov 30 '17 at 03:35
  • Anyway, you can look into this question: https://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c to learn about how to tokenize a string. – lamandy Nov 30 '17 at 03:39
  • If i is uninitialized, the code behavior will be undefined so anything could happen. Anyway, you should provide us with a sample input, `a, b`, `a , b`, `a,b` could produce very different results using your code. – lamandy Nov 30 '17 at 03:43
  • The problem is when you `inSS >> firstWord;` when your input is `Jill, Allen`, you take in the comma as well, so comma should never appear in your secondWord, thus the error. Go to the link I post earlier to learn how to tokenize string. – lamandy Nov 30 '17 at 03:56
  • I read tokenize, but we have not learned that in class yet, and have to stick to the covered material. – Glovern Nov 30 '17 at 03:59
  • You could change the whitespace delimiter to include comma. Then it will extract words without the comma. See https://stackoverflow.com/questions/7302996/changing-the-delimiter-for-cin-c – Jerry Jeremiah Nov 30 '17 at 04:27
  • You can implement a tokenizer based on what you have learnt, by using string.find_first_of and string.find_first_not_of – lamandy Nov 30 '17 at 05:32

1 Answers1

0
vector<string> tokenize(const string& line, const string& delimiters)
{
    int start = 0, end = 0;
    vector<string> result;
    while (end != string::npos)
    {
        start = line.find_first_not_of(delimiters, end);
        end = line.find_first_of(delimiters, start);
        if (start != string::npos && end != string::npos)
            result.push_back(line.substr(start, end - start));
        else if (start != string::npos)
            result.push_back(line.substr(start));
        else
            break;
    }
    return result;
}

This method returns the tokens separated by delimiters. If your delimiters is ", ", then you need additional check to ensure that there is comma in between. If your delimiters is ",", you will want to do some trimming of white spaces at the beginning and end of each word.

lamandy
  • 962
  • 1
  • 5
  • 13