C++猜单词的游戏
可能有点帮助
猜单词
#include
#include
#include
#include
using namespace std;
class wordobj
{
public:
wordobj(){};
~wordobj(){};
bool getWord();
void selectWord();
const string& getSelWord();
bool isRight(char ch, int index);
private:
vector svec;
int _size;
string _selectedWord;
char ch;
};
bool wordobj::getWord()
{
_size = 0;
ifstream infile("txt.txt");
if (!infile)
{
cout <
return false;
}
else
{
string word;
while (infile >> word)
{
svec.push_back(word);
}
_size = svec.size();
return true;
}
}
void wordobj::selectWord()
{
srand(_size);
int index = rand() % _size;
_selectedWord = svec[index];
cout <
for (int i = 0; i != _selectedWord.size(); ++i)
{
cout <
}
cout <
}
bool wordobj::isRight(char ch, int index)
{
return ch == _selectedWord[index];
}
const string& wordobj::getSelWord()
{
return _selectedWord;
}
int main()
{
wordobj wdj;
wdj.getWord();
wdj.selectWord();
int count = 0;
char ch;
cout <
while (count
{
cin >> ch;
if(!wdj.isRight(ch, count))
{
cout <
continue;
}
else
{
if ((count + 1) == wdj.getSelWord().size())
{
break;
}
cout <
count ++;
}
}
cout <
return 0;
}
C++大学教程中有一个掷骰子的程序