Unhandled exception at 0x00B9EBA2 in project.exe: 0xC0000005: Access violation reading location 0x0035145C.

Hello everyone!
I'm a beginner to c++ so hope I'm asking correctly and with the right information. The program I'm having issues with is a theatre seating chart where customers can buy tickets. It has a 15 x 30 2D seats structure array initialized in the class constructor. In main three questions are asked
Number of seats
Preferred row
Preferred starting seat

Main then calls the requestTickets function passing this information to it. requestTickets will check if seats exist and are available ('#' = exist, '*' = not available). requestTickets must return a string back to main. If seats don't exist and/or are not available it will send back the appropriate message starting with "Sorry." If no sorry is returned main will call the purchaseTicket.

Things of note:
Mac OSX 10.10.1
VMWare Fusion - Professional Version 7.0.1 (2235595)
Microsoft Visual Studio - 12.0.21005.1 REL

Other info: The build is always fine. When I select inside the range of the array for rows and seats it always runs fine. However there are times when I'm testing the requestTickets and select some input outside of the range in the array I will get "project.exe has stopped." When I debug it the issue is this line
double tot = (numTicks * seats[prefRow - 1][prefSeat - 1].seatPrice);

The interesting part is it's only when I select a row outside the range and it's never consistent. For example, I've done the following
Number tickets 15
preferred row 30
preferred seat 30
And it will crash and debug will point to the double tot. I then will shut down Visual Studio and VMWare, reboot my computer, try the same selections, and it will run fine giving me the appropriate "Sorry." If I stay in the program (it loops around) it will continue to run fine with any selections I choose. If I exit the program and go back in, it will crash with the selections above. I'm doing with for school as a project with two other people. They cannot recreate the issue.


The 0xC0000005 in my error message above is consistent every time it crashes. All research I've done online where it's a program issue had pointers that were not initialized correctly. Research also shows that 0xC0000005 could mean a RAM/computer program problem. I'm trying to figure out if it's the program that's the issue or not. I believe I posted all the relevant code below but let me know. Thanks in advance for any help you can give!

****TicketManager.h*****
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream> 
using namespace std;

const int SEATSINAROW = 30; 
const int ROWS = 15; 


class TicketManager		// TicketManager Class
{
private:
	struct SeatStructure
	{
		double seatPrice;
		char available;
		SeatStructure(double p = 0, char a = ' ')
		{
			seatPrice = p;
			available = a;
		}

	};

	int		seatSoldTotal,
			seatAvailTotal,
			numberOfSeats,
			rowNumber,
			startingSeatNumber;
	double	totalSales;
	
	SeatStructure seats[ROWS][SEATSINAROW]; //	Define 2D array of seats

public:
	TicketManager()			
	{
		getSeatData();
		getSeatPrices();
		seatSoldTotal = 0;
		seatAvailTotal = 0;
		numberOfSeats = 0;
		rowNumber = 0;
		startingSeatNumber = 0;
		totalSales = 0;
	}

	void getSeatData();		

	void getSeatPrices();		
	
	string displaySeats(bool forDisplay);
	
	string requestTickets(int ticketNum, int rowPrefer, int seatPrefer);	
	
	string ticketPurchase(int prefN, int prefR, int prefS);
	
	string salesReport();		
	
	void updateSeats();
	

	~TicketManager()	
	{
		updateSeats();
	}
};


****ticketSalesProgram******
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "TicketManager.h"		//	TicketManger header file
				
void displayMenu();				//	Function prototypes	
int getChoice();

int main()
{
	int		choice,
			ticketNum,
			rowPrefer,
			seatPrefer;
	string	sorry = "Sorry";
	char	answer;

	TicketManager ticketSales;	// Create TicketManager object

	do
	{
		displayMenu();			//	Display program menu
		choice = getChoice();	//	Get user choice
		if (choice != 4)		//	Program loop begins
		{
			if (choice == 2)
			{
				cout << "Enter the number of tickets you would like to purchase : \n";
				cin >> ticketNum;
				cout << "Enter the row that you prefer to sit in : \n";
				cin >> rowPrefer;
				cout << "Enter the desired starting seat number : \n";
				cin >> seatPrefer;
			}
			switch (choice)
			{
				case 1:	cout << ticketSales.displaySeats(1);		
				break;
				case 2:	
				{
						cout << ticketSales.requestTickets(ticketNum, rowPrefer, seatPrefer) << endl;

						size_t found = (ticketSales.requestTickets(ticketNum, rowPrefer, seatPrefer).find(sorry));
						//cout << "Found is at " << found << endl;  //Test the find
						if (string::npos == found)   
						{
							cout << "Would you like to purchase these tickets (type Y or N)? ";
							cin >> answer;
							if (answer == 'Y' || answer == 'y')
							{
								cout << ticketSales.ticketPurchase(ticketNum, rowPrefer, seatPrefer);
							}
							else
							{
								cout << "We hope to see you again!" << endl;

							}
							break;
						}
						else
							break;
			}
			case 3: cout << ticketSales.salesReport();
			}
		}
		cout << "\nPress enter to continue\n";		//	Clears screen after each run
		cin.ignore();
		cin.get();
		system("cls");
	} while (choice != 4);

	return 0;
}

//	displayMenu fuction.  Displays user menu for program
void displayMenu()
{
	string 	dash;
	dash.assign(40, '-');
	
	cout << dash << "\n";
	cout << setw(35) << "Theater ticket program menu\n";
	cout << dash << "\n";
	cout << "1.Display seating chart for theater\n";
	cout << "2.Request tickets\n";
	cout << "3.Print sales report\n";
	cout << "4.Quit program\n\n";
	cout << "Enter a choice : ";
}
//	getChoice function gets users choice, validates input.
int getChoice()
{
	int validChoice;

	cin >> validChoice;
	while (validChoice < 1 || validChoice > 4)
	{
		cout << "ERROR!You must make a valid choice from the menu above\n"
			<< "Enter a choice : \n";
		cin >> validChoice;
	}
	return validChoice;
}


*****TicketManager.cpp******
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//	Checks if tickets are available. Menu option # 2.
string TicketManager::requestTickets(int numTicks, int prefRow, int prefSeat)
{
	ostringstream requestOut;
	string requestReturn;
	int tick = numTicks,
		row = prefRow,
		seat = prefSeat,
		seatNotAvailable = 0,
		seatNotExist = 0,
		badSeat = 0,
		badRow = 0,
		count = 0;
	double tot = (numTicks * seats[prefRow - 1][prefSeat - 1].seatPrice);
Last edited on
Oh men edit that and use <> when writing the code!!!
Sorry about that!
I got it. In the code above after this line I have my checks on seats
double tot = (numTicks * seats[prefRow - 1][prefSeat - 1].seatPrice);
STATEMENTS FOR CHECK ON SEATS

So if I pick some crazy number of seats it's looking way outside of the array. I should have it AFTER my check on seats.

STATEMENTS FOR CHECK ON SEATS
double tot = (numTicks * seats[prefRow - 1][prefSeat - 1].seatPrice);


Topic archived. No new replies allowed.