简体   繁体   中英

Why there is no output from cout for the string

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

  1. It isn't my code
  2. 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;
}

It appears that the text dictionary file has Windows format newlines (CR/LF pair) while the system you run on is only expecting a single newline (LF) character. When you read in a word, the CR ( \\r ) character is the last one in the word. When you output it with cout , this CR moves the output caret to the beginning of the line, and the subsequent output overwrites the word.

You can check for this using your debugger, checking the length of one of the words, and/or adding a \\n character to the cout right after motMystere .

The fix would be to check the last character of the word after reading it in, and removing it if it is a CR character. (You could change the getline to stop when it sees the \\r , but then you'd have to skip the next character which would be a newline.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM