I'm working an a project that must take a text file (provided to us, in.txt) that has the scores of students from different sections on different labs. You can view the assignment page at: http://www.cs.wmich.edu/~nelson/CS111FALL04/lab06/index.htm.

The problem I'm having is that I can't think of how to make a function that will read the file and put it into an array (no function in this program is supposed to be more than 24 lines so there's no way it can go into the main funciton, I was planning on making a readFile function to do this work).

Can you pass a file to a funciton as a paramater (I got an error, I didn't think it was possible, just wanted to try)? Should I read the file in main and somehow pass it to the function in some way? I'm also quite lost as far as I should go about getting the data from the text file stored correctly into the array. This is an example of how the info in the data file is stored:

The input file:

Name: “in.txt you must use the name in.txt

First line 2 integers:

The number of sections in the class <= 10

The number of lab assignments in the class,  NumLabs <= 10

For Each Section

First Line, 1 Integer:  N The number of students in the section <= 50

For each Student

Student Id  L1  L2  … LNumLabs

Student Id: 4 digit integer

Labs scores: Integers 0 to 100

Example: A small class with 3 sections and 4 labs.

The first section has 4 students, the second 5 and the third 4

3 4
8591 87 60 82 64
1881 95 63 75 94
6758 71 56 72 63
7541 65 95 84 87
3690 88 86 99 68
3515 78 60 98 53
1534 91 95 65 81
2459 86 69 79 50
7047 70 61 63 71
6204 89 98 56 69
6437 92 67 95 87
2012 67 97 66 54
8147 90 50 63 100

I tried setting up some for loops in a test program to read info from the input file into an array (not included into my actual program because as stated above I can't figure out how to implement it into my actual program) but go no where. Could someone help me figure out how to go about this? I would be very grateful to any help.

thanks again for everything,


I finally figured out the getArrayValues function and how to make it work. Logically my code I have should work but I'm not quite sure why I'm getting errors. W/ Visual C++ I get an unexpected end of file and with Linux I get a "
lab06.cxx: In function `void getArrayValues(int (*)[50], int*, int, int)':
lab06.cxx:65: error: parse error at end of input". The code I have thus far is:

/* Name: Scott Kamen  
  Student  Number: 9054  
  Assignment number and descrition: Lab 06 Assignment (Program to open and read a student's 
  scores for a semester. Display a chart with the average and median scores for each section 
  and each lab.)  
  Lab Instructor: Huma Kamal  
  Lab Day and Time: Wed at 6:00 */  
  #include <iostream>  
  #include <fstream>
  #include <cstdlib>
  using namespace std;  
  // Prototypes for Program
  void getArrayValues (int scores [] [50], int labs [], int numsections, int numlabs);
  int main() {   
  // Declare Variables to Store Array Information
  int scores [10] [50];
  int labs [7];
  int numsections;
  int numlabs;
  getArrayValues (scores, labs, numsections, numlabs);
  return 0;
  void getArrayValues (int scores [] [50], int labs [], int numsections, int numlabs) {
  	// Declare Stream and Open Text File
  	ifstream inStream;
  	if (inStream.fail()) {
  	cerr << "Can't open file!\n";
  	else {
  		while (!inStream.eof()) {
  		//"Garbage" Variable to Get Ride of Student ID Number
  		int id;
  		// Get Number of Sections and Labs from Text File
  		inStream >> numsections >> numlabs;
  		// Nested For Loops to Read Info from Text File into Array
  		for (int i = 0; i < numsections; i++) {
  		inStream >> labs[i];
  			for (int j = 0; j < labs[i]; j++) {
  			inStream >> id;
 				for(int k = 0; k < numlabs; j++){
  				inStream >> scores[j][i];
  	// Close File

Can anyone help me fix these errors?

thanks so much,


I fixed the end of file error (added a missing curley brace at the end of the program) but then I got an error for passing an un-initialized variable to a function so I just initialized my numsection and numlab variables to 0 (they are re-assigned new values later so it really shouldn't matter what their initial value is). Now I get no compiler error but I do get a segmentation runtime error. I can't for the life of me figure this one out. If someone could help me with why I'm getting this runtime error that would be awesome.

thanks yet again,


This article has been dead for over six months. Start a new discussion instead.