By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,110 Members | 1,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,110 IT Pros & Developers. It's quick & easy.

problem reading from a file

P: n/a
SB
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[i].setName(temp);
cout<<endl<<"name is <"<<doc[i].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[i].setDay(i, temp);
cout<<endl<<"day is <"<<doc[i].getDay(j)<<">";
getline(inputFile, temp);
doc[i].setAmStart(i, temp);
cout<<endl<<"am start for "<<doc[i].getDay(j)<<" is
<"<<doc[i].getAmStart(j)<<">";
getline(inputFile, temp);
doc[i].setAmEnd(i, temp);
cout<<endl<<"am end for "<<doc[i].getDay(j)<<" is
<"<<doc[i].getAmEnd(j)<<">";
getline(inputFile, temp);
doc[i].setAmAppt(i, temp);
cout<<endl<<"am appointment for "<<doc[i].getDay(j)<<" is
<"<<doc[i].getAmAppt(j)<<">";
getline(inputFile, temp);
doc[i].setPmStart(i, temp);
cout<<endl<<"pm start for "<<doc[i].getDay(j)<<" is
<"<<doc[i].getPmStart(j)<<">";
getline(inputFile, temp);
doc[i].setPmEnd(i, temp);
cout<<endl<<"pm end for "<<doc[i].getDay(j)<<" is
<"<<doc[i].getPmEnd(j)<<">";
getline(inputFile, temp);
doc[i].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!
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sun, 11 Apr 2004 20:56:32 -0400, "SB" <vo******@hotmail.com> 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
Jul 22 '05 #2

P: n/a
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
Jul 22 '05 #3

P: n/a
SB
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" <le**@bdsoft.com> wrote in message
news:in********************************@4ax.com...
On Sun, 11 Apr 2004 20:56:32 -0400, "SB" <vo******@hotmail.com> 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

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.