We are beginners in C++, and we have an assignment due on Wed. We are to create an input file, scores.txt, to contain a list of no more than 32 student records. Each record will have the following format:
<last name><sp><firstname><sp><p01><sp>...<p05><sp><hw01 ><sp>...<hw12><sp>
<he1><sp><he2><sp><final><newline>
We have to declare 2D arrays for lastname, firstname, and grades. We already have that finished. We are having trouble reading in the input file, scores.txt, to the array and then returning the length. If anyone could tell us if that is correct or at least almost correct, we'd appreciate it.
Thanks in advance.
Here's what we have so far:
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
const char LNAME_SIZE = 20;
const char LAST = 32;
int readScores(ifstream& scores_file, ofstream& records_file, const char lname[][LNAME_SIZE + 1], int length);
// reads scores.txt into the arrays and returns the length
void main(void)
{
ifstream fin;
ofstream fout;
char lname[LAST][LNAME_SIZE + 1];
int length;
// connect the input stream to the file scores.txt
fin.open("scores.txt");
// tests the input stream
if ( fin.fail() )
{
cerr << "Error opening file scores.txt for reading. Aborting!"
<< endl
<< endl;
exit(1);
}
// connect the output stream to the file records.txt
fout.open("records.txt");
// tests the output stream
if ( fout.fail() )
{
cerr << "Error opening file records.txt for reading. Aborting!"
<< endl
<< endl;
exit(1);
}
length = readScores(fin, fout, lname, length);
fin.close();
fout.close();
}// end of main()
int readScores(ifstream& scores_file, ofstream& records_file, const char lname[][LNAME_SIZE + 1], int length)
{
char lastName;
scores_file.get(lastName);
while ( !scores_file.eof() )
{
for (int row = 0; row < 1; row++)
{
for (int col = 0; col < 1; col++);
{
length = lname[row][col];
records_file << lastName;
}
}
scores_file.get(lastName);
}
return(length);
}// end readScores()