Error "Expression: vector subscript out of range" when trying to print a vector

  2d-vector, c++

I am trying to read a file "chartIn.txt" that stores seats on a plane in the format:
1 A B C D
2 A B C D

I have read the file with method readChart() in the Plane class. It seams to read the file and store in 2d vector seatChart. When I enter Menu option 1 which prints the chart stored in seatChart, I get this error: Expression: vector subscript out of range.

At this line of code within printChart(),

cout << seatChart[i][j] << "  ";

This is what is shown when I return back to Visual Studio:
"Unhandled exception at 0x7B1FF2F6 (ucrtbased.dll) in Project1.exe: An invalid parameter was passed to a function that considers invalid parameters fatal."

When I print the chart inside of the readChart() method, it prints fine.
I am only getting the error when I am printing the chart from the actual printChart() method.

Also, I read from a different answer on here that the problem could be that the seatChart vector was not actually storing anything (which I thought may have made sense as I could access it within the method but once I was outside of it, I could not.) so I added "seatChart.resize(60);" under the vector declaration. This does not seam to make any difference.

#include "Plane.h"

//---------- Function Prototypes ----------

void DisplayMenu();
int getMenuItem(const string& message = "Please Enter Your Choice(1 - 7) : ");

//----------------- Main ------------------

int main()
{
    /*
     *Author: Alex Zuccarini
     *Creation Date: 4/17/2021
     *Modification Date: 4/17/2021
     *Purpose: Air Plane - Main
    */
    Plane honeysuckle;
    honeysuckle.readChart();



    DisplayMenu();

    int menuItem = getMenuItem();
    while (menuItem != 7){

        if (menuItem == 1) {
            honeysuckle.printChart();
            return 1;
        }
        else if (menuItem == 2) {
            return 2;
        }
        else if (menuItem == 3) {
            return 3;
        }
        else if (menuItem == 4) {
            return 4;
        }
        else if (menuItem == 5) {
            return 5;
        }
        else if (menuItem == 6) {
            return 6;
        }
        int menuItem = getMenuItem();
    }
    cout << "Thank you! Goodbye.";
    system("pause");
    return 0;
}


//--------------- Functions ---------------

void DisplayMenu() {
    cout << "------------------------ Menu ---------------------------" << endl;
    cout << "1. Display Seat Chart" << endl;
    cout << "2. Reserve Seat" << endl;
    cout << "3. Cancel Reservation" << endl;
    cout << "4. Save Seat Chart to File" << endl;
    cout << "5. Statistics" << endl;
    cout << "6. Help" << endl;
    cout << "7. Quit" << endl;
    cout << "---------------------------------------------------------" << endl;
}
// Validate Function

    // keep asking for an int until one is returned
    int getMenuItem(const string& message) {
        int userVal;

        cout << message;

        while (!(cin >> userVal) || userVal > 7 || userVal < 1) {
            cin.clear();    //must be before ignore
            cin.ignore(numeric_limits<streamsize>::max(), 'n');    //clear buffer
            cout << "Invalid inputn" << message;
        }

        return userVal;
    }

Plane.h

#pragma once
#include <iostream>
#include <string>
#include <sstream>
#include <vector> 
#include <fstream>  // File import/output

using namespace std;
class Plane {
private: 
    string planeChart = "chartIn.txt";
    


    //vector<Seat> seatChart;
public: 
    vector<vector<char>> readChart();
    void printChart();
    vector<vector<char>> seatChart;



};

Plane.cpp

#include "Plane.h"
#include <iostream>
#include <string>
#include <sstream>
#include <vector> 
#include <fstream>  // File import/output
using namespace std;

vector<vector<char>> Plane::readChart() {
    int vCount = 0;

    ifstream iFile(planeChart);
    if (!iFile.is_open()) {
        cout << "Failure to open file " << planeChart << endl;
        //return -1;
    }

    vector<vector<char>> seatChart(15, vector<char>(4, ' '));
    seatChart.resize(60);
    for (int i = 0; i < 15; i++) {
        iFile >> vCount; // Used to get rid of the first row on every line within "chartIn.txt".
        for (int j = 0; j < 4; j++) {
            iFile >> seatChart[i][j];
        }
    }
    cout << endl;


    iFile.close();

    //--------------------------------------
    for (int i = 0; i < 15; i++) {
        cout << i + 1 << "  ";
        if (i + 1 < 10)
            cout << " ";
        for (int j = 0; j < 4; j++) {
            cout << seatChart[i][j] << "  ";
        }
        cout << endl;
    }
    //--------------------------------------
    return seatChart;
}
void Plane::printChart() {
    for (int i = 0; i < 15; i++) {
        cout << i + 1 << "  ";
        if (i + 1 < 10)
            cout << " ";
        for (int j = 0; j < 4; j++) {
            cout << seatChart[i][j] << "  ";
        }
        cout << endl;
    }
}

Output & Error Message

Source: Windows Questions C++

LEAVE A COMMENT