File into Array

This is only a small portion of my code that I am having problems with. I do not seem to know how to do this properly, I've read other threads on this topic in this forum but none of them actually worked in threading file contents into an array.
Here is the Data from the file:
Jason 10 15 20 25 18 20 26
Samantha 15 18 29 16 26 20 23
Ravi 20 26 18 29 10 12 20
Sheila 17 20 15 26 18 25 12
Ankit 16 8 28 20 11 25 21

This is what I have so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void openFile(ifstream& avgfile, double matrix[][NumberOfColumns])
{
	string inputFile;//playing around with you inputing file name yourself =)
	 cout << "Enter the input file name:"; 
	 cin >> inputFile; //opens file

	 avgfile.open(inputFile);

     if(!avgfile) //If not file or no file there then this will proceed
     {
         cout << "Input file opening failed."<< endl;
        	 }
	 while(!avgfile.eof())//while not end of file
    {
        for(int i=0; i<row; i++){
            for(int j=0; j<col; j++){
                avgfile >> matrix[i][j]; //stupid attempt to pass information from file into array
            }

    }
	 }


If you need the rest of the code let me know. This function has to pull the information from the file and input that info into an array, then I have to move the array around to other functions to complete a specific task.

Thanks!
Could i have the rest of the code?
I think it's time for u to use iterators xD
Not sure what an iterator is the instructions are for me to use a parallel array/2d array to output this:
Name Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Averages
Jason 10.00 15.00 20.00 25.00 18.00 20.00 26.00 19.14
Samantha 15.00 18.00 29.00 16.00 26.00 20.00 23.00 21.00
Ravi 20.00 26.00 18.00 29.00 10.00 12.00 20.00 19.29
Sheila 17.00 20.00 15.00 26.00 18.00 25.00 12.00 19.00
Ankit 16.00 8.00 28.00 20.00 11.00 25.00 21.00 18.43
Wants me to use three functions. I was wondering if maybe I should use pointers but my understanding of them is weak.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std; 
//declaring global variables
double matrix[4][8];
double avg;
const int NumberOfColumns = 8;
const int NumberOfRows = 4;
double row, col;
void openFile(ifstream& avgfile, double matrix[][NumberOfColumns]);//prototypes
void averages(ifstream& avgfile, double matrix[NumberOfRows][NumberOfColumns]);
void printMatrix(ifstream& avgfile, double matrix[NumberOfRows][NumberOfColumns]);

int main()
{
	ifstream avgfile;
	openFile (avgfile, matrix);// calling function to open file
	system ("pause");
return 0; 
}
void openFile(ifstream& avgfile, double matrix[][NumberOfColumns])
{
	string inputFile;//playing around with you inputing file name yourself =)
	 cout << "Enter the input file name:"; 
	 cin >> inputFile; //opens file

	 avgfile.open(inputFile);

     if(!avgfile) //If not file or no file there then this will proceed
     {
         cout << "Input file opening failed."<< endl;
        	 }
	 while(!avgfile.eof())//while not end of file
    {
        for(int i=0; i<row; i++){
            for(int j=0; j<col; j++){
                avgfile >> matrix[i][j]; //stupid attempt to pass information from file into array
            }

    }
	 }
	 averages(avgfile, matrix); //initializes function averages to calculate sum and average
}
void averages(ifstream& avgfile, double matrix[NumberOfRows][NumberOfColumns])//this function doesnt work at all
{
	 cout << fixed << showpoint;
	 cout << setprecision(2);
	 double sum=0;
	double avg=0;
	for (int row=0; row < NumberOfRows; row++){
		for (int col = 0; col < NumberOfColumns; col++){
	sum = sum + matrix[row][col];//
	avg = sum / 7; //finding the average of the matrix
		}
	printMatrix(avgfile, matrix);
	}
}
	 void printMatrix(ifstream& avgfile, double matrix[][NumberOfColumns])
	 { 
     cout << fixed << showpoint;
	 cout << setprecision(2);

	 //Next line is an attempt at making a 1d array to display this information
	 string Days[9]= {"Name", "Day 1", "Day 2", "Day 3", "Day 4", "Day 5", "Day 6", "Day 7", "Averages"};
		 
	 cout << Days; 

	cout << endl;

	cout << matrix[4][8];
	 
	 }

Topic archived. No new replies allowed.