Solve use of dictionary for hangman

I try to add use of dictionary for my hangman program. File opens correctly and my random choice still doesn't work asking get line() as often as "nbMotChoisi" in a for loop. When I use fichier.tellg() to get the cursor position, the answer is -1?? though i tried to set it at 0 or 1 with seek().
Can someone help me?
Thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 do {
        cout << "Voulez-vous utiliser le dictionnaire (oui/non) ? " << endl;
        cin >> dico;
            if (dico=="oui")
            {
                nbMot=0;
                ifstream fichier("/Users/mickael_fonck/Documents/Programmation/Pendu/Pendu/dico.txt");
                if(fichier)
                {
                    //L'ouverture s'est bien passée, on peut donc lire
                    
                    while(getline(fichier, ligne))
                    {
                        //Tant qu'on n'est pas à la fin, on lit
                        nbMot++;
                    }
                    while (nbMotChoisi==0) nbMotChoisi= rand() % nbMot;
                }
                else
                {
                    cout << "ERREUR: Impossible d'ouvrir le fichier en lecture." << endl;
                    dico="non";
                }
                for (k=0;nbMotChoisi;k++)
                {
                    getline(fichier,mot);
                }
            }
            else if (dico=="non")
            {
                /* Quel est le mot à faire deviner */
                
                cout << "Quel est le mot à deviner ?" << endl;
                cin >> mot;
            }
            }
When you open the file and count the lines, you read to the end of the file and then the eof bit will be set. you have to use clear() and then seekg() to go back to the beginning.
Thank you so much, was not aware of the clear() function and eof bit set to 1. It was missing in my tutorial.
Topic archived. No new replies allowed.