1

I am working on a school project and I am trying to add each student object "objStudent[5]" to an array of pointers "classRosterArray". When I try to initialize the pointer in the constructor "Roster". I receive the warning "Index 5 is out of valid index range". This is my first time learning C++ so I'm not sure what to do.

This is the Roster class where I created the array of pointers "classRosterArray"

//roster.h
#pragma once
#include <string>
using namespace std;

class Roster {
private:
string* classRosterArray[5];
public:
    Roster();
    void ParseStudentData();
    void PrintAll();
};

The message "Index out of range" appears in the Roster::Roster constructor

//roster.cpp
#include "roster.h"
#include "student.h"

//studentData table
const string studentData[5] = {
"A1,John,Smith,John1989@gm ail.com,20,30,35,40,SECURITY",
"A2,Suzan,Erickson,Erickson_1990@gmailcom,19,50,30,40,NETWORK",
"A3,Jack,Napoli,The_lawyer99yahoo.com,19,20,40,33,SOFTWARE",
"A4,Erin,Black,Erin.black@comcast.net,22,50,58,40,SECURITY",
"A5,Joe,Shmoe,jshmoe@google.com,34,20,30,40,SOFTWARE"
};

//Object for each student in the studentData table
Student objStudent[5];

Roster::Roster()
{
    classRosterArray[5] = nullptr; //index out of range
    classRosterArray[5] = new string[5]; //tried using this to solve issue taking stabs in the dark
}

//Function to parse each set of student data in the studentData table
void Roster::ParseStudentData() {
    for (int i = 0; i < 5; i++) {
        size_t rhs = studentData[i].find(",");
        objStudent[i].SetStudentID(studentData[i].substr(0, rhs));

        size_t lhs = rhs + 1;
        rhs = studentData[i].find(",", lhs);
        objStudent[i].SetFirstName(studentData[i].substr(lhs, rhs - lhs));

        lhs = rhs + 1;
        rhs = studentData[i].find(",", lhs);
        objStudent[i].SetLastName(studentData[i].substr(lhs, rhs - lhs));

        lhs = rhs + 1;
        rhs = studentData[i].find(",", lhs);
        objStudent[i].SetEmailAddress(studentData[i].substr(lhs, rhs - lhs));

        lhs = rhs + 1;
        rhs = studentData[i].find(",", lhs);
        objStudent[i].SetAge(stoi(studentData[i].substr(lhs, rhs - lhs)));      
    }
}

void Roster::PrintAll() {
    ParseStudentData(); //Call ParseStudentData function to parse studentData table
}
  • Welcome to StackOverflow. Hint: Array indexes start at 0, not 1. – HazardousGlitch Sep 10 '20 at 02:21
  • Welcome to Stack Overflow. In C++, arrays are zero-indexed. That means that `int A[5]` runs from `A[0]` to `A[4]`. You declare `string* classRosterArray[5];`, then try to write something to `classRosterArray[5]`, which is out of bounds. – Beta Sep 10 '20 at 02:21
  • Modern C++ rarely uses raw pointers, `new`, and `delete`. There are a number of fundamental flaws in the shown code. There is no reason for `classRosterArray` to be an array of five pointers, to be allocated with `new`. This does not accomplish anything useful except to leak memory and give every possible opportunity to cause memory corruption. Similarly, `new string[5]` is not how it would be allocatd. That creates an array of five strings. Which is not the same thing as five pointers to a string, which is what `classRosterArray` is. – Sam Varshavchik Sep 10 '20 at 04:23
  • Does this answer your question? [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – HazardousGlitch Sep 10 '20 at 10:26

1 Answers1

0

You're getting this error:

“Index is out of valid index range”

because when you create an array of 5 elements, you only have an index from 0 to 4.

The largest valid index to access your specific array is 4.

If you change this line:

classRosterArray[5] = nullptr;

to:

classRosterArray[4] = nullptr;

should fix it.

Geno C
  • 1,401
  • 3
  • 11
  • 26