Dan
Dan

Reputation: 1

C++ static void _Xout_of_range("invalid string position")

[Solved]Edit2: So I figured out the issue and replaced the at function by putting the array strings into a string to assess the positions in the string. THis still caused an issue when reading spaces, so I forced the program to continue.

Edit1: So I made some changes to it and the errors went away and it compiles. However, the point array seems to end up always being 0. Everything else seems to work now except the points being carried.

Output

I am fairly new to C++ and programming, I am currently taking a college level class on C++ and coming across issues dealing with establishing the below code as a dynamic array. The exercise requires utilizing dynamic arrays so I am stuck with utilizing that. I built the below code and tried everything to try to figure out what my issue is, I think I pinpointed it to the calculatePoints() function. I established cout positions to output the data being stored to see if I can figure out the issue and it crashes before any output comes out of that function.

The error i receive reads:

Unhandled exception at 0x00007FFAB656543C in ConsoleApplication5.exe: Microsoft C++ exception: std::out_of_range at memory location 0x000000FCDD8FF620.

The program is supposed to read from a file (data below) and then store it in a dynamic array. Based off output I see, this is occuring.

TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF
DEF56278 TTFTFTTTFTFTFFTTFTTF
ABC42366 TTFTFTTTFTFTFFTTF
ABC42586 TTTTFTTT TFTFFFTF

The calculatePoints() function is supposed to read every position within the given array (every row after the key and after the student ID's) and compare it to the first row of the array (the answer key) to see whether it is the same, different or a space. Based off those values, points are accrued to assess a percentage and grade in another function. These are then outputted to screen along with the key, student ID and test results. This is what is required of the exercise.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
    int *points;
    double *percent;
    string *submission;
    int questions = 0;
    int rows = 5;
    char *grade;

    void getData(string * sub, int s);
    void calculatePoints(string * sub, int* pts, int s, int q);
    void calculateGrade(int* pts, int s, double* per, char* g);
    void printData(string * sub, int* pts, char* g, int s);

    cout << " Enter the number of questions for the test: " << endl;
    cin >> questions;
    cout << " Enter the number of students: " << endl;
    cin >> rows;
    rows = rows + 1;
    submission = new string[rows];
    percent = new double[rows];
    grade = new char[rows];
    points = new int[rows];

    for (int p = 0; p < rows; p++)
    {
        points[p] = 0;
    }

    getData(submission, rows);
    calculatePoints(submission, points, rows, questions);
    calculateGrade(points, rows, percent, grade);
    printData(submission, points, grade, rows);

    return 0;
}

    void getData(string* sub, int s)
    {
        ifstream inFile;
        inFile.open("Ch12_Ex2Data.txt");
        for (int row = 0; row < s; row++)
        {
                getline(inFile, sub[row]);
        }
        inFile.close();
    }

    void calculatePoints(string* sub, int* pts, int s, int q)
    {
        string answerKey = sub[0];
        for (int r = 1;r < s;r++)
        {
            string student = sub[r];
            for (int i = 0; i < q; i++)
            { 
                if(i + 9 >= student.size()) continue;
                if (pts[r] < 0)
                {
                    pts[r] = 0;
                }
                if (student[i + 9] == ' ')
                {
                    pts[r] += 0;
                }
                if (student[i + 9] == answerKey[i])
                {
                    pts[r] += 2;
                }
                if (student[i + 9] != answerKey[i] && student[i + 9] != ' ')
                {
                    pts[r] = pts[r] - 1;
                    if (pts[r] < 0)
                    {
                        pts[r] = 0;
                    }
                }
            }
        }
    }
        

    void calculateGrade(int* pts, int s, double* per, char* g)
    {
        for (int j = 1;j < s;j++)
        {
            per[j] = (pts[j] / static_cast<double>(40)) * 100;
            if (per[j] <= 100 || per[j] >= 90)
            {
                g[j] = 'A';
            }
            if (per[j] < 90 || per[j] >= 80)
            {
                g[j] = 'B';
            }
            if (per[j] < 80 || per[j] >= 70)
            {
                g[j] = 'C';
            }
            if (per[j] < 70 || per[j] >= 60)
            {
                g[j] = 'D';
            }
            if (per[j] < 60)
            {
                g[j] = 'F';
            }
        }
    }
    
    void printData(string* sub, int* pts, char* g, int s)
    {
        cout << "Processing Data" << endl;
        cout << "Key: " << sub[0] << endl;
        for (int z = 1; z < s; z++)
        {
            cout << sub[z] << " " << pts[z] << " " << g[z] << endl;
        }
    }

I have tried numerous things to resolve this, even storing the key to a seperate array which did not work. I also started out with not utilizing functions and instead just if and for loops, but went the function route to better assess where the issue is. Any help to get past this error is much appreciated. Expectations of the program from the class: Redo Programming Exercise 6 of Chapter 8 using dynamic arrays. The instructions have been posted for your convenience.

The history teacher at your school needs help in grading a True/False test. The students’ IDs and test answers are stored in a file. The first entry in the file contains answers to the test in the form:

TFFTFFTTTTFFTFTFTFTT

Every other entry in the file is the student ID, followed by a blank, followed by the student’s responses. For example, the entry:

ABC54301 TFTFTFTT TFTFTFFTTFT

indicates that the student ID is ABC54301 and the answer to question 1 is True, the answer to question 2 is False, and so on. This student did not answer question 9 (note the empty space). The exam has 20 questions, and the class has more than 150 students. Each correct answer is awarded two points, each wrong answer gets one point deducted, and no answer gets zero points. Write a program that processes the test data. The output should be the student’s ID, followed by the answers, followed by the test score, followed by the test grade.

An example of the program is shown below:

Processing Data Key: TTFTFTTTFTFTFFTTFTTF ABC54102 T FTFTFTTTFTTFTTF TF 27 D Remember, a space indicates that a question has been skipped. If spaces are added for formatting, there are too many or too few, it may affect the accuracy of your program.

Assume the following grade scale: 90%–100%, A; 80%–89.99%, B; 70%–79.99%, C; 60%–69.99%, D; and 0%–59.99%, F.

Upvotes: 0

Views: 165

Answers (0)

Related Questions