problem reading from a file

Discussion in 'C++' started by SB, Apr 12, 2004.

  1. SB

    SB Guest

    Hello. I have an input file which is laid out in the following manner...
    Name
    Day 1
    am time 1
    am time 2
    appointment
    pm time 1
    pm time 2
    appointment
    Day 2
    am time 1....

    This is repeated for three people, for five days. So for each person (three
    names), there is five days worth of the am/pm time 1/2, appointment data.
    The problem I'm having is reading each line and storing it's value in the
    corresponding person's class member. I have a Doctor class (which represents
    a person in the file) and that class has a five element array of a Day class
    (each day's data gets its own Day object). I also have a Schedule class
    which contains an array of three Doctor classes. Here is the code for
    reading the input file and populating the objects data members...

    void Schedule::loadSchedule(ifstream& inputFile)
    {
    string temp; // temp variable for storing each line read from file
    int i; // for doc index
    int j; // for day index

    // read from input file and populate each doctors data
    for (i = 0; i < NUMBER_OF_DOCTORS; i++)
    {
    getline(inputFile, temp);
    doc.setName(temp);
    cout<<endl<<"name is <"<<doc.getName()<<">";
    j = 0; // reset day counter for each doctor
    for (j; j < NUMBER_OF_DAYS; j++)
    {
    // make sure we haven't accidentally read beyond the end of the file
    if (inputFile.eof())
    {
    cout<<"End of File has been reached while reading the input
    file."<<endl;
    exit(1);
    }
    getline(inputFile, temp);
    doc.setDay(i, temp);
    cout<<endl<<"day is <"<<doc.getDay(j)<<">";
    getline(inputFile, temp);
    doc.setAmStart(i, temp);
    cout<<endl<<"am start for "<<doc.getDay(j)<<" is
    <"<<doc.getAmStart(j)<<">";
    getline(inputFile, temp);
    doc.setAmEnd(i, temp);
    cout<<endl<<"am end for "<<doc.getDay(j)<<" is
    <"<<doc.getAmEnd(j)<<">";
    getline(inputFile, temp);
    doc.setAmAppt(i, temp);
    cout<<endl<<"am appointment for "<<doc.getDay(j)<<" is
    <"<<doc.getAmAppt(j)<<">";
    getline(inputFile, temp);
    doc.setPmStart(i, temp);
    cout<<endl<<"pm start for "<<doc.getDay(j)<<" is
    <"<<doc.getPmStart(j)<<">";
    getline(inputFile, temp);
    doc.setPmEnd(i, temp);
    cout<<endl<<"pm end for "<<doc.getDay(j)<<" is
    <"<<doc.getPmEnd(j)<<">";
    getline(inputFile, temp);
    doc.setPmAppt(i, temp);
    cout<<endl<<"pm appointment for "<<doc[i].getDay(j)<<" is
    <"<<doc[i].getPmAppt(j)<<">";
    }
    }

    // close the file
    inputFile.close();
    }

    Here is the output from the cout statement in the above code...

    name is <Andrew Jones>
    day is <Monday>
    am start for Monday is <9>
    am end for Monday is <12>
    am appointment for Monday is <Lecture>
    pm start for Monday is <13>
    pm end for Monday is <16>
    pm appointment for Monday is <Free>
    day is <>
    am start for is <>
    am end for is <>
    am appointment for is <>
    pm start for is <>
    pm end for is <>
    pm appointment for is <>
    day is <>
    am start for is <>
    am end for is <>
    am appointment for is <>
    pm start for is <>
    pm end for is <>
    pm appointment for is <>
    day is <>
    am start for is <>
    am end for is <>
    am appointment for is <>
    pm start for is <>
    pm end for is <>
    pm appointment for is <>
    day is <>
    am start for is <>
    am end for is <>
    am appointment for is <>
    pm start for is <>
    pm end for is <>
    pm appointment for is <>
    name is <Eric Roberts>
    day is <>
    am start for is <>
    am end for is <>
    am appointment for is <>
    pm start for is <>
    pm end for is <>
    pm appointment for is <>
    day is <Tuesday>
    am start for Tuesday is <9>
    am end for Tuesday is <12>
    am appointment for Tuesday is <Free>
    pm start for Tuesday is <13>
    pm end for Tuesday is <16>
    pm appointment for Tuesday is <Surgery>
    day is <>
    am start for is <>
    am end for is <>
    am appointment for is <>
    pm start for is <>
    pm end for is <>
    pm appointment for is <>
    day is <>
    am start for is <>
    am end for is <>
    am appointment for is <>
    pm start for is <>
    pm end for is <>
    pm appointment for is <>
    day is <>
    am start for is <>
    am end for is <>
    am appointment for is <>
    pm start for is <>
    pm end for is <>
    pm appointment for is <>
    name is <Mathew Hayden>
    day is <>
    am start for is <>
    am end for is <>
    am appointment for is <>
    pm start for is <>
    pm end for is <>
    pm appointment for is <>
    day is <>
    am start for is <>
    am end for is <>
    am appointment for is <>
    pm start for is <>
    pm end for is <>
    pm appointment for is <>
    day is <Wednesday>
    am start for Wednesday is <9>
    am end for Wednesday is <12>
    am appointment for Wednesday is <Free>
    pm start for Wednesday is <13>
    pm end for Wednesday is <16>
    pm appointment for Wednesday is <Lecture>
    day is <>
    am start for is <>
    am end for is <>
    am appointment for is <>
    pm start for is <>
    pm end for is <>
    pm appointment for is <>
    day is <>
    am start for is <>
    am end for is <>
    am appointment for is <>
    pm start for is <>
    pm end for is <>
    pm appointment for is <>

    The problem is the empty less than/greater than signs. I don't know why data
    is not being populated for those values. Is it the way I'm using getline or
    something? Interestingly, the less than/greater than signs that do contain
    data are all correct. I matched them up with the data in the input file and
    it matches. So the values that are getting populated are being done in the
    correct order. Can anyone see what I'm doing wrong here? Any help is
    appreciated!

    Thank you in advance![/i][/i]
     
    SB, Apr 12, 2004
    #1
    1. Advertising

  2. SB

    Leor Zolman Guest

    On Sun, 11 Apr 2004 20:56:32 -0400, "SB" <> wrote:

    Without declarations for the data you're working with, folks aren't too
    likely to waste their time guessing as to what your problem might be.

    I'd suggest re-posting with a /small/ version of your program that exhibits
    the erroneous behavior, driven by a /small/ input file that you can show
    the exact contents of (I couldn't tell if your input file details were a
    description or data or what...but I guessed they were just a description),
    and then just show enough of the output to indicate one or two examples of
    the failure.

    Thanks,
    -leor

    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: Download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
     
    Leor Zolman, Apr 12, 2004
    #2
    1. Advertising

  3. SB

    Leor Zolman Guest

    I just noticed you also have all these calls to setThisAndThat(). Include
    them too. IOW, show a complete program if you want prompt, useful feedback.
    Thanks,
    -leor

    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: Download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
     
    Leor Zolman, Apr 12, 2004
    #3
  4. SB

    SB Guest

    Thanks, but I actually figured it out myself. I mistakenly used variable i
    instead of j for the setXXX function calls. Although, the output that I
    posted was the entire output.

    Thanks anyways!

    "Leor Zolman" <> wrote in message
    news:...
    > On Sun, 11 Apr 2004 20:56:32 -0400, "SB" <> wrote:
    >
    > Without declarations for the data you're working with, folks aren't too
    > likely to waste their time guessing as to what your problem might be.
    >
    > I'd suggest re-posting with a /small/ version of your program that

    exhibits
    > the erroneous behavior, driven by a /small/ input file that you can show
    > the exact contents of (I couldn't tell if your input file details were a
    > description or data or what...but I guessed they were just a description),
    > and then just show enough of the output to indicate one or two examples of
    > the failure.
    >
    > Thanks,
    > -leor
    >
    > --
    > Leor Zolman --- BD Software --- www.bdsoft.com
    > On-Site Training in C/C++, Java, Perl and Unix
    > C++ users: Download BD Software's free STL Error Message Decryptor at:
    > www.bdsoft.com/tools/stlfilt.html
     
    SB, Apr 12, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    3
    Views:
    688
    Kevin Spencer
    Nov 11, 2004
  2. Wael Soliman

    ASP.NET Reading problem (reading .xls)

    Wael Soliman, Jan 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,808
    =?Utf-8?B?dmluYXk=?=
    Jan 3, 2005
  3. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,421
    Scott Allen
    Jun 4, 2005
  4. Replies:
    0
    Views:
    798
  5. Karim Ali

    Reading a file and resuming reading.

    Karim Ali, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    381
    Hrvoje Niksic
    May 25, 2007
Loading...

Share This Page