So I've written a program which picks a word in a list of words, masks it by switching its letters and ask the user to find the starting word in less than 5 tries. here's the main.cpp file:
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
#include "mystere.h"
using namespace std;
int main(int argc, char const *argv[])
{
srand(time(0));
char again('Y');
vector<int> scores;
string secret(""),mystere(""),userInput("");
do{
int tries(0);
secret=choixMot("dico.txt"); //The file I'm talking about is right there
mystere=masque(secret);
bool comparaison;
do{
cout << "What is this word? " << mystere << endl; //Shows the masked word to the user
cin >> userInput;
toUpper(userInput);
comparaison=compare(userInput,secret);
verification(comparaison);
tries++;
}while(!comparaison && tries<5);
if(tries==5)
cout << "Lost! the solution was: " << secret << endl;
cout << "Wanna try again?" << endl;
cin >> again;
toUpper(again);
scores.push_back(tries);
}while(again=='Y');
if(again=='N'){
cout << "Average number of tries: " << moyenne(scores) << endl;
cout << "Bye and thanks for playing." << endl;
}
else
cout << "Unrecognized character, logging you out." << endl;
return 0;
}
here's the mystere.cpp file:
#include "mystere.h"
bool compare(string const& mot1, string const& mot2){
if(mot1.size()!=mot2.size()){
return false;
}else{
for(int i(0); i<mot1.size(); i++){
if(mot1[i]!=mot2[i]) return false;
}
return true;
}
}
string masque(string secret){
string masque;
int indice(0);
while(secret.size()>0){
indice=rand()%secret.size();
masque.push_back(secret[indice]);
secret.erase(indice,1);
}
return masque;
}
void toUpper(string& chaine){
for(int i(0);i<chaine.size();i++){
if(chaine[i]>96 && chaine[i]<123)
chaine[i]-=32;
}
}
void toUpper(char& charactere){
if(charactere>96 && charactere<123)
charactere-=32;
}
void verification(bool comparaison){
if(comparaison)
cout << "Congrats! " << endl;
else
cout << "This is not the word" << endl;
}
double moyenne (vector<int> const& scores){
double moyenne(0),taille(scores.size());
for(int i(0);i<taille;i++)
moyenne+=scores[i];
return(moyenne/taille);
}
// Mot means "Word" in english for those wondering
string choixMot(string const nomFichier){
string Mot("");
int nbrMots(0),indice(0);
ifstream fichier(nomFichier.c_str());
if(fichier){
//cout << fichier.tellg();
while(getline(fichier,Mot)){
nbrMots++;
}
fichier.clear(); //to clear the eof bit and allow seekg to escape from the end of the file
fichier.seekg(0,ios::beg);
getline(fichier,Mot);
indice=rand()%nbrMots+1;
for(int i(0);i<indice;i++)
getline(fichier,Mot);
}else
cout << "Impossible to open the file in reading mode" << endl;
return Mot;
}
And here is the mystere.h file:
#ifndef MYSTERE
#define MYSTERE
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <fstream>
using namespace std;
bool compare(string const& mot1, string const& mot2);
string masque(string secret);
void verification(bool comparaison);
void toUpper(string& chaine);
void toUpper(char& charactere);
double moyenne (vector<int> const& scores);
string choixMot(string const nomFichier);
#endif
finally, here is the Test.txt file:
TABLE
BATEAU
CHAUSSURES
BOUTEILLE
MONTRE
TELEPHONE
ORDINATEUR
ECOUTEURS
ECRAN
CHAISE
When I build the program replacing "dico.txt" in the main function by "Test.txt", there is no problem and the program works perfectly well. But when I use the "dico.txt" file which is accessible here and that I won't put here because it is around 30000 lines. The program builds perfectly but when it runs I see this:
XAELOINHNULs word ?
so the cout is not working anymore. I want to understand the problem and where it comes from as it never happens with the smaller file. Thanks a lot to those who'll take time to read and even test my program!