Access Reading Violation

Im working on a BST homework, and I have been doing alright, but I keep running into this error that I have NO idea how to solve.

This is the piece of code that is causing errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int blank1, blank2, blank3, blank4, count, counter2, secondname;
	blank1 = blank2 = blank3 = blank4 = count = counter2 = secondname = 0;
	string fn, ln, add, digits,rest, ln2, fn2;
	char c[1];
	cout << "Inside Client_Address_Book Update\n";
	//hash_table[0].Update(s); 
	string tmp = s;
	char ary[200];
	strncpy_s(ary, tmp.c_str(), s.length());
	string therest = s.substr(2);
	count = s.length() - 8;
	int check = (int)ary[0];
	check -= 48;
	if (check == 3 || check == 6 || check == 7)
	{
		for (signed int i = 0; i < s.length() - 8; i++)
		{


There is more, but I think from looking at that part, it looks OK. It ran fine on a previous homework involving linked list. But on this BST homework it says access reading violation. The specific line of code having an error is this line

if (check == 3 || check == 6 || check == 7)
I check, and 'check' equals 1, which is fine because I have an 'else if (check == 1)' lower in the code. But it NEVER goes there. It reads the line
if (check == 3 || check == 6 || check == 7)
and then I get the error

Unhandled exception at 0x00031896 in : 0xC0000005: Access violation reading location 0x00000014.

and it goes into xstring (I dont know what this is, but it opens it up EVERYTIME and shows an arrow on this
1
2
3
4
	size_type size() const _NOEXCEPT
		{	// return length of sequence
		return (this->_Mysize);
		}


can someone please help me and explain how to fix the error? I thought it should work :(
Incase it matters, originally I had it as
 
if (ary[0] == '3' || ary[0] == '6' || ary[0] == '7')

which ary[0] was 1, just like check is now, but it gave the same error. :(
Is your code too large?
Can you post more of it(or entire code), I don't see any wrong spot right now.
My code is quite large. It's actually why I shrunk it down. But I will try to show the code pieces that are being used here.
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
int main()
{
	CAB My_Book;
	cout << "Please always enter LAST NAME FIRST, then first name. Thank you\n\n";
		int i = 0;
		string line;
		ifstream myfile("data.txt");
		if (myfile.is_open())
		{
			while (getline(myfile, line))
			{
				i++;
				My_Book.Insert(line);
			}
			myfile.close();
		}
		else
		{
			cout << "Unable to open file";
		}
// that code above reads a bunch of lines of information to store in the BST
	My_Book.Insert("John Smithson 777 Glades Road 207-2780");
	My_Book.Insert("1 John Smith Bruce Lee Comb Harry 555 Palmetto Park Road 555-3444");
// so this means that, since it starts with a 1, it should take the node of Bruce Lee, take the address, and phone number information down, delete the node, then add the node with Bruce Lee's information but with the name John Smith.

	return 0;

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void CAB::Insert(const string & s)
{ 
	string tmp = s;
	char ary[200];
	strncpy_s(ary, tmp.c_str(), s.length());
	if (ary[0] == '1' || ary[0] == '2' || ary[0] == '3' || ary[0] == '4' || ary[0] == '5' || ary[0] == '6' || ary[0] == '7')
	{
		Update(s);
		return;
	}

	int place = 0;
	place = (int)ary[0];
	place -= 65;
	hash_table[place].Insert(s);
};


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
void CAB::Update(const string & s)
{ 


	int blank1, blank2, blank3, blank4, count, counter2, secondname;
	blank1 = blank2 = blank3 = blank4 = count = counter2 = secondname = 0;
	string fn, ln, add, digits,rest, ln2, fn2;
	char c[1];
	//hash_table[0].Update(s); 
	string tmp = s;
	char ary[200];
	strncpy_s(ary, tmp.c_str(), s.length());
	string therest = s.substr(2);
	count = s.length() - 8;
	int check = (int)ary[0];
	check -= 48;
	if (check == 3 || check == 6 || check == 7)
	{
		for (signed int i = 0; i < s.length() - 8; i++)
		{
			if (blank1 == 0)
			{
				ln += ary[i];
				if (ary[i + 1] == ' ')
				{
					i++;
					blank1 = 1;
				}
			}
			else if (blank2 == 0 && blank1 == 1)
			{
				fn += ary[i];
				if (ary[i + 1] == ' ')
				{
					blank2 = 1;
					i++;
				}
			}
			else if (blank2 == 1 && blank1 == 1)
			{
				add = add + ary[i];
				counter2++;
			}
		}
		digits = s.substr(count);
		string combine = ln + " " + fn;
		char hi2 = ary[2];
		int hi = (int)hi2;
		hi -= 65;
		BST_Node *p = hash_table[hi].Search(combine);
		if (p == 0)
		{
			cout << "\n Not found! Can not update! Did you enter last name first? Sorry!\n";
			return;
		}
		if (check == 3)
		{
			p->address = add;
			p->phone_number = digits;
			return;
		}
		else if (check == 6)
		{
			p->address = add;
			return;
		}
		else if (check == 7)
		{
			p->phone_number = digits;
			return;
		}
	}
	else if (check == 4 || check == 5 || check == 1 || check == 2)
	{
	
		//code here for if its 1,2,4,5
	}

};
Last edited on
Topic archived. No new replies allowed.