944,179 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4107
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 6th, 2005
0

Please help with data file and array

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
christyj5 is offline Offline
7 posts
since Sep 2004
Oct 6th, 2005
0

Re: Please help with data file and array

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Oct 6th, 2005
0

Re: Please help with data file and array

This is a piece of the data file.

C++ Syntax (Toggle Plain Text)
  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] >>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
christyj5 is offline Offline
7 posts
since Sep 2004
Oct 6th, 2005
0

Re: Please help with data file and array

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.
C++ Syntax (Toggle Plain Text)
  1. for (int c = 0; c<maxstudents; c++)
  2. {
  3. infile>>studentid[c] >> quiz[c];
  4. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Oct 6th, 2005
0

Re: Please help with data file and array

i tried that and still got the -86 error
Reputation Points: 10
Solved Threads: 0
Newbie Poster
christyj5 is offline Offline
7 posts
since Sep 2004
Oct 6th, 2005
0

Re: Please help with data file and array

-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:
C++ Syntax (Toggle Plain Text)
  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
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Oct 6th, 2005
0

Re: Please help with data file and array

C++ Syntax (Toggle Plain Text)
  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] >>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
christyj5 is offline Offline
7 posts
since Sep 2004
Oct 6th, 2005
0

Re: Please help with data file and array

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Oct 6th, 2005
0

Re: Please help with data file and array

I am getting a blank page

C++ Syntax (Toggle Plain Text)
  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] >>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
christyj5 is offline Offline
7 posts
since Sep 2004
Oct 7th, 2005
0

Re: Please help with data file and array

visually verify the data file is correct -- no blank lines and every line has the same format.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: where to get C compiler
Next Thread in C++ Forum Timeline: C++ help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC