Во время исправления домашнего задания товарищей (OpenClassroom) я столкнулся с этой странной проблемой.
Строка, которая была прочитана из файла словаря (1 столбец 300000 строк слов, слишком велика, чтобы поместить ее здесь, но чтобы дать вам представление
...
ABAISSAIS
ABAISSAIT
ABAISSAMES
ABAISSANT
...
)
с getline
не появилось на выходе (строка 70)
actual | shuffledmysteryWord | userInput
expected mysteryWord | shuffledmysteryWord | userInput
Я пытался с восстановленной строкой
for (int i=0; i<(motMystere.size()-1); i++) motMystere1 += motMystere[i];
и он работает, как и ожидалось, поэтому он не пустой, он отлично читается, может содержать строку новой строки (причина getline)
Существует множество других вещей, которые можно / можно исправить, но
- Это не мой код
- Мне просто любопытно, что за строка
код
#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;
}