Declaring arrays

I haven't posted on this forum before, so sorry if there's some type of etiquette I'm missing, or if this problem has been addressed before. I can't find a solution to this, or perhaps I'm just not entering the right search terms. I'm creating a program to read information about class schedules at my school, reformat the information, and allow the user to search for specific semesters. There are eight fields of information. I'm reading the info from a text file using eight parallel arrays, but I'm having trouble declaring the arrays. I can run this code in one compiler (Dev-C++) with no problems, but I get errors when trying to compile it using Visual Studio stating that arrays must be declared with a constant value. I have a loop to run through the text file, with a counter to increment with each subsequent line, then I create a constant int equal to the counter, and declare the arrays of size equal to the constant int. Suggestions for getting around this problem would be a huge help to me. Here's the section of code in question:

// Counting the number of lines in the text file"CIS225HW1DA.txt");
	string countLine;
	int numberOfLines = 0;
	//Discarding the first line of the text file containing only column headings
	getline(inFileForLines, countLine);
	// Counting number of lines in data file
	while (!inFileForLines.eof()) {
          getline(inFileForLines, countLine);

	// Setting up parallel arrays with number of elements equal to number of lines in the data text file
	const int SIZE_OF_ARRAY = numberOfLines;
	string semesterCode[SIZE_OF_ARRAY], courseID[SIZE_OF_ARRAY], section[SIZE_OF_ARRAY], instructor[SIZE_OF_ARRAY], dow[SIZE_OF_ARRAY], time[SIZE_OF_ARRAY], building[SIZE_OF_ARRAY], room[SIZE_OF_ARRAY];

Use std::vector< std::string > instead.
What version of Visual Studio are you using?
The ability to declare an array based on a variable was introduced in C++11 and is not supported in VS2010.

Several ways around this.
1) Declare your arrays to be bigger than the max number of classes. Wasteful and requires knowing the maximum number of classes at compile time.

2) Read the file as you are to get the count and then use the new operator to allocate your arrays dynamically. Don't forget to release the storage when you're done.
string * semesterCode = new string [numberOfLines];

3) Use a STL collection such as a vector. Collections will adjust their size automatically and you don't need to read the file twice.
vector<string> semesterCode;
string code;
  semesterCode.push_back (code);

> he ability to declare an array based on a variable was introduced in C++11

Variable length arrays are a C99 feature; they have never been, and still is not allowed by standard C++.
Thanks for the correction.
I'm using VS 2010 for this project, which is for a class assignment. One of the requirements is to use parallel arrays, so I may have to just hard code in the size of the arrays - it doesn't seem like the right thing to do. I still don't understand why VS finds declaring a const from a variable to be a problem, as long as I only declare the const once. Anyway, thanks for the suggestions.
I still don't understand why VS finds declaring a const from a variable to be a problem

Initializing a const int from a variable is not the problem. That's ok.

The problem is trying to declare an array from a variable that is determined at run time. Before C99, compilers would allocate storage for an array at compile time.
If the array limit is variable (not known at compile time), the compiler doesn't know how much storage to allocate.
I see... So, I was misunderstanding the way the compiler works by assuming the compiled executable would allocate memory during run time? It accepts the const declaration but not the array declaration, even though the const is declared just fine. Thank you for clarifying.
Topic archived. No new replies allowed.