Program to go through a menu won't compile

I hate vague errors where I don't even know where to start looking for an error. Error happens immediately and it says "Unhandled exception at 0x5981c9c7 (msvcr100d.dll) in experiment.exe: 0xC0000005: Access violation reading location 0xabababab."

Does any one have any clue on why this screwed up?

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

using namespace std;

void clear()
{
	system("cls");
}

class UI // User interface
{
private:
	int total_options, place; //Total amount of items in the menu / what place on the menu the user is currently at.
	string * menuarray; //This points to all the strings that the menu uses
	void MenuShift(int shiftamount);
	void display() const;
public:
	UI(int Total_options, const string * menuwords);
	~UI();
	
	int getcommand();
};

UI::UI(int Total_options, const string * menuwords)
{
	menuarray = new string[Total_options];
	total_options=Total_options;
	
	for(int i=0; i<=total_options; i++)
	{
		menuarray[i] = menuwords[i];
	}
	
	place = 0;
	this->display();
}

UI::~UI()
{
	delete [] menuarray;
	menuarray = NULL;
}

void UI::MenuShift(int shiftamount)
{
	place += shiftamount;
	if(place>total_options)
	{
		place = 0;
	}
	else if(place<0)
	{
		place = total_options;
	}
}

void UI::display() const
{

	
	for(int i = 0; i<=total_options; i++)
	{
		if(i<place || i>place)
		{
			cout<<menuarray[i]<<endl;
		}
		else if(i==place)
		{
			cout<<">"<<menuarray[i]<<endl;
		}
	}
}

int UI::getcommand()
{
	while(true)
	{
		char response;
		response=getch();

		if(response=='s')
		{
			this->MenuShift(1);
		}
		else if(response=='w')
		{
			this->MenuShift(-1);
		}
		else if(response=='\n')
		{
			return place;
		}
		clear();
		this->display();
	}
}
int _tmain(int argc, _TCHAR* argv[])
{
	string * arr;
	arr = new string[4];
	arr[0] = "Play";
	arr[1] = "Multiplayer";
	arr[2] = "Options";
	arr[3] = "Credits";
	UI testmenu(4, arr);
	int result = testmenu.getcommand();
	cout<<result<<endl;
	system("pause"); //I know this is bad. I'll fix it later
	return 0;
}
Line 31: your loop condition is wrong. Like it is now, it will loop 5 times, although the array is only 4 elements long. You should change <= to <
LIne 31 goes past the end of the array. This is the idiom to use:

for(int i=0; i < total_options; i++)

A big help to you would be to learn to use a debugger. You can watch your variables change value as you step through your code 1 line at a time, to see where it all goes wrong & deduce why.

With the error message: "Access violation reading location", means you are accessing memory you shouldn't, & array over run is a classic mistake.

If you can get hold of a clang compiler - it apparently has really nice messages. Just check whether your IDE can use it.

Hope all goes well :)
Topic archived. No new replies allowed.