after a more work (what I could get in between work and school), here is the updated post. All post before this can be pushed to the side / ignored b/c I have re-wrote my code and coming in again. lol. Thanks for input Bazzy. Will look into it more.
Figure I might as well go ahead and clear up that this is obviously a school assignment. I have completed a somewhat decent amount of code (not saying I have a lot of faith in it's correctness tho) and need assistance to figure the rest out. I'm at a complete loss due to teacher and book not really covering material in a way to prepare for the assignment. I have looked over the tutorials for I/O on this site as well as others and if I need to change things, I'm wide open for assistance. I did attempt to figure out as much as I could, but I'm stuck. Thanks in advance for anyone who takes time to assist me. I greatly appreciate it.
To clear things up from the start, here are the instructions:
The program is to take a text file, students.txt, containing information about the currently-enrolled students in a school. Each
line represents a record for a different student and contains the student's identification number (eight digits), credit hours
completed, and grade point average. A sample line follows:
10082352 66 3.04
Program is supposed to have the identification number and grade point average for each student in the file copied to one of four
different text files based on the number of credit hours completed:
Less than 32 freshmen.txt
32 to 63 sophomores.txt
64 to 95 juniors.txt
96 or more seniors.txt
Next is to write the identification number and grade point average of each student on a single line (one line per student) in the
appropriate file. For example, since the student mentioned above has completed 66 credit hours, the following would be
added to the file juniors.txt:
10082352 3.04
The number of students in the original file is unknown, so the program will need to read these records in until you reach the end of
the file.
Also, no student should have a negative number of credit hours, and the maximum number of credit
hours that a student can accumulate is 250. If the program encounters a student record with an erroneous number of
credit hours, write the entire record for that student (identification number, credit hours, and grade point average) to the
file hoursError.txt rather than any of the four aforementioned files. The lowest grade point average a student may have is 0.0, and the highest grade point average a
student may have is 4.0. If the program encounters a student record with an erroneous grade point average, write the
entire record for that student to the file averageError.txt rather than any of the four aforementioned files.As was previously stated, the student's identification number should be eight digits. If the program
encounters a student record with an identification number of fewer or greater than eight digits, write the entire record for
that student to the file identError.txt rather than any of the four aforementioned files.Assume that an identification number error has highest precedence, then the
number of credit hours completed, then the grade point average. Don't write a student record to multiple error files: only
the one that has the highest precedence should receive the record.
I'm going in the direction that I believe it needs to have the input file made into a dynamic array, but not sure how to do that.
I am now at the point that I would need to create an array or vector that would read in the line from students.txt and then be able to sort it to different output files based on the 2nd column of numbers.
ie.
say the line is 10082352 66 3.04
Next is to write the identification number and grade point average of each student on a single line (one line per student) in the
appropriate file.
For example, since the student mentioned above has completed 66 credit hours, the following would be
added to the file juniors.txt based on the criteria for sorting listed in the 1st post:
10082352 3.04
Here is my code thus far
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main()
{
ifstream students("Students.txt");
if (students.fail())
{
cout<< "Input file opening failed.\n";
exit(1);
}
int count=0;
char carray[1000];
while (!students.eof())
{
students.getline(carray,10000);
cout << carray <<endl;
count++;
}
//just to test and see if loop is working I print it to the console.
cout << "The number of records found in the file are: " << count << endl;
ofstream freshmen("freshmen.txt");
ofstream sophomores("sophmores.txt");
ofstream juniors("juniors.txt");
ofstream seniors("seniors.txt");
//test to show output file creation and writing are working
if (freshmen.is_open())
{
freshmen << "The number of records found in the file are: " << count << endl;
freshmen.close();
}
else cout << "Unable to open file";
}
|
To clear up where I'm at, I need assistance in figuring out how to get the input file data into either an array or vector and then sort it to its different respective output files.
There is also possibility that the input file may have a student ID with more than 8 numbers as well as the other two columns having a varying number digits in each column (which will also affect which output file it will be sent to).
//Edited to include
not sure exactly how to use it, but could something like
1 2 3 4
|
int studentID[count];
int creditHours[count];
int gpa[count];
|
work to create an array that will vary based on the number of lines in the file?
//End of edit
Thanks to all who have the patience to assist me.