Thread: Reading .txt file across (blank) lines into struct

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    203

    Reading .txt file across (blank) lines into struct

    I'm having trouble reading information from .txt file into a struct when that info is spread across several lines of the file with some blank lines in between. The .txt file looks like:

    1. What is the capital of Peru? 2 //2 being the key to the right answer

    1. Santiago
    2. Lima
    3. Montevideo
    4. Havana

    2. Which is the largest planet in the solar system? 3

    1. Saturn
    2. Neptune
    3. Jupiter
    4. Uranus

    My code is here: #10963301 - Pastie

    However the output looks like this:
    Code:
    Question 1:  What is the capital of Peru?
    The options are:
    1.  Santiago
    2.  Lima
    3.  Montevideo
    4.  Havana
    And the correct choice is option 2
    Question 2:  Which is the largest planet in the solar system?
    The options are:
    1.  Santiago
    2.  Lima
    3.  Montevideo
    4.  Havana
    5.  Saturn
    6.  Neptune
    7.  Jupiter
    8.  Uranus
    And the correct choice is option 3
    The second question is picking up all the choices from the first question and then it's own options. I've tried a couple of other approaches (commented out in the program) but each produces it own set of quirks. If somebody could help me put the finishing touches here I'd be most grateful. Thanks,
    Last edited by sean_cantab; 11-15-2016 at 11:00 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    quest.m_answers.clear();
    while (File >> quest)
            {
                QQQ.emplace_back(quest);
            }
    You need to clear the answers every time you read a question, not just the first time.

    Put the clear statement inside operator >>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Thank you very much indeed.
    Here's how I'm trying to explain this back to myself, please let me know if there are any lacunae in my understanding:
    Question object quest remains in scope as long as filestream object File is open and so it's container data-member, here vector<string>, also stays 'open'. So when the second question is being read from File, quest's container data member still has the answers from the first question. This is true for all quest's data-members but POD members are simply overwritten but for container data-members like vectors, since we're using push_back or emplace_back the second set of answers get attached to the end of the first set of answers and so on. Thus when struct or class objects with container-type data members are being read into, we need to clear() the containers after each reading ...
    Last edited by sean_cantab; 11-16-2016 at 05:39 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking Blank Lines ??
    By rakk92 in forum C++ Programming
    Replies: 5
    Last Post: 12-12-2015, 11:25 AM
  2. Replies: 2
    Last Post: 05-04-2013, 04:29 PM
  3. Checking for a Blank Line When Reading From a File
    By jeanermand in forum C Programming
    Replies: 6
    Last Post: 02-07-2012, 06:50 PM
  4. Reading a file containing a blank line
    By maniac123 in forum C++ Programming
    Replies: 8
    Last Post: 01-27-2011, 11:42 AM

Tags for this Thread