reading from a file into a 2d array?

I cannot figure out how to go from having 3 parallel arrays to 2 parallel arrays while making one of the arrays a 2d array while reading it from a file. my programs purpose is to read from a file elves names and the # of toys they made while having a third array with their individual rating based on the # of toys they made and im supposed to link the # of toys to their rating how do i do this? my readinto function was my attempt at doing so but since ive commented it out the program works fine and displays the 3 parallel arrays properly.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h> // for exit()
#include <limits.h> // for INT_MIN and INT_MAX

#define MAX 50

using namespace std;


void displayPurpose();
void openFile(ifstream& infile);
void readFile(ifstream &infile, string elves[], int toys[], int &cnt, int ratings[]);
void detRating(int toys[], int ratings[], int cnt);
int calcTotalToys(int toys[], int cnt);
int calcNumElves500(int toys[], int cnt);
void print(string elves[], int toys[], int ratings[], int cnt);
void readinto(ifstream &infile, int toys[], int ratings[], int cnt);


int main()
{
ifstream inf;
string elves[MAX];
int toys[MAX];
int ratings[MAX];
int cnt = 0;
int totalToys;
int numElves500;
int minInd; // index for elf making least toys
int maxInd; // index for elf making most toys
int currInd;
int minIndx;

displayPurpose();

// open input file
openFile(inf);

// read elf information from input file
readFile(inf, elves, toys, cnt, ratings);

// determine ratings for each elf
detRating(toys, ratings, cnt);

// calculate total toys made by all elves
totalToys = calcTotalToys(toys, cnt);

// calculate number of elves that made more than 500 toys
numElves500 = calcNumElves500(toys, cnt);

//readinto(inf, toys, ratings, cnt);


// print elf information
print(elves, toys, ratings, cnt);
return 0;
} // end funtion main


/*****************************************************************************/
/*
* Function name: displayPurpose
* Description: Display purpose of program to user.
* Parameters: none
* Return value: none
* */
/*****************************************************************************/
void displayPurpose()
{
cout << "\n\n";
cout << "This program will read inforation about Santa's elves from a file." << endl;
cout << "Each elf will receive a rating based on their toy production." << endl;
cout << "A table will be printed with each elve's name, number of toys made and their rating" << endl;
cout << "Summary information will also be printed." << endl;
cout << endl << endl;

}



/*****************************************************************************/
/*
* Function name: openFile
* Description: Prompt user for filename and open the file.
* Parameters: ifstream &infile - the input filestream
* Return value: none
* */
/*****************************************************************************/
void openFile(ifstream &infile)
{
string filename;

cout << "Enter the name of the input file: ";
cin >> filename;

// convert to c-type string for open() funtion
infile.open(filename.c_str());

if (infile.fail()) // can also do if (!infile) here
{
cout << "Error opening input file, " << filename << endl;
exit(1);
}

return;
}


/*****************************************************************************/
/*
* Function name: readFile
* Description: Read the elf information (name and number of toys made)
* from the input file.
* Parameters: ifstream &infile - the input file stream
* string elves[] - elf names
* int toys[] - number of toys made
* int &cnt - number of elves
* Return value: none
* */
/*****************************************************************************/
void readFile(ifstream &infile, string elves[], int toys[], int &cnt, int ratings[])
{

infile >> elves[cnt];
while (!infile.eof()) {
infile >> toys[cnt];
cnt++;
infile >> elves[cnt];
} // end while

return;
} // end function readFile

/*
void readinto(ifstream &infile, int toys[], int ratings[], int cnt)
{
int i, j;
int arr2[MAX][2] ;
//infile >> arr2[MAX][2];
while (!infile.eof())
{
for (int i = 0; i < MAX; i++)
{
for (int j = 0; j < 2; j++)
{
infile >> arr2[i][j];
}
}

}
return;

}
*/

/*****************************************************************************/
/*
* Function name: detRating
* Description: Determine the rating for each elf based on number of toys made.
* Parameters: int toys[] - number of toys made
* string ratings[] - ratings for each elf
* int cnt - number of elves
* Return value: none
* */
/*****************************************************************************/
void detRating(int toys[], int ratings[], int cnt)
{
for (int i = 0; i < cnt; i++) {
if (toys[i] >= 500)
ratings[i] = 5;
else if (toys[i]>= 300)
ratings[i]= 3;
else if (toys[i]>= 200)
ratings[i]= 1;
else
ratings[i]= 0;
} // end for

return;
} // end function rating



