Need help with my final.

I'm a high school student taking c++. My final (month long project, my teacher asked me to ask for help here) requires me to pass an array to a function and use a .h file, among other things. Here is the error I get when I try to run my program: "rep movsd ;N - move all of our dwords"


Here is the void function that plays the Hangman game.
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
 void hangman(int outcount, int quest, string origword)
{
	srand(time(0));
	ifstream infile;
	string letter = "";
	string fileword = "";
	string list = "--------------------------";
	string stt = ""; 
	string displayword = "-----";
	char dashreplaced = 'N';
	char gameover = 'N';
	int mark = 0;
	
	int count = 0;
	int numincorrect = 0;
	int left = 10;//0 ORIGINAL
	int num = 0;
	int hume = 0;
	system("cls");

		string ranks[10]={"RAP GOD", "SHAKESPEARE", "NOBEL LAUREATE",
		"NEW YORK TIMES BESTSELLER",
		"POPULAR NOVELIST", "HIGH SCHOOL ENGLISH TEACHER", 
		"CROSSWORD KING", "LOCAL BLOGGER",
		"MEDIOCRE POET", "UNCULTURED FOOL"};
		
	rankheader r;
	
	if(quest==1)
	{
	num = rand() % outcount + 1;

     
	string snum = "";    // string which will contain the result


	ostringstream convert;   // stream used for the conversion

	convert << num;      // random num

	snum = convert.str(); 
	
	
	infile.open("bank.txt", ios::in);
	if (infile.is_open())
	{
		while (!infile.eof())
		{ 
			getline(infile, stt, '#');
			getline(infile, fileword);
			if (stt.substr(0,100)==snum.substr(0,100))
			{
				origword = fileword; // origword is the word that will be used in the hangman game. 
			}// what this has done is select a random word from the text file, and using it in the hangman game.
		} 
	}
	else
		cout << "File could not be opened. " << endl;
	int jacko = origword.length();
	displayword.resize(jacko, '-');
	hume = origword.length();
	system("cls");
	}
	else
	{
		int jacko = origword.length();
	displayword.resize(jacko, '-');
	hume = origword.length();
	system("cls");
	}
	cout << "Guess this word: " << displayword << endl;
	while (gameover == 'N')
	{
		cout << "Enter an uppercase letter: ";
		cin >> letter;

		mark = 0;

		dashreplaced = 'N';
		if (letter.length() != 1)
		{
			cout << "Make sure to enter ONE uppercase letter." << endl;
			left = 10 - numincorrect;
			cout << "You have " << left << " guesses remaining. " << endl;
		}
		else
		{
			for (int y = 0; y < 26; y += 1)
			{
				if (letter == list.substr(y, 1))
				{
					mark = 1;
				}
			}
			list.replace(count, 1, letter);
			count += 1;

			for (int x = 0; x < hume; x += 1)
			{
				if (origword.substr(x, 1) == letter)
				{
					displayword.replace(x, 1, letter);
					dashreplaced = 'Y';
				}
			}
			if (mark == 0)
			{
				if (dashreplaced == 'Y')
				{
					if (displayword.find("-", 0) == -1)
					{
					
						gameover = 'Y';
						cout << endl << "Yes, the word is " << origword << endl;
						cout << "Good work! " << endl;

						r.setrank(ranks, left);
						// I suspect the problem is here, when I pass it to the function.

					}
					else
					{
						cout << endl << "Guess this word: " << displayword << endl;
						dashreplaced = 'N';
					}
				}
				else
				{
					numincorrect += 1;
					left = 10 - numincorrect;
					cout << "You have " << left << " guesses remaining. " << endl;
					if (numincorrect == 10)
					{
						gameover = 'Y';
						cout << endl << "Sorry, the word is " << origword << endl;

						cout<<"Your rank is: BARELY LITERATE "<<endl;
					}
				}
			}
			else if (mark == 1)
			{
				left = 10 - numincorrect;
				cout << "You've used this letter before. " << endl;
				cout << "You have " << left << " guesses remaining. " << endl;
			}
		}
		
	}
}


Here is my .h file that processes the guesses it took the user to win the game, and gives them a rank that has a place in the string array.

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
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class rankheader
{
public:
	rankheader();
	void setrank(string[], int);
	void sendrank();
private:
	string level[10];
	int triesleft;
	string yri;
};
rankheader::rankheader()
{
	level[10]=" ";
	triesleft=0;
	yri=" ";
}
void rankheader::setrank(string ranks[],int left)
{
	for(int x=0; x<10; x++)
	{
			level[x]=ranks[x];
	}
	if(left<=10 && left >0)
	{
		triesleft=left-1;
		yri="Your rank is: ";
	}
	
}
void rankheader::sendrank()
{
	int count=0;
	for(int y=0; y<10; y++)
	{
		count=10-y;
		if(triesleft==count)
			cout<<yri+level[y]<<endl;
	}
}


thank you, and would appreciate a response.
Last edited on
are you asking this in the middle of a test?
No! It's a month long project, my teacher is sitting right next to me.
would you also mind telling us the line of code next to the error?

I.E. 172: rep movsd

assuming that's given?
Last edited on
The error is line 404.
Look at your constructor

level[10]=" ";

You already declared an array of size 10. Having the square brackets is redundant. Because levels is already declared, you are instead accessing the 11th element of an array. Thus, you are going out of bounds and retrieving inaccessible memory.

You need to do something more like this

1
2
3
for (int i = 0; i < 10; i++) {
level[i]=" ";
}


You have to be careful with arrays. I don't know of way to quickly auto-set the values that doesn't require looping.

Let me know if that fixes it.

Last edited on
Yes, that fixed it. Thanks for the help, very much appreciated.
Topic archived. No new replies allowed.