0

thanks for looking at this in advance. I can get my code to compile correctly and whatnot, but when running I get stuck in an infinite seeming loop and I have to exit out manually every time. I am new to reading in data from files and I think that is probably my error but any help looking at my code would be appreciated.

In the interest of not submitting some giant file, I'm only going to submit my main function because I assume the error is in there, I'm just not sure of where.

  4 #include "class.h"
  5 #include <fstream>
  6 using namespace std;
  7 
  8 
  9 int main()
 10 {
 11     ifstream external; //declaring input stream for my external file
 12     //external.open("external.txt"); //telling the compiler what the source of my external file is called
 13     //external.close();
 14 
 15     char time[4], course[4], section[3]; //places to store read in values
 16     char dept_name[20];
 17 
 18     table hash_table; //instance of my class
 19 
 20     while(!external.eof()) //while it is not the end of the file
 21     {
 22         external.open("external.txt"); //opens file from location
 23 
 24         external.getline(dept_name, 20); //grabs the info to be input
 25         external.getline(time, 4);
 26         external.getline(course, 4);
 27         external.getline(section, 3);
 28 
 29         external.close(); //closes file until new one must begin
 30 
 31         cin.ignore(5, '\n'); //ignores five characters until next course
 32         hash_table.insert(dept_name, course, section, time);  //inserts to table
 33     }
 34     hash_table.display_all();
 35 }
  • Check out [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – herohuyongtao Feb 27 '14 at 04:03
  • That's interesting, but in my case since I'm reading in to multiple different arrays, it seems like that would be more difficult to apply. – smooth_atonal_sound Feb 27 '14 at 04:09

1 Answers1

3

Don't open the file for every entry you read. Among other things, opening the file for each entry means you always start reading at the start of the file.

Change this:

 20     while(!external.eof()) //while it is not the end of the file
 21     {
 22         external.open("external.txt"); //opens file from location
 23 
 24         external.getline(dept_name, 20); //grabs the info to be input
 25         external.getline(time, 4);
 26         external.getline(course, 4);
 27         external.getline(section, 3);
 28 
 29         external.close(); //closes file until new one must begin
 30 
 31         cin.ignore(5, '\n'); //ignores five characters until next course
 32         hash_table.insert(dept_name, course, section, time);  //inserts to table
 33     }

to this:

 20     external.open("external.txt"); //opens file from location
 21     while(!external.eof()) //while it is not the end of the file
 22     {
 23         
 24         external.getline(dept_name, 20); //grabs the info to be input
 25         external.getline(time, 4);
 26         external.getline(course, 4);
 27         external.getline(section, 3);
 28 
 29         
 30         cin.ignore(5, '\n'); //ignores five characters until next course
 31         hash_table.insert(dept_name, course, section, time);  //inserts to table
 32     }
 33     external.close(); //closes file
user253751
  • 57,427
  • 7
  • 48
  • 90