/*****************************************************************************/
/*
* Function name: calcTotalToys
* Description: Calculate the total number of toys made by all elves.
* Parameters: int toys[] - number of toys made
* int cnt - number of elves
* Return value: total number of toys made
* */
/*****************************************************************************/
int calcTotalToys(int toys[], int cnt)
{
int totalToys = 0;

for (int i = 0; i < cnt; i++)
totalToys = totalToys + toys[i];

return totalToys;
} // end function calcTotalToys


/*****************************************************************************/
/*
* Function name: calcNumElves500
* Description: Determinte the number of elves that made more than 500 toys.
* Parameters: int toys[] - number of toys made
* int cnt - number of elves
* Return value: number of elves that made more than 500 toys
* */
/*****************************************************************************/
int calcNumElves500(int toys[], int cnt)
{
int numElves = 0;

for (int i = 0; i < cnt; i++)
if (toys[i] > 500)
numElves = numElves++;

return numElves;
} // end function calcNumElves500



/*****************************************************************************/
/*
* Function name: print
* Description: Display a table of the elf information
* Parameters: string elves[] - the elf names
* int toys[] - number of toys made
* string ratings[] - the ratings for each elf
* int cnt - number of elves
* Return value: none
* */
/*****************************************************************************/
void print(string elves[], int toys[], int ratings[], int cnt)
{
// print headings
cout << endl;
cout << "ELF TOY PRODUCTION" << endl;
cout << left << setw(25) << "Elf Name" << right << setw(7) << " Toys";
cout << left << setw(3) << " " << setw(6) << "Rating" << endl;
cout << left << setw(25) << "--------" << right << setw(7) << " ----";
cout << left << setw(3) << " " << setw(6) << "------" << endl;

for (int i = 0; i < cnt; i++) {
cout << left << setw(25) << elves[i];
cout << right << setw(7) << toys[i];
cout << setw(3) << " " << left << setw(6) << ratings[i] << endl;
} // end for

cout << endl;

return;
} // end function print
Are the parallel arrays a requirement?

Have you studied structures yet?

they are a requirement and no i have not learned about structures.
Okay then, why are you trying to go to a 2D array? And what do you want that 2D array to hold?

im trying to go to a 2d array because that is the requirement of this program turning the 3 seperate parallel arrays into 2 with one of the arrays being a 2d array containing the information on how many toys were made and the rating based on that number.
It would help if you were to post the text of the actual assignment. Using a 2D array on a "parallel array" assignment really doesn't make much sense.

Program #1 - 2D Arrays (Elves)
You are to modify the program you wrote for CSC135, Program #6 (Arrays) to make use of 2D (two
dimensional) arrays. If you want to begin with my version of the elf program
.In Program #6, you read the number of toys per elf using a data file called elves.dat and placed the values in
two parallel arrays. You created a third parallel array of strings to record a rating for each elf.
For this program, you are going to create a 2D array containing the number of toys and corresponding
ratings. The rating will be a number from 0 to 5 instead of number of stars, based on the table below:
Ratings:
Toys Made Rating
500 or more 5
between 300 and 499 3
between 200 and 299 1
under 200 0
The program should read into the arrays. It should look at the number of toys made by each elf and record this
number, along with a rating in the 2D array. The elf’s name will still go into a parallel array.
You will add the ability for the user to search for a particular elf. The program should ask the user for an elf
name, and should print the number of toys and rating for that elf.
As in the previous program, the program should then print out the arrays side by side in neat, labeled columns.
For this program, the output should be printed in descending order by number of toys made. So, the first elf
listed should be the one that made the most toys. It should still display the total number of toys made by the
elves and the number of elves who made more than 500 toys. Since the elves are printed in order by the number
of toys made, it is no longer necessary to print the name of the elf who made the most toys or the name of the
elf who made the least toys.
The program should also calculate and print the average number of toys made by the elves.
The goal of this program is to determine the impact of changing from 3 parallel arrays to 2 parallel arrays -
where one array is now 2D.
Please post a small sample of your input file.
Smiley 662
Curley 88
Clementine 335
Jasper 105
Lucinda 775
Brunhilda 103
Florence 441
Oskar 820
Snowflake 990
Bernard 690
Punch 298
Chuckie 10
Frosty 102
Snowman 311
April 830
Merry 299
Sunshine 331
Buddy 1234
Carol 271
Misty 111
Harold 52
Henry 292
Twinkle 308
Starlight 703
Burr 112
Angelica 444
Bluenose 689
Harry 254
Twinkle 259
Stardust 121
Greensleeves 453
Noel 312
Happy 209
Yukon 534
Snowcap 190
Northpole 598


this is the whole file.
Topic archived. No new replies allowed.