During correction of mates homework (OpenClassroom) I encountered this strange problem.
The string that was read from a dictionary file (1 column 300000 lines of words its too big to put it here but to give you an idea
...
ABAISSAIS
ABAISSAIT
ABAISSAMES
ABAISSANT
...
)
with getline wasn't appearing in the output (line 70)
actual | shuffledmysteryWord | userInput
expected mysteryWord | shuffledmysteryWord | userInput
I tried with reconstituted string
for (int i=0; i<(motMystere.size()-1); i++) motMystere1 += motMystere[i];
and it works as expected so its not empty its perfectly readable maybe containing newline (cause getline) string
There is a lot of other things that can/might be corrected but
- It isn't my code
- I am just curious about the string
Code
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <fstream>
using namespace std;
string melangerLettres(string mot)
{
string melange;
int position(0);
int wordSize=mot.size();
while ( wordSize > 1 )
{
if ( wordSize > 2 ) { position = rand() % (wordSize-1); }
else if ( wordSize == 2 ) { position = 0; }
melange += mot[position];
mot.erase(position, 1);
wordSize--;
}
return melange;
}
int main(void)
{
int compteur(0);
string motMystere, motMelange, motUtilisateur, motMystere1, ligne;
srand(time(0));
ifstream dico("dico.txt");
if (dico)
{
while (getline(dico, ligne))
{
++compteur;
}
dico.clear();
dico.seekg(0, ios::beg);
int nrandom = rand() % compteur;
for (unsigned int i = 0; i < nrandom; ++i)
{
getline(dico, ligne);
}
motMystere = ligne;
}
else
{
cout << "Erreur : lecture du fichier impossible\n";
return 1;
}
motMelange = melangerLettres(motMystere);
// dont know why but motMystere is just broken
//~ for (int i=0; i<(motMystere.size()-1); i++) motMystere1 +=
//~ motMystere[i];
cin >> motUtilisateur;
cout << motMystere << " | " << motMelange << " | " << motUtilisateur
<< "\n";
return 0;
}