Inputting text file into array and finding top 3 values

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 5
Reputation: JoE Guana is an unknown quantity at this point 
Solved Threads: 0
JoE Guana JoE Guana is offline Offline
Newbie Poster

Re: Inputting text file into array and finding top 3 values

 
0
  #11
May 27th, 2008
Hey i've done what you've said but it stills seems to only read the one value and then the program never ends. do i need a getline sorta thing? or anything else


  1. bool readStudents(string fName, studentType stuList[], int& size)
  2. {
  3.  
  4. ifstream inFile;
  5. studentType stu;
  6. size = 0;
  7.  
  8. inFile.open (fName.c_str());
  9.  
  10. if (inFile.is_open())
  11. {
  12. inFile >> stu.id >> stu.assignments[0] >> stu.assignments[1] >> stu.assignments[2] >> stu.exam;
  13. while(!inFile.eof())
  14. {
  15. cout << stu.id << ", " << stu.assignments[0] << ", " << stu.assignments[1] << ", " << stu.assignments[2] << ", " << stu.exam << endl;
  16. cin >> stuList[size].id >> stuList[size].assignments[0] >> stuList[size].assignments[1] >> stuList[size].assignments[2] >> stuList[size].exam;
  17. cout << endl;
  18. size++;
  19. }
  20. return true;
  21. }
  22. else
  23. return false;
  24.  
  25. inFile.close ();
  26.  
  27. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Inputting text file into array and finding top 3 values

 
0
  #12
May 27th, 2008
Originally Posted by JoE Guana View Post
Hey i've done what you've said but it stills seems to only read the one value and then the program never ends. do i need a getline sorta thing? or anything else


  1. bool readStudents(string fName, studentType stuList[], int& size)
  2. {
  3.  
  4. ifstream inFile;
  5. studentType stu;
  6. size = 0;
  7.  
  8. inFile.open (fName.c_str());
  9.  
  10. if (inFile.is_open())
  11. {
  12. inFile >> stu.id >> stu.assignments[0] >> stu.assignments[1] >> stu.assignments[2] >> stu.exam;
  13. while(!inFile.eof())
  14. {
  15. cout << stu.id << ", " << stu.assignments[0] << ", " << stu.assignments[1] << ", " << stu.assignments[2] << ", " << stu.exam << endl;
  16. cin >> stuList[size].id >> stuList[size].assignments[0] >> stuList[size].assignments[1] >> stuList[size].assignments[2] >> stuList[size].exam;
  17. cout << endl;
  18. size++;
  19. }
  20. return true;
  21. }
  22. else
  23. return false;
  24.  
  25. inFile.close ();
  26.  
  27. }
Get rid of lines 12 and 15. You don't want/need stu. stu is only read into once, so it's going to be the same each time when you output it. Stick some debugging output (i.e. some cout statements saying various things) in different places in your program to see how far it gets. You need to know whether you ever return from your function, whether you get into an infinite loop, what size is, etc., so put some cout statements before your while loop, after your while loop, and inside your while loop. You'll delete them later. If your belief that it only reads one line is based on it displaying the same thing over and over, like I said, that's because you are displaying stu, which is never read into in your while loop. Also, replace cin in line 16 with inFile. That's what your problem is. However, sticking cout statements at different spots is an excellent way to narrow down where an error is. The cin in this case is your problem. It's waiting for you to type something. However, line 12 needs to go and line 15 either needs to go or be changed and moved.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 847 | Replies: 11
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC