newbie question reading in an array using a structure for the array type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • traviswhiskey
    New Member
    • Dec 2007
    • 5

    #1

    newbie question reading in an array using a structure for the array type

    so i'm trying to read in from a file but having lots of trouble so far all i get are segmentation faults or jibberish numbers when outputting and i just need a little bit of direction on how to solve this problem. i'm reading i don't have much experience with reading arrays in from a file and stuff. heres what i have.
    Code:
    struct date
    {
          int day, month, year; 
    };
    
    struct student
    {
          string firstName, lastName;
          int hours;
          double points;
          date dob;
    };
    those are my structs and my prime read is
    Code:
    students>>  listOfS[arraySize].lastName >> listOfS[arraySize].firstName >> listOfS[arraySize].hours >> listOfS[arraySize].points >> listOfS[arraySize].dob.day >> listOfS[arraySize].dob.month >> listOfS[arraySize].dob.year;
    
    while(!students.eof())
        { 
                              arraySize++;
                              cout <<  listOfS[arraySize].lastName << listOfS[arraySize].firstName << listOfS[arraySize].hours << listOfS[arraySize].points << listOfS[arraySize].dob.day << listOfS[arraySize].dob.month << listOfS[arraySize].dob.year;
        }
    array size is initialized to 100 and i have error checking to make sure the file opens and the dat file is
    Code:
    Jones Pete 30 78.0 1 11 1973
    Brown Carl 64 198.4 6 7 1985
    Smith Ann 37 118.4 9 2 1980
    White Mary 54 143.1 3 17 1975
    Black John 47 87.4 12 10 1981
    Flyn Bee 58 145.0 8 15 1987
    Sikes Bob 23 88.78 2 11 1983
    thank you in advance
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    You honestly should read the forum rules about using CODE tags. It's more than rude to dump mass amounts of unformatted code. We can't read it properly, and we won't be able to help you.

    Maybe the problem is somewhere else. But I already see a problem with:

    Code:
    while(!students.eof())
    {
        arraySize++;
    eof() does not behave like you imagine it does. That is why you should always read the documentation before you use it. The documentation will state something like it returns true if the eof flag on a stream is set. I'm going to stop here, and see if you intuitively see the problem. If not, I'll explain further.

    Comment

    • traviswhiskey
      New Member
      • Dec 2007
      • 5

      #3
      yeah i'm not understanding

      Comment

      • traviswhiskey
        New Member
        • Dec 2007
        • 5

        #4
        so whats wrong with the eof()?

        Comment

        • traviswhiskey
          New Member
          • Dec 2007
          • 5

          #5
          can anyone else help me out?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by traviswhiskey
            Jones Pete 30 78.0 1 11 1973
            With data line this if you use the >> operator into a string all you get is Jones. The >> operator skips whitespace.

            You can only use the >> operator when you know the format of the data and your data has unknown format. I say this because this data:
            [code=c]
            weaknessforcats 30 78.0 1 11 1973
            [/code]

            looks the same but has fewer data elements.

            You need to a) alter your input by inerting field separators so you know where your data elements are, or b) read the entire line in as a single string and then parse out the elements from the string.

            Comment

            Working...