Thread: reading in array and returning length

  1. #1
    Unregistered
    Guest

    reading in array and returning length

    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()

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    please use code tags...
    I'm not really sure what you want to do, and I'm not going to read your code. Can you give me an example of an input file? Are you supposed to read the data into structs? And what length are you measuring? The amount of chars in the file? The number of records?

  3. #3
    Unregistered
    Guest
    Thanks for the response. Sorry for not using code tags. We don't normally post on message boards.

    An example of an input file (from the input file we have to create - scores.txt) is:

    Code:
    Jones Mary 25 23 20 24 25 5 5 4 1 2 3 4 5 5 4 3 2 35 20 135
    Smith Jim 22 15 24 18 20 5 5 5 3 4 5 1 5 4 0 5 3 45 30 125
    where Jones is the last name, Mary is the first name, and all numbers after are grades for certain assignments. This data is to be read into the arrays lname, fname, and grades, which are all 2D arrays. We have to return the length of the number of records in the arrays. Since each array will be used in parallel, the length will be the same for all three arrays. That is all the information given to us.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2009, 05:48 AM
  2. Returning Array
    By baffa in forum C Programming
    Replies: 26
    Last Post: 02-01-2008, 10:08 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  5. Replies: 6
    Last Post: 10-21-2003, 09:57 PM