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.

Here is the code I have with compiler errors formatted within brackets after comment code. e.g. //[compiler error]

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

/** [ I get the "unknown size" error on all of these b/c I don't know how to assign them ] */

int studentID[];
int creditHours[];
double gpa[];


int main()
{

int studentID[]; 
int creditHours[]; 
int gpa[];
ifstream students; 
ofstream freshman, sophomores, juniors, seniors; 


ifstream in_stream;
in_stream.open("students.txt");

if (in_stream.fail())
{
cout<< "Input file opening failed.\n";
exit(1);
}

ofstream out_stream;
out_stream.open("freshmen.txt");
out_stream.open("sophomores.txt");
out_stream.open("juniors.txt");
out_stream.open("seniors.txt");
out_stream.open("hoursError.txt");
out_stream.open("averageError.txt");
out_stream.open("identError.txt");

if (out_stream.fail())
{
cout<< "Output file opening failed.\n";
exit(1);
}

out_stream.setf(ios::fixed);
out_stream.setf(ios::showpoint);
out_stream.precision(2);

while (!students.eof())
{

void getInput(ifstream students, int &studentID, int &creditHours, int &gpa);

}


/** [ error C2446: '<' : no conversion from 'int *' to 'int' There is no context in which this conversion is possible ] & [ error C2040: '<' : 'int' differs in levels of indirection from 'int []' ] */

for (int i=0; i < studentID; i++)
out_stream << studentID[i] << creditHours[i] << gpa[i] << endl;


in_stream.close();
out_stream.close();
}



/** [ error C4430: missing type specifier - int assumed. Note: C++ does not support default-int ]*/

getInput(ifstream students, int &studentID, int &creditHours, int &gpa); 

// [ error C2447: '{' : missing function header (old-style formal list?) ]
{ 
    int i = 0; 
        while(!students.eof()) 
        { 
       students >> &studentID[i] >> &creditHours[i] >> &gpa[i]; 
       cout << studentID[i] << creditHours[i] << gpa[i] << endl; 
           i++; 
        } 
return;

}

Thanks again for anyone who has patience to assist me. It's greatly appreciated.

you are doing it all wrong..your concepts of ifstream and ofstream arent clear.i think you should first study them,then you will be able to do this assignment!.
well i can help you.
when you make an object of "ifstream" you use it like this:

ifstream open("students.txt");

this makes an object of ifstream which opens the file students.txt.
now use this object to perform other opertations.

first of all you need to know the number of records in the file.
use the eof() to read and count the number of times it reads a line.then you will know the number of records!

read the integers in a char array and then convert and break into int.
you need to create dynamic array of integers because you do not know the number of records.

when you have the number of record through the above mentioned process.then create a array like this.

\\lets say count(int type) has the number of  variables

int *dynarray= new int[count];

//this is a 1-D dynamic array,this way you wont get the error.

here is the code to read a line into a char array

char carray[10];

ifstream in("filename.txt");

in.getline(carray,10);   //read a line(of 10 chars) into carray

then break the read line and get your values.
i would suggest that first you read the values in variables then worry about writing.

hope this help

I'm going to try and break things down and do them step by step. Here is the code I'm trying to use to open the input file, students.txt, and just a single output file, freshmen.txt. Can someone tell me if I'm on the right track please? My book said that if the output file didn't exist, the program would create it, but it isn't doing that nor when I create a file does it print anything to the file. There isn't anything showing up on the compiler screen other than it saying that there wasn't any errors.

As in my first post, I would like to be able to open the students.txt file and then sort its elements into different output files based on certain criteria is why I have the other open output file code, but I can't get just the one output file right now.

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;


int main()
{
 int elements;          

ifstream students;
ifstream open("students.txt");

if (students.fail())
{
cout<< "Input file opening failed.\n";
exit(1);
}
 students >> elements;        
     while (!students.eof( ))      
     {
          cout<<elements<<endl;    
          students >> elements;         
     }


ofstream freshmen;
  freshmen.open ("freshmen.txt");
  /**
ofstream sophomores;
  sophomores.open ("sophomores.txt);
ofstream juniors;
  juniors.open ("juniors.txt");
ofstream seniors;
  seniors.open ("seniors.txt");
*/
if (freshmen.fail()/**, sophomores.fail(), juniors.fail(), seniors.fail()*/)
{
cout<< "Output file opening failed.\n";
exit(1);
}
}

it wont let me edit my post, so...

when I'm able to open the files, I want to use eof() to count the number of lines in the file.

Also, I was looking on the net and many users say that its better to use vectors for the type of function I'm needing. I don't have any experience and limited knowledge on vectors tho. Any suggestions?

ya do it step by step.

your code:

ifstream students;
ifstream open("students.txt");

if (students.fail())
{
cout<< "Input file opening failed.\n";
exit(1);
}

you made an object of students,but didnt open any file with it.and then you created another object (open)and you are opening the file with it.

here is what your code should be:

ifstream students("Students.txt");
if (students.fail())
{
cout<< "Input file opening failed.\n";
exit(1);
}

students >> elements;

will read only the first integer value into the integer "elements",

reading like this will not read the whole line...only before the spaces.

use the getline method.

int count=0;
char carray[10];
 while (!students.eof( ))      
     {
          students.getline(carray,10);
          cout << carray <<endl;    
              count++;   
     }

cout << "The number of records found in the file are: " << count << endl;

make an output file like this :

ofstream freshmen("freshmen.txt");
//this will make  a file named freshmen.txt

theres no need to check if the output file fails to open.

I was able to get two different ways to work (tho I had to change your code to increase the size of the array to make it work for the purpose until the next "step" is completed). I was able to print to the console as the normal cout code is supposd to and I was able to print the line into the text file.

Now the next thing is to figure out how to create an array (or as suggested on another forum I have this posted, is to create a vector) that will accept the input from the input file and place into it's elements. Then to work on the sorting the files into their respective output files based on the instructions (I put them in the 1st post. elements are seperated based on how many credit hours completed, which is also defined in the 1st post).

my updated code is :

#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++;
      }
      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");


//The way the tutorial on cplusplus.com said to do it

if (freshmen.is_open())
  {
    freshmen << "The number of records found in the file are: " << count << endl;
    freshmen.close();
  }
  else cout << "Unable to open file";

}

yet again, I wanted to edit the post due to working on the program after posting it, but it wouldn't allow me to. Scratch that post and here is the updated one. Sorry for not waiting to make the post. Will work on that.


I was able to get two different ways to work (tho I had to change your code to increase the size of the array to make it work for the purpose until the next "step" is completed). I was able to print to the console as the normal cout code is supposd to and I was able to print the line into the text file.

Now the next thing is to figure out how to create an array (or as suggested on another forum I have this posted, is to create a vector) that will accept the lines from students.txt and place into it's elements. Then to work on the sorting the files into their respective output files based on the instructions (I put them in the 1st post. elements are seperated based on how many credit hours completed, which is also defined in the 1st post).


So another way of saying all that is 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 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

my updated code is :

#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++;
      }
      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");


//The way the tutorial on cplusplus.com said to do it

if (freshmen.is_open())
  {
    freshmen << "The number of records found in the file are: " << count << endl;
    freshmen.close();
  }
  else cout << "Unable to open file";

}

I'm going to work on the code for a few more min then I have to crash for school then work immediately after. I will work on it more when I get home.

Thanks again very much for the patience and assistance.

not sure exactly how to use it, but could something like

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.