Please help with data file and array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 7
Reputation: christyj5 is an unknown quantity at this point 
Solved Threads: 0
christyj5 christyj5 is offline Offline
Newbie Poster

Please help with data file and array

 
0
  #1
Oct 6th, 2005
I am trying to infile my data file into two seperate arrays which are seperated by a white space in my file.
But I keep getting -86******* something number where my file is suppose to be any help will be very helpful. My first array which will read the student id is a one dimensional array and the second arry is a 2 dimensional arry which will read in the students answers to a true false test and the second col will be the total number correct for each true false question. Yeah and the answer key is suppose to be the last line in the 2 D array which is the first line in my data file.
  1. #include<iostream>
  2. #include <fstream>
  3. #include "string"
  4. using namespace std;
  5. int main()
  6. {
  7. const int maxstudents=50;
  8. const int maxcols=3;
  9. char quiz[maxstudents+1][maxcols];
  10. int studentid[maxstudents];
  11.  
  12. ifstream infile;
  13. string inputfile;
  14. cout<<"Input filename"<<endl;
  15. cin>> inputfile;
  16.  
  17. infile.open(inputfile.c_str());
  18. for (int c = 0; c<maxstudents; c++)
  19. infile>>quiz[maxstudents][c];
  20. for (int k=0; k<maxstudents; k++)
  21. infile>>studentid[k];
  22.  
  23. for (int j=0; j<maxstudents; j++)
  24. cout<<"studentid"<<j<<"\t"<<studentid[j]<<endl;
  25. for (int r=0; r<10; r++)
  26. cout<<quiz[maxstudents][r]<<"\t";
  27.  
  28.  
  29. return 0;
  30. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Please help with data file and array

 
0
  #2
Oct 6th, 2005
post the contents of the data file, or just a few lines if its too large. Your program is written to expect all student ids to be listed first, followed by all answers and lastly the total number correct for each student. But I doubt that is how they appear in the data file.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7
Reputation: christyj5 is an unknown quantity at this point 
Solved Threads: 0
christyj5 christyj5 is offline Offline
Newbie Poster

Re: Please help with data file and array

 
0
  #3
Oct 6th, 2005
This is a piece of the data file.

  1. tttttttfft
  2. 3685 ttftttttft
  3. 2525 ttftfffttt
  4. 3689 ttfffftttt
  5. 3644 fftttffttt
  6. 2505 fffffttttt
  7. 2523 tttttttttf
  8. 1254 tttttttttt
  9. 5255 fffffffftt
<< moderator edit: added code tags: [code][/code] >>
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Please help with data file and array

 
0
  #4
Oct 6th, 2005
where is the third column? Your program needs only one loop. maxcols is too small -- needs to be 1 more than the number of t/f test scores.
  1. for (int c = 0; c<maxstudents; c++)
  2. {
  3. infile>>studentid[c] >> quiz[c];
  4. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7
Reputation: christyj5 is an unknown quantity at this point 
Solved Threads: 0
christyj5 christyj5 is offline Offline
Newbie Poster

Re: Please help with data file and array

 
0
  #5
Oct 6th, 2005
i tried that and still got the -86 error
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Please help with data file and array

 
0
  #6
Oct 6th, 2005
-86, is that a compile-time error or runtime error?
1. make sure maxstudents is not greater than the number of lines in the data file. There are better (and easier) ways to read the file without knowing the number of lines beforehand, but get this working first and you can refine your program later.

2. The output line is incorrect. It should look like this:
  1. cout<<quiz[r]<<"\t";

If you still have problems after making the changes, repost your program. But those should fix it, at least it did in my version that I don't intend to post
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7
Reputation: christyj5 is an unknown quantity at this point 
Solved Threads: 0
christyj5 christyj5 is offline Offline
Newbie Poster

Re: Please help with data file and array

 
0
  #7
Oct 6th, 2005
  1. #include<iostream>
  2. #include <fstream>
  3. #include "string"
  4.  
  5. using namespace std;
  6.  
  7.  
  8. const int maxstudents=25;
  9.  
  10.  
  11. int main()
  12. {
  13.  
  14. const int maxcols=4;
  15. char quiz[maxstudents+1][maxcols];
  16. int studentid[maxstudents];
  17.  
  18. ifstream infile;
  19. string inputfile;
  20. cout<<"Input filename"<<endl;
  21. cin>> inputfile;
  22.  
  23. infile.open(inputfile.c_str());
  24.  
  25.  
  26. for (int c = 0; c<maxstudents; c++)
  27. {
  28. infile>> studentid[c] >> quiz[c];
  29. }
  30.  
  31. for (int r=0; r<maxstudents; r++)
  32. cout<<quiz[r]<<"\t";
  33. return 0;
  34. }
<< moderator edit: added code tags: [code][/code] >>
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Please help with data file and array

 
0
  #8
Oct 6th, 2005
const int maxcols=4;


Count the number of t's and f's in one of those lines. How many did you count? Less than 4 or more than 4. maxcols needs to be a minimum of (number of t's and f's) + 1 for the character array's null-terminator.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7
Reputation: christyj5 is an unknown quantity at this point 
Solved Threads: 0
christyj5 christyj5 is offline Offline
Newbie Poster

Re: Please help with data file and array

 
0
  #9
Oct 6th, 2005
I am getting a blank page

  1. #include<iostream>
  2. #include <fstream>
  3. #include "string"
  4.  
  5. using namespace std;
  6.  
  7.  
  8. const int maxstudents=3;
  9. int main()
  10. {
  11.  
  12.  
  13. ifstream infile;
  14. string inputfile;
  15. cout<<"Input filename"<<endl;
  16. cin>> inputfile;
  17.  
  18. infile.open(inputfile.c_str());
  19.  
  20.  
  21. char quiz[maxstudents+1][11];
  22. int studentid[maxstudents];
  23.  
  24. for (int c = 0; c<maxstudents; c++)
  25. {
  26. infile>> studentid[c] >> quiz[c];
  27. }
  28.  
  29. for (int r=0; r<maxstudents; r++)
  30. cout<<quiz[r]<<"\t";
  31. return o;
  32. }
<< moderator edit: added code tags: [code][/code] >>
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Please help with data file and array

 
0
  #10
Oct 7th, 2005
visually verify the data file is correct -- no blank lines and every line has the same format.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC