Exception thrown

I'm learning C++ (it's also my first computer language), and don't know what the following means:

Exception thrown at 0x0FFD46EE (vcruntime140d.dll) in Project8.exe: 0xC0000005: Access violation reading location 0x0AB54DB8. occurred

The funny thing is I used the exact few lines of code in other areas (just with variable name changes) and it worked fine, so I'm not sure what I'm doing wrong. Any help appreciated.

Here's the code (the exception was thrown at line 137 assuming the line numbers stay the same after I paste it):

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
#include <string>
#include <ctime>
#include <fstream>

using namespace std;

class Item
{
private:
	std::string name, description;
	//If status is 'true' the item is the murder weapon.
	bool status;
	// 'rft' stands for 'read from text'. These are place keepers for reading from the description file
	int rft_begin, rft_end;

public:
	//Constructor
	Item(std::string, bool);

	//Destructor
	~Item();

	//Fetch name of item
	std::string getName();

	//Fetch description of item
	void getDescription(std::string);

	//Fecth status - true for murder weapon, else false
	bool getStatus();
};

//Constructor - being passed name and status (murder weapon or not)
Item::Item(std::string iName, bool iStatus)
{
	name = iName;
	status = iStatus;
}

//Destructor - not used
Item::~Item() {}

//Function to return item name
std::string Item::getName()
{
	return name;
}

//Function to return item status - is it the murder weapon? (true for yes)
bool Item::getStatus()
{
	return status;
}

//Function to return item descriptions
void Item::getDescription(string iName)
{
	//Opening text file
	string tempName = iName;
	std::string text;
	fstream readText;
	readText.open("Item.txt");
	int i = 0; //For keeping track of iterations

			   /*Was going to use a switch statement here but they can't parse strings, so will
			   have to use if statements*/

	if (readText.is_open())
	{

		while (!readText.eof()) //Will loop until the eof (end of file).
		{
			getline(readText, text);
			i++;
			if (tempName == "tortoise" && i < 3)
			{
				cout << text << endl;
			}
			else if (tempName == "ping pong bat" && i >= 3 && i < 6)
			{
				cout << text << endl;
			}
			else if (tempName == "shetland pony" && i >= 6 && i < 12)
			{
				cout << text << endl;
			}
			else if (tempName == "orange peeler" && i >= 12 && i < 14)
			{
				cout << text << endl;
			}
			else if (tempName == "bedazzled laptop" && i >= 14 && i < 22)
			{
				cout << text << endl;
			}
			else if (tempName == "Hendrix-enchanted-guitar" && i >= 22 && i < 28)
			{
				cout << text << endl;
			}
			else if (tempName == "Beatles songbook" && i >= 28 && i < 31)
			{
				cout << text << endl;
			}
			else if (tempName == "milk-bottle-of-death" && i >= 31 && i < 34)
			{
				cout << text << endl;
			}
		}
		system("pause");
	}
	readText.close();
}



int main()
{
	


	//Array of items to be randomised 
	string aItems[8] = { "tortoise", "ping-pong-bat", "shetland-pony",
		"orange-peeler", "bedazzled-laptop", "Hendrix-enchanted-guitar",
		"Beatles-songbook", "milk-bottle-of-death" };

	//Variables for randomisation.
	int num = 0;
	srand(time(0));
	string sTemp;

	//Loop for randomising names.
	for (int i = 0; i < 12; i++)
	{
		//Randomising array of items
		num = (rand() % 8);
		sTemp = aItems[i];
		aItems[i] = aItems[num];
		aItems[num] = sTemp;
	}


	//Initialising Items.
	Item i0(aItems[0], false);
	Item i1(aItems[1], false);
	Item i2(aItems[2], false);
	Item i3(aItems[3], false);
	Item i4(aItems[4], false);
	Item i5(aItems[5], true); //Murder weapon.
	Item i6(aItems[6], false);
	Item i7(aItems[7], false);

	string itemName = i1.getName();
	//Debugging test
	
	cout << i1.getName() << endl;
	cout << i1.getStatus() << endl;
	i1.getDescription(itemName);



	system("pause");
	return 0;
}
0xC0000005: Access violation reading location something something
means "you have tried to read, or write, to memory that doesn't belong to your program".

When you run your program, the OS gives it some memory. If you try to read a memory address that isn't what the OS gave you, the OS will stop you. That's what's happened here.

It's usually done by using a pointer that is pointing at memory that isn't yours, or is a null (zero value) pointer. Also commonly done by taking an array or vector of size x, and trying to read/write well beyond x; trying to read or write beyond the limit of the array or vector.

1
2
3
4
5
6
7
8
9
//Loop for randomising names.
	for (int i = 0; i < 12; i++)
	{
		//Randomising array of items
		num = (rand() % 8);
		sTemp = aItems[i];
		aItems[i] = aItems[num];
		aItems[num] = sTemp;
	}


Tell me, given that aItems is of size 8 (so aItems[0] to aItems[7] exist), and i here runs from 0 to 12, do you see any problem when i=8 to 12? When you're trying to use aItems[8], or aItems[10], for example?
Last edited on
Just close the window from the last time you ran the program.
Ahh. Thanks Repeater. I condensed the code down to the essentials before I posted it here. The original has two more array randomisations in the same loop each with 12 elements. Such a stupid mistake. Have put it a separate loop now. Thanks again.
Topic archived. No new replies allowed.