Comparing last word in line (added to vector each word as different element) with typing word C++

80 views Asked by At

I'm making one of my firsts programs in cpp. In short, small game. Rule of that round is to choose random word from line, replace it with "...", ask for typing word and check if it is the same word as the word before replacing. I have problem only in situation the last word is replaced. Even if I write the same word, I get answer it's not alright. Checked it on debug and I can not see ant different between replacing last word or any other word. As I wrote, not working only with last word from the line.

Logic: Read lines from file, add them to vector. Choose one random line, add word by word to next vector erasing all spaces, choose random word, replace, print sentence, type, compare.

 vector<string> thirdRoundSentences;
 vector<string> arrayWithOneLineStrings;
 string chosenLine;
 string wholeSentenceLine;
 string delimiter;
  ifstream fileRoundThree("../../langGame/roundThree.txt");

    while (getline(fileRoundThree, line)) {
        thirdRoundSentences.push_back(line);
    }
    fileRoundThree.close();
}
 srand(time(0));
    random = rand() % thirdRoundSentences.size();

    wholeSentenceLine = thirdRoundSentences[random];
    chosenLine = wholeSentenceLine;
    delimiter = " ";

    size_t pos = 0;

    while ((pos = wholeSentenceLine.find(delimiter)) != std::string::npos) { 
        token = wholeSentenceLine.substr(0, pos);
        wholeSentenceLine.erase(0, pos + delimiter.length());  //erase word one by one from whole sentence
        arrayWithOneLineStrings.push_back(token);
    }
    arrayWithOneLineStrings.push_back(wholeSentenceLine); //add last left word to vector

    srand(time(0));
    random = rand() % arrayWithOneLineStrings.size();

    token = arrayWithOneLineStrings[random];

    if (chosenLine.find(token) != string::npos) {
        chosenLine.replace(chosenLine.find(token), token.size(), "...");
    }

    cout << "\n" << chosenLine<< "\n";
    cin >> typeWord;
    arrayWithOneLineStrings.clear();

    if (typeWord == token) {
        scorer.addPoints();
    } else {

This is what I'm talkin about. Print

Piece of ...
cake  //typed word

 Wrong!

 Right answer is: cake  
0

There are 0 answers