0
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <stdlib.h>

using namespace std;

int gradeExam(string answerKeyARR[]);


int main()
{
    const int QUESTIONS = 10;
    const int MAX_STUDENTS = 250;

    ifstream inFile;
    ofstream outFile;
    inFile.open("grade_data.txt");
    outFile.open("grade_report.txt");

    string ansKey;
    inFile >> ansKey;

    string answerKeyARR[QUESTIONS];

//Loop for storing each answer into an array rather than all in a single string.
    for (int j = 0; j < QUESTIONS; j++)
    {
        answerKeyARR[j] = ansKey.substr(j,1);
    }

    int i = 0;
    int numStudents = 0;
    string studentAnswers[MAX_STUDENTS];

//Loop to read in all answers into array and count number of students.
    while (!inFile.eof())
    {
        inFile >> studentAnswers[i];
        numStudents++;
        i++;
    }

//WHY DOES IT CRASH HERE?!
    string studentAnswersARR[numStudents][QUESTIONS];

    for (int k = 0; k < numStudents; k++)
    {
        for (int l = 0; l < QUESTIONS; l++)
        {
            studentAnswersARR[k][l] = studentAnswers[l].substr(l,1);
cout << studentAnswersARR[k][l];
        }
    }

    inFile.close();
    outFile.close();
    return 0;
}

Okay, so basically once it gets to the part where it's removing the substring, it crashes. It works perfectly fine for retrieveing the answerkey answers, so why the hell is it crashing when it gets to this point? This is still a WIP for basic coding 2. Also, when I change variable 'l' to, say, position 0, it works. What gives?

  • 1
    `string studentAnswersARR[numStudents][QUESTIONS];` This isn't standard C++. It shouldn't compile. If it does compile, it is a non-standard extension with rules that are up to the compiler vendor. – PaulMcKenzie Feb 06 '15 at 23:25
  • Oh, it is not standard C++ due to you using a variable to denote the number of items in an array. Arrays must use a compile time constant when declaring them. – PaulMcKenzie Feb 06 '15 at 23:29

1 Answers1

0

There are multiple issues with your code that may cause problems.

First, your input loop is not bounded properly. It should stop at MAX_STUDENTS, but you fail to check for this limit.

Second, do not use eof() to check for the end of file. There are many posts on SO discussing this.

while (!inFile && i < MAX_STUDENTS)
{
    inFile >> studentAnswers[i];
    numStudents++;
    i++;
}

The next issue is the line you highlighted in your question. It probably crashes due to stack space being exhausted. But the code you have uses a non-standard C++ extension, and once you do that, then the rule as to what that line really does internally is up to the compiler vendor.

So to alleviate this with using standard C++, the following can be done:

#include <vector>
#include <string>
#include <array>
//...
    std::vector<std::array<std::string, QUESTIONS> > 
                               studentAnswersARR(numStudents);
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • Thanks for your quick response! I've made the changes you suggested, but I'm not sure I'm really understanding the new syntax of the array. Also, its still crashing anyway. – liquidTurtle Feb 07 '15 at 00:18
  • I don't think you need to break up `ansKey` into another vector. You can use the string as-is without having to do this. – PaulMcKenzie Feb 07 '15 at 00:30
  • I need it separated for later because I'll have to compare all the answers by student and add up all the wrong/right answers to return their score. – liquidTurtle Feb 07 '15 at 00:39
  • Also, shouldn't the number of answers the student give match the number of answers in the answer key? Why is your array of student answers `MAX_STUDENTS` size instead of `QUESTIONS` size? – PaulMcKenzie Feb 07 '15 at 00:40
  • My intent is to read in each line from the answers file. As far as my limited knowledge goes, there's no way to compare an entire string by each individual letter (So i can count correct answers/see if thery're right). So then I remove each answer (all 10) by using substr and put them into my new 2D array so I can compare them later. But, as we've established, this part is not currently working. It operates just like the loop for the answer key, so I don't understand why it's having a problem, as that one functions perfectly. – liquidTurtle Feb 07 '15 at 00:48
  • You can access individual characters in the string by simply using `[ ]`. Honestly, you're making it harder than it should be. There is no need to create "helper" arrays just to compare if character 'x' in one array is equal to character 'y' in another array. – PaulMcKenzie Feb 07 '15 at 00:50
  • Right, but I'm taking in all 10 answers you see. studentAnswers[X] would be 10 letters, not just one. – liquidTurtle Feb 07 '15 at 00:55
  • Please see this example. Again, there is no need to create arrays for no reason here: http://ideone.com/66rhU8 If it was the case that every time we need to process a string, we need to create separate arrays, I would have quit C++ coding. – PaulMcKenzie Feb 07 '15 at 01:05