1

So I need to make a program that acts as a virtual dictionary. Whilst I don't want anyone to write my code for me, I'd appreciate some feedback on my code and maybe a point in the right direction to find some information on a problem im having.

Most of my program works fine, I'm having issues populating my vector from a .txt file and admittedly I dont really understand how it works.

Heres what I've been using:

    ifstream myfile(filename);
    if (myfile.is_open())
    {
        string Line;
        string buffer;
        string currentWordType = "none";
        string currentWord = "none";
        string currentWordDef = "none";
        while (!myfile.eof())

        getline(myfile, buffer);
        currentWordType = buffer;

        getline(myfile, buffer);

        currentWord = buffer;

        getline(myfile, buffer);

        currentWordDef = buffer;

        Word newWord(currentWordType, currentWord, currentWordDef);
        wordList.push_back(newWord);

    }
    myfile.close();

Again I'm not exactly looking for someone to do this for me, just maybe point out some area's ive gone wrong and point me in the right direction.

Thanks!

Bren
  • 13
  • 2
  • Welcome to SO! What is the body of `while` loop? – Tarek Dakhran Apr 19 '20 at 12:22
  • 1
    Also consider [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Thomas Apr 19 '20 at 12:59
  • @TarekDakhran thanks for the welcome! I realised i messed up and forgot the { } to define the scope of the while loop. Essentially it ends right after the wordList.push_back(newWord); – Bren Apr 20 '20 at 14:54
  • @Thomas I looked at the comment section on that article and it seemed like there was alot of for and against arguments between using >> and .eof. can you give me an example of how >> would be used in this block? should i get rid of the getline statements when using that? – Bren Apr 20 '20 at 14:56
  • Well it all depends. The point being in part is that IF you parse with `>>` , then `fail` may happen and `eof` is the wrong check. If you just use `getline` then you should check whether that succeeded AFTER the `getline`. – Thomas Apr 20 '20 at 17:58

2 Answers2

1

To read three strings from a line each you need a loop ... but instead of just checking for eof

while (!myfile.eof())

we check all the error states of the stream

while( myfile ){ ...
};

After each read we should check whether that succeded ...

      std::string currentWordType;
      if( ! getline(myfile, currentWordType)) {
          break;
      }
      std::string currentWord;
      if( ! getline(myfile, currentWord)) {
          break;
      }
      std::string currentWordDef;
      if( ! getline(myfile, currentWordDef)) {
          break;
      }

Afterwards we can add the Word to wordList as before.

Word newWord(currentWordType, currentWord, currentWordDef);
wordList.push_back(newWord);

See working example here

Alternatively you can parse inside the condition

while( myfile >> currentWordType >> currentWord >> currentWordDef ) {     
    Word newWord(currentWordType, currentWord, currentWordDef);
    wordList.push_back(newWord);      
};

See working example here

Thomas
  • 4,980
  • 2
  • 15
  • 30
  • thank you for all the help, I'm still running into issues with getting no return. I hate to keep asking you but this is what my overall solution looks [like](http://coliru.stacked-crooked.com/a/737f8ca764182583) and it draws from [this](http://coliru.stacked-crooked.com/a/f264144cb1e7bead) In case its actually something else causing the issue. I really do appreciate the help. – Bren Apr 21 '20 at 00:10
  • you could have prepared your code better. maybe [this](http://coliru.stacked-crooked.com/a/8ad2e8ef165a7146) helps, but it's essentially what i wrote before. Anyway, your code contained TWO loops, which is not right. – Thomas Apr 21 '20 at 06:08
  • Thank you for your patience mate it really does help. I need to keep at this so I can understand what is happening but im glad that I got such positive feedback from yourself and the other users that responded to this question. – Bren Apr 21 '20 at 13:33
  • If reading is working, you can accept the answer. if you still have problems with getting the user input and looking up the answer, then you will need to ask another question. – Thomas Apr 21 '20 at 15:17
0

there is the while loop you forgot {}, in this instance it will run only the next line which:

getline(myfile, buffer);

until it will reach eof, which means it will overwrite it each time. if you can fix the code so we know that is not the problem. also can you post what error exactly you are getting, or what is the output you are getting, it would help.

  • OK so I had a long day yesterday and can't believe I forgot those brackets, that's been rectified. – Bren Apr 20 '20 at 14:31
  • The problem is the program needs to accept an input word from the user, search for that word in the vector, and then return the definition. Unfortunately it seems to be returning one line of \n before going back to the menu. when I debug it line by line, I notice that my wordList vector remains at size = 0 – Bren Apr 20 '20 at 14:41