我正在用 cpp 制作我的第一个程序。总之,小游戏。该轮的规则是从一行中随机选择一个单词,将其替换为“...”,要求输入单词并检查它是否与替换前的单词相同。我只有在最后一个词被替换的情况下才有问题。即使我写同一个词,我得到的答案也不好。在调试时检查了它,我看不出替换最后一个词或任何其他词之间的区别。正如我所写,不仅仅使用该行的最后一个字。
逻辑:从文件中读取行,将它们添加到向量中。选择一个随机行,逐字添加到下一个向量擦除所有空格,选择随机单词,替换,打印句子,键入,比较。
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 {
这就是我所说的。打印
Piece of ...
cake //typed word
Wrong!
Right answer is: cake