I have a two dimensional array that I need to make into a global variable to be able to access it from other functions. I can't quite figure out what the correct syntax is.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
 
using namespace std;
 
void ReadStudents (string Students);
void ConvertToInt (string Students[]);
void main ()
{
string Students[10][3];
 
ReadStudents (Students[]);
ConvertToInt (Students[10][3]);
}
 
//Function to read in students Grades
//Read it into a two Dimensional Array
void ReadStudents (string Students[]) 
{ 
fstream infile; 
int i, j;

infile.open ("c:\\students.txt"); 
//While not EOF read the Tab seperated file
while (!infile.eof()) 
{ 
for (i = 0; i < 5; i ++)
{
for (j = 0; j < 3; j++)

infile >> Students[i][j];
}

}

infile.close (); 
}
 
//Function to convert third row to and int
void ConvertToInt (string Students[10][3])
{
int i;
int j = 2; 

for (i=0; i < 5; i++)
{
cout <<Students[i][j] << " ";
cout << endl;
}
}

Recommended Answers

All 3 Replies

I have a two dimensional array that I need to make into a global variable to be able to access it from other functions. I can't quite figure out what the correct syntax is.

Put them any where before the main function. Kind like the same way
you put prototypes of functions before main.

By the way, void main should be int main(void)

that seems to work, but how do I call the variable into the function

ie:

void function (stringarray[])
void function (stringarray[][])

none of these seem to work I get ']' syntax error

void function (stringarray[])
void function (stringarray[][])

void function (char stringarray[])
void function (char stringarray[][MAX])

MAX will be how many elements are in the array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.