2D Arrays as a parameter

So I've been reading many forums here about this subject.

I am doing this for a programming assignment that my teacher assigned us.
However, she says that we cannot use global variables, so I try to put a value in the second array in my function as:

 
  int read(ifstream & inFile, string name[], double score[][6], int row)


but when I call it with the array that I am using in my main it is giving me an error saying "argument of type "double" is incompatible with parameter of type "double (*)[6]""

Here is the code that I am working on:

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;
int read(ifstream& inFile, string name[], double score[][6], int row);

int main()
{
	string studentName[10] = { " " }, grade[10] = { " " };
	double score[10][6] = { 0 };
	ifstream inFile;
	int row;
	read(inFile, studentName, score[10][6], row);
	
	
	return 0;
}

int read(ifstream & inFile, string name[], double score[][6], int row)
{
	row = 0;
	inFile.open("arrays.txt");

	for (int i = 0; i < 10; i++)
	{
		inFile >> name[i];
		cout << name[i] << '\t';
		row++;
		for (int j = 1; j < 6; j++)
		{
			inFile >> score[i][j];
			cout << score[i][j] << ' ';
		}
		cout << endl;
	}
	cout << row;
	return row;
}


Is there some other way to pass it, or am I missing something?
Last edited on
Line 15 should be read(inFile, studentName, score, row);

What you are doing is passing a single 2D array element into the function, not the entire array.

An element that is out of bounds in both dimensions. The 11th row, 7th column.

Without knowing what is in your data file I can't test the code to be sure it reads the data correctly.

If you are going to "hardwire" in your for loops the limit values to check against there is no need to pass the row value.

Especially since you are doing nothing with the function's return value in main().
Last edited on
Thanks Furry Guy, that helped clarify a lot of things and gives me a better understanding of arrays, also the rows is for another part of my assignment that I am working on so no need to worry about that, Thank you
I constantly "Frankenstein" code to see if a certain code approach will work, so there are parts and pieces from previous tests throughout my test code.
Topic archived. No new replies allowed.