I have an issue with a section of code that will not work correctly. The first instance work fine but everything after is junk data.

int arrivaltime = 0;
int servicetime = 0;
int waittime = 0;

// This section shows the bank wait time for the different branch and tellers
  std::ifstream in("bankSMALL.txt",std::ios::in |std::ios::binary);
cout<< "Branch      Teller ID      Arrival Time       Service Time       Wait time/sec" <<endl;

if(!in)
{
 
std::cout<<"Could not open file"<<std::endl;

 
}
//this is where we are reading in the information into our array
while( in)
{
	
in >> branchnumber >> tellern1 >> tellern2 >> ahr1 >> ahr2 >> amin1 >>amin2 >> asec1 >> asec2
>> shr1 >>shr2 >> smin1 >> smin2 >> ssec1 >> ssec2;
cout << " " << branchnumber << " ";
cout <<"         "<< tellern1 << tellern2;
cout <<"             " << ahr1 << ahr2 << ":" << amin1 << amin2 << ":"<< asec1 << asec2;
cout <<"           " << shr1 << shr2 << ":" << smin1 << smin2 << ":"<< ssec1 << ssec2;
// calculate the arrival time, service time and wait time in sec
arrivaltime = ((ahr1 * 10) + ahr2) * SEC_PER_HR + ((amin1 + amin2) * sec) + (asec1 + asec2);
servicetime = ((shr1 * 10) + shr2) * SEC_PER_HR + ((smin1 + smin2) * sec) + (ssec1 + ssec2);
waittime = servicetime - arrivaltime;

cout << "           " << waittime <<endl;
 loopCount++;
}

I have to convert hrs, min, sec into just sec for both the arrival time and service time and out put the wait time from the two. My data set is as such:
arrival / service / wait (should be)
14:12:51 / 14:16:59 / 248
09:05:37 / 09:07:48 / 131
10:15:02 / 10:29:29 / 867

I set waittime = 0 in the begining of the loop. Can someone help me

Recommended Answers

All 7 Replies

Why did you add 'binary' to the input mode for a text file?

Before you get into calculations, just make the program print out what it read in. If it can't do that much properly, then there isn't much point with the rest of the program.

You also need to post a few lines of your input file, and how you declared the various variables used to hold input data.

The input come from a text file that is hard coded into the program. it reads in all the correct data and print it out fine. At the bottom of my first post is the input arrival , service and the supposed wait time calculated from the service and arrival. Like I said it prints out the first wait time correct and then all other wait times are wrong

could there be something in my loop that is causing it to give me the incorrect wait time? My loop count also count one extra line for some reason.

line 20 and 21...are you sure we can read data from a binary file like that???

Paste some lines of your text file, and show some variable declarations.

It seems like you're just inputting characters, which would obviously work for the first line (perhaps), but because you fail to deal with the newline means all the other lines are messed up.

> My loop count also count one extra line for some reason.
Because you don't check the result of your file "READ" command until much later on, namely after you've processed some garbage data and then incremented the count.
It should be of the form

while ( in >> branchnumber >> tellern1 ... ) {
}

Though it would be much better IMO to read the line using a std::string, then extract information from that line. You're much less likely to run into problems caused by formatting errors in the input file.

figured out the calculation issue but still having the line problem. And to answer you question about reading binary data from a file in that manner yes you can. The program work except the added junk line.

Please post you latest code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.