0

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  
MichalWds
  • 13
  • 7
  • Is there a newline hiding at the end of your string (that then gets pulled into your `arrayWithOneLineStrings`)? Without seeing how you build `thirdRoundSentences`, we can't say. – scohe001 Jan 03 '20 at 21:22
  • 1
    Please provide a [mre], at a guess you are reading your lines from a file and the newlines in your file don't match the native newlines of your platform so there are stray carriage returns at the end of your lines – Alan Birtles Jan 03 '20 at 21:25
  • @AlanBirtles that was my first tought, but there is no dots or any other char/signs after the last word. Anyway, I edited post – MichalWds Jan 03 '20 at 21:35
  • Make it compileable. It's much easier for people to help with a [mcve]. – Ted Lyngmo Jan 03 '20 at 22:46
  • Still not a [mre]. Try removing your vector of lines and fill `chosenLine` with a hard coded value, does it still fail? – Alan Birtles Jan 03 '20 at 22:50
  • i figure this out. After last word I had '\r' automaticly added with word. That was the problem. And I will remember for the future to put compilable pieces of code. – MichalWds Jan 04 '20 at 11:10

0 Answers0