-4

I am trying to allocate some arrays in my function, but it seems like my constructors are not being called?

void Ticket::load(ifstream & inputFile)
{
string a;
int ticketCount = -1;
int seatCount;
Ticket tickets[2];
// try to open the waitlist.txt file as input mode
//inputFile.open("hi.txt");
inputFile.open("hi.txt");



// cannot open the file
if (!inputFile.is_open())
    cout << "Cannot open the file.\n";
// open successfuly
else
{
    string category = "";
    string data = "";
    string name = ""; 
    string flightCode = "";
    int age;
    int seatNumber;
    int c;
    double d;
    double price = 0;

    inputFile >> category;
    inputFile >> data;

    // keep reading till the end of the file
    while (!inputFile.eof())
    {
        if (category == "Ticket:")
        {
            /*
            * Here we increment the index of the ticket in the array.
            * But in our actual Airplane Project we will use LinkedList
            * to store the Tickets, so we will dynamically allocate
            * memory whenever we read the word "Ticket:" in the file.
            * Other than that, the way of reading the file will be the same.
            */
            c = stoi(data);
                cout << "Ticket Number:"<< c <<endl;
            //Ticket::setTicket(tickets, c);

            tickets[++ticketCount].ticketNumber = c;
            seatCount = 0;
                }

        else if (category == "Size:")
        {


            c = stoi(data);

            tickets[ticketCount].groupSize = c;
            // allocate space for the seat array
            tickets[ticketCount].seatInfo = new Seat [c];
            tickets[ticketCount].seatInfo->reserver = new Person[c];


        }
        /*else if (category == "Flight:")
        {
            flightCode = data;
        }
        */
        else if (category == "Name:")
        {
            name = data;

                            /*
            * keep reading data for name because it may contain white spaces
            * stop whenever the data is "Age:" (new category)
            */

            inputFile >> data;
            while (data != "Age:")
            {
                name += " " + data;
                inputFile >> data;


            }

            /*
            * done reading the name
            * set the category to be the current data,
            * then go to the READ_DATA label to read in the new data
            */
            category = data;
            goto READ_DATA;
        }
        else if (category == "Age:")
        {
            age = stoi(data);

        }
        else if (category == "Seat:")
            seatNumber = stoi(data);
        else if (category == "Price:")
        {
            d = stod(data);
            price = d;

            // fill the seat in the array

                        tickets[ticketCount].seatInfo[seatCount].reserver[seatCount].name = name;
                tickets[ticketCount].seatInfo[seatCount].reserver[seatCount].age = age;
            tickets[ticketCount].seatInfo[seatCount].seatNumber =     seatNumber;
            tickets[ticketCount].seatInfo[seatCount++].price = price;

        }

        inputFile >> category;
    READ_DATA:  // label to jump to
        inputFile >> data;

    }

    // close the file after finish reading
    inputFile.close();


}
}

I get an index out of bounds error when I get to the bottom where I try to add them to my ticket list. I cannot seem to add any at seatCount = 2;.

            tickets[ticketCount].seatInfo = new Seat [c];
            tickets[ticketCount].seatInfo->reserver = new Person[c];

This is the format of my input file

Ticket: 01
Size: 1

Name: Namees
Age: 20
Seat: 34
Price: 100
---------- ----------
Ticket: 02
Size: 3

Name: Poop master
Age: 20
Seat: 23
Price: 100

Name: Gun Master
Age: 19
Seat: 21
Price: 100

Name: idccc
Age: 21
Seat: 22
Price: 100
---------- ----------
Jolta
  • 2,620
  • 1
  • 29
  • 42
Khi
  • 1
  • 2

1 Answers1

0

the lines are the ones causing the leak. When declaring new object arrays. you must make enough space for the object. Before the program was trying to setName and setAge in a array that wasn't yet memory allocated.

tickets[ticketCount].seatInfo[seatCount].getReserver().setName(name);
tickets[ticketCount].seatInfo[seatCount].getReserver().setAge(age);
Khi
  • 1
  • 2
  • If you're looking for a leak, then you might want to look at `Seat *seat = new Seat(seatNumber, price, new Person(name, age));`. That allocation gets leaked (the only thing that uses the `seat` pointer variable is a line of code that's commented out. Also, if `getReserver()`, `setName()`, or `setAge()` leak (or have some other bug), there's no way for anyone to help with that - you haven't posted the code for any of those functions. – Michael Burr Feb 14 '16 at 06:48