"Exception thrown: read access violation"

Hello, I am new here! So I hope I format this all correctly.

I'm writing out some different chunks of code to decide how exactly I want to format my program for a class lab. I have this one piece written out that compiles without any errors. All the code seems to line up, everything works while debugging until the console goes to output my array. Then my debug crashes! To be honest, I don't know what about my code could be causing, but the error I get when it crashes is:

"Exception thrown: read access violation.
std::_String_alloc<std::_String_base_types<char,std::allocator<char> > >::_Mysize(...) returned 0xFDFDFDF5."


Sorry about all this code I'm posting, but I have no idea where in the code the problem is.

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
  #include<iostream>
#include<string>

using namespace std;

void main()
{
	int numOfPets;
	int categories = 4;
	enum categories {name, species, breed, gender};

	cout << "How many pets would you like to enter?" << endl;
	cin >> numOfPets;

	string** dynamArr = new string*[numOfPets];

	for (int i = 0; i < numOfPets; i++)
	{
		dynamArr[i] = new string[categories];

	}

	cin.ignore();
	for (int n = 0; n < categories; n++)
	{

		cout << "Please enter name of pet # " << n + 1 << ": " << endl;
		getline(cin, dynamArr[n][name-1]);

		cout << "Please enter species of pet # " << n + 1 << ": " << endl;
		getline(cin, dynamArr[n][species-1]);

		cout << "Please enter breed of pet # " << n + 1 << ": " << endl;
		getline(cin, dynamArr[n][breed-1]);

		cout << "Please enter gender of pet # " << n + 1 << ": " << endl;
		getline(cin, dynamArr[n][gender-1]);
	
	}

	for (int j = 0; j < numOfPets; j++)
	{

		for (int i = 0; i < categories; i++)
		{
			cout << dynamArr[i][j] << ' ';
		}

		cout << endl;

	}

	system("pause");
}
Line 24:
for (int n = 0; n < categories; n++)
categories should be numOfPets.

Lines 28 to 31 - remove the - 1 from the subscript [name-1] becomes [name] etc.


Line 46 j and i are transposed.

Last edited on
Thank you very much! The first point you made was the issue. Works like a charm now! The other two points you made were just things I'd forgotten to change back when trying to get the code to work. P:

Its always the small things!
Topic archived. No new replies allowed.