Receiving Access Violation

I am working on a project. The program is supposed display items from a vending machine. The user is supposed to be able to type in an item, pay an amount; then the vending machine gives the user their item. Well, the problem I am having is when I run the program it goes through the the whole process perfectly. Except when it prompts the user if they want to quit (either yes or no), windows promptly says the program has stopped working a must be closed. When debugging, I step through all of VendingMaching::run() and receive an access violation after receiving the user input and trys to execute this->display(); again at the beginning of the loop.

I have spent a considerable amount of time trying to figure this out by debugging and adjusting different things within the program, but I cannot find an adequate solution or any solution for that matter.

Any help and push in the right direction will be greatly appreciated. Thanks!

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
101
102
103
void VendingMachine::run()
{
	string inputItem = "";
	string quit = "no";
	int itemIndex = 0;
	string moneyStr = "";
	double money = 0.0;
	double amountDifference = 0.0;
	double additionalMoney = 0.0;


	do
	{
		this->display();
		cout << "Enter item name to purchase: ";
		getline(cin, inputItem);
		itemIndex = this->lookup(inputItem);

		if (itemIndex == -1)
		{
			cout << "Invalid item" << endl;
		}

		else
		{
			if (items[itemIndex]->getQuantity() == 0)
			{
				cout << "Item sold out" << endl;
			}
			else
			{

				cout << "Enter Money: $";
				cin >> moneyStr;
				money = strtod(moneyStr.c_str(), NULL);
				amountDifference = money - items[itemIndex]->getPrice();

				if (amountDifference < 0)
				{
					cout << "Enter  $" << abs (amountDifference) << " more: $";
					cin >> moneyStr;
					additionalMoney = strtod(moneyStr.c_str(), NULL);
					money += additionalMoney;
					amountDifference = money - items[itemIndex]->getPrice();
				}

				if (amountDifference > 0)
				{
					cout << "Here is your change: $" << fixed << showpoint << setprecision(2)
						<< amountDifference << endl;
				}

				cout << "Please take your " << items[itemIndex]->getName() << "." << endl;
				--(*items[itemIndex]);
				items[itemIndex]--;
			}
		}

		cout << "Quit? (yes or no) ";
		cin >> quit;
		system("cls");

	} while (quit != "yes" || "Yes");

}

int VendingMachine::lookup(string itemName)
{
        // linear search
	for (int i = 0; i < numItems; ++i)
	{
		if (items[i]->getName() == itemName)
		{
			return i; // item found
		}
	}

	return -1; // item not found
}

void VendingMachine::display()
{
	for (int i = 0; i < numItems; ++i)
	{
		items[i]->display();
	}
}
//destructor
VendingMachine::~VendingMachine()
{
	if (items != nullptr)
	{
		for (int i = 0; i < numItems; ++i)
		{
			delete items[i];
		}
	}

	delete[] items;
	items = nullptr;
	numItems = false;
}
Last edited on
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

It sounds like you have memory corruption - generally when memory is corrupted, either it is detected immediately, or it stays hidden until later in the program where it comes back into play. It is likely you are unlucky and it is not being detected immediately. Re-check all code that indexes into vectors/arrays and/or deal with pointers and make sure nothing is going out of bounds.
Thank you for the tip.

I just cannot seem to figure it out because everything works fine until the loop is either attempted to be exited (quit == "yes") or when it attempts to go back through for another iteration of the loop. It uses various virtual functions and inheritance, but I don't see any issues with those. I will adjust post to include destructor in case any abnormalities can be seen from that.. I have checked it numerous times and can't see any.
Woah, what's going on on line 95? The delete[] operator doesn't take any parameters inside the square brackets []
the purpose for that is to loop through the entire array and free the memory associated with each element. Then delete the entire array and etc. I figured that was the correct way to do it. But now that you say that that is likely the issue. Any idea on a correct way to do that or a point in the right direction? thank you for pointing that out.
Change delete[i] items; to delete items[i];?
Thank you for the tip. However, I am still getting an unhandled exception (0xC0000005: Access violation reading location 0x00000000.) It is still occurring in the same portion of the program and when trying to debug if I step into the display function on second call. I look to see what values are set to what and i noticed that _vfptr says it is unable to read memory...
Topic archived. No new replies allowed.