Before beginning, I must first say that I have already looked into possible solutions for this error. Unfortunately, they all have to do with not using arrays, which is a requirement for my project. Also, I'm currently taking Intro to CS so my experience is next to none.
The purpose of the array is to gather names from a file. So in order to initialize array, I count the number of names and use it as the size. The problem is the error stated in the title, but I don't see a way around it while still using a 1D array.
main.cpp
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <iostream>
#include "HomeworkGradeAnalysis.h"
using namespace std;
int main()
{
ifstream infile;
ofstream outfile;
infile.open("./InputFile_1.txt");
outfile.open("./OutputfileTest.txt");
if (!infile)
{
cout << "Error: could not open file" << endl;
return 0;
}
string str;
int numLines = 0;
while (infile)
{
getline(infile, str);
numLines = numLines + 1;
}
infile.close();
int numStudents = numLines - 1;
int studentGrades[numStudents][maxgrades];
string studentID[numStudents];
infile.open("./InputFile_1.txt");
BuildArray(infile, studentGrades, numStudents, studentID);
infile.close();
outfile.close();
return 0;
}
HomeworkGradeAnalysis.cpp
using namespace std;
void BuildArray(ifstream& infile, int studentGrades[][maxgrades],
int& numStudents, string studentID[])
{
string lastName, firstName;
for (int i = 0; i < numStudents; i++)
{
infile >> lastName >> firstName;
studentID[i] = lastName + " " + firstName;
for (int j = 0; j < maxgrades; j++)
infile >> studentGrades[i][j];
cout << studentID[i] << endl;
}
return;
}
HomeworkGradeAnalysis.h
#ifndef HOMEWORKGRADEANALYSIS_H
#define HOMEWORKGRADEANALYSIS_H
const int maxgrades = 10;
#include <fstream>
using namespace std;
void BuildArray(ifstream&, int studentGrades[][maxgrades], int&, string studentID[]);
void AnalyzeGrade();
void WriteOutput();
#endif
The text files are of simple format:
Boole, George 98 105 0 0 0 100 94 95 97 100
Each line is like this, with varying number of students.
What is another method to which I can still stream the names of the students while still using an array?
Declaring variable-length arrays can lead to a stack overflow and potential vulnerabilities in the program. Consider the example: void foo(size_t n) { int arr[n]; // .... } Transmission of large number 'n' can lead to a stack overflow as the array will become too large and take up more memory than it really is.
The biggest problem is that one can not even check for failure as they could with the slightly more verbose malloc'd memory. Assumptions in the size of an array could be broken two years after writing perfectly legal C using VLAs, leading to possibly very difficult to find issues in the code.
An Array must be declared with a constant value, you can't use a variable. if you want to declared it using variables you must use a dynamically allocated array.
string studentID[numStudents]; //wrong
string *studentID = new string[numStudents]; //right
EDIT: Be sure to free the array once your finished with it
delete [] studentID
Variable-lengthed arrays is not standard feature of the language. You have to allocate on the heap or create a vector or use a constant instead.
Besides. I got this error message from Clang, whereas g++-4.9 does't give it to me and compilation is ok. So it's compiler-dependent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With