#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class wordobj
{
public:
wordobj(){};
~wordobj(){};
bool getWord();
void selectWord();
const string& getSelWord();
bool isRight(char ch, int index);
private:
vector<string> svec;
int _size;
string _selectedWord;
char ch;
};
bool wordobj::getWord()
{
_size = 0;
ifstream infile("txt.txt");
if (!infile)
{
cout << "open file failed" << endl;
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 << "the selected word is : ";
for (int i = 0; i != _selectedWord.size(); ++i)
{
cout <<"*";
}
cout << endl;
}
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 << "please enter a character : ";
while (count < wdj.getSelWord().size())
{
cin >> ch;
if(!wdj.isRight(ch, count))
{
cout << "wrong, repeat entering this charactr /n (press '0' to quit) : ";
continue;
}
else
{
if ((count + 1) == wdj.getSelWord().size())
{
break;
}
cout << "enter the next character : ";
count ++;
}
}
cout << "successful!" << endl;
return 0;
}
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class wordobj
{
public:
wordobj(){};
~wordobj(){};
bool getWord();
void selectWord();
const string& getSelWord();
bool isRight(char ch, int index);
private:
vector<string> svec;
int _size;
string _selectedWord;
char ch;
};
bool wordobj::getWord()
{
_size = 0;
ifstream infile("txt.txt");
if (!infile)
{
cout << "open file failed" << endl;
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 << "the selected word is : ";
for (int i = 0; i != _selectedWord.size(); ++i)
{
cout <<"*";
}
cout << endl;
}
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 << "please enter a character : ";
while (count < wdj.getSelWord().size())
{
cin >> ch;
if(!wdj.isRight(ch, count))
{
cout << "wrong, repeat entering this charactr /n (press '0' to quit) : ";
continue;
}
else
{
if ((count + 1) == wdj.getSelWord().size())
{
break;
}
cout << "enter the next character : ";
count ++;
}
}
cout << "successful!" << endl;
return 0;
}