Unhandled exception in .exe 0xC0000005 access violation

Hi all!
I'm trying to run this program, but at one point it crashes and gives me the following error:

Code:
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include<conio.h>
using namespace std;
const int maxtries=5;
int letterFill (char, string, string&);

int main(){
string name;
char answer, letter;
int wrongguess=0;
string word;

string words[] ={
"singapore",
"china",
"japan",
"vietnam",
"philippines",
"australia",
"indonesia"
};


menu:

srand(time(NULL));
int n=rand()% 10;
word=words[n];
string unknown(word.length(),'*');

system("CLS");
cout << "\n\n\t\t\tWelcome to Guess the Country!";
cout << "\n\n\t\t    Each letter is represented by a star.";
cout << "\n\n\t\tYou have to type only one letter in one try.\n\n\n";
cout<<"Press any key to proceed to the game..";
getch();

system("CLS");
cout << "\n\n\t\tYou have " << maxtries << " tries to try and guess the word.";
cout << "\n\t\t  ======================================";

while (wrongguess < maxtries){
      
cout << "\n\n\t\t\t\t" << unknown;
cout << "\n\n\t\tGuess a letter: ";
cin >> letter;


if (letterFill(letter, word, unknown)==0){
system("CLS");

cout << endl <<"\t\tWhoops! That letter isn't in there! Please enter another one." << endl;
wrongguess++;

}else{
 system("CLS"); 
cout << endl << "\t\tYou're guess is correct! Please enter another letter." << endl;
} 

cout <<"\t\tYou have " << maxtries - wrongguess;
cout << "guesses left." << endl;

if (word==unknown)
{
system("CLS");

cout << "\n\n\t\tCongratulations! You've guessed the correct Country!!! ";
cout << "\t\t\t\t\t Your answer was "<<word<<"\n\n";
cout << "Do you want to try again? (y/n): ";
cin>>answer;
if (answer=='y')
  goto menu;
else if (answer == 'n')
cout<<"Thank you! Please press any key to terminate the game.";
getch();
break;
}
}

if(wrongguess == maxtries){
system("CLS");
cout << "\nSorry, you have just exceeded the total amount of guess available." << endl;
cout << "The word was : " << word << endl;
cout << "Do you want to try again? (y/n): ";
cin>>answer;
if (answer=='y'){
  wrongguess=0;
  goto menu;
  }else if (answer == 'n'){
cout<<"Thank you! Please press any key to terminate the game.";
getch();
}
}


getch();
return 0;
}

//FUNCTION:
int letterFill (char guess, string secretword, string &guessword){
    
int i;
int matches=0;
int len=secretword.length();
for (i = 0; i< len; i++){

if (guess == guessword[i])
return 0;


if (guess == secretword[i]){
guessword[i] = guess;
matches++;
}
}
return matches;
}

Thank you for the help! :)
Does it happen every time or only sometimes?

My best guess off the cuff would be your code here:

1
2
int n=rand()% 10;
word=words[n];


Your variable n is obtaining a random value 1-10 and afterwards you are assigning a value to the variable called word from your array. Suppose that the random number turns up to be 9, for example. There are only 7 values inside of the array. So, when you try to access the 9th index, you will get an access violation.

Hope this helps!
Cheers,
Tresky
There's a good reason why "goto" has such a bad name. I didn't actually figure out what's wrong, but in the meantime, I've replaced the goto and added a bit of indentation. See below.

I compiled and ran this as-is on my system. I removed the conio.h, CLS and getch because they don't work on my (Linux) system. With the possible exception of "clear", this should build and run on windows too.


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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
//#include<conio.h>
using namespace std;
const int maxtries=5;
int letterFill (char, string, string&);

int main()
{
	string name;
	char answer, letter;
	int wrongguess=0;
	string word;

	string words[] =
	{
		"singapore",
		"china",
		"japan",
		"vietnam",
		"philippines",
		"australia",
		"indonesia"
	};


	bool goOn = true;
	while (goOn)
	{
		srand(time(NULL));
		int n=rand()% 10;
		word=words[n];
		string unknown(word.length(),'*');
		
		system("clear");
		cout << "\n\n\t\t\tWelcome to Guess the Country!";
		cout << "\n\n\t\t    Each letter is represented by a star.";
		cout << "\n\n\t\tYou have to type only one letter in one try.\n\n\n";
		cout<<"Press any key to proceed to the game..";
		cin.get();
		
		system("clear");
		cout << "\n\n\t\tYou have " << maxtries << " tries to try and guess the word.";
		cout << "\n\t\t  ======================================";
		
		while (wrongguess < maxtries)
		{
			cout << "\n\n\t\t\t\t" << unknown;
			cout << "\n\n\t\tGuess a letter: ";
			cin >> letter;
			
			if (letterFill(letter, word, unknown)==0)
			{
				system("clear");
				
				cout << endl <<"\t\tWhoops! That letter isn't in there! Please enter another one." << endl;
				wrongguess++;
				
			}
			else
			{
				 system("clear"); 
				cout << endl << "\t\tYou're guess is correct! Please enter another letter." << endl;
			} 
		
			cout <<"\t\tYou have " << maxtries - wrongguess;
			cout << "guesses left." << endl;
		
			if (word==unknown)
			{
				system("clear");
				
				cout << "\n\n\t\tCongratulations! You've guessed the correct Country!!! ";
				cout << "\t\t\t\t\t Your answer was "<<word<<"\n\n";
				cout << "Do you want to try again? (y/n): ";
				cin>>answer;
				
				if (answer=='y')
				{
			  		goOn = true;
		  		}
				else if (answer == 'n')
				{
					goOn = false;
					cout<<"Thank you! Please press any key to terminate the game.";
					cin.get();
				}
				cin.get();
				break;
			}
		}
		
		if(wrongguess == maxtries)
		{
			system("clear");
			cout << "\nSorry, you have just exceeded the total amount of guess available." << endl;
			cout << "The word was : " << word << endl;
			cout << "Do you want to try again? (y/n): ";
			cin>>answer;
			if (answer=='y')
			{
			  wrongguess=0;
			  goOn = true;
			}
			else if (answer == 'n')
			{
				goOn = false;
				cout<<"Thank you! Please press any key to terminate the game.";
				cin.get();
			}
		}
	}
	
	cin.get();
	return 0;
}

//FUNCTION:
int letterFill (char guess, string secretword, string &guessword)
{
	int i;
	int matches=0;
	int len=secretword.length();
	for (i = 0; i< len; i++)
	{
		if (guess == guessword[i])
			return 0;
		
		if (guess == secretword[i])
		{
			guessword[i] = guess;
			matches++;
		}
	}

	return matches;
}
Doh!

@Tresky is right of course.

words[] only goes from 0 - 6, but n can go up to 9! Maybe try b = rand() % 7; instead.
Please! No applause. ;)
Thanks mate! Another question. What if the user entered SINGAPORE instead of singapore in the program? My program cannot consider capital letters. Please help. Thanks!
You can use the ASCII table for this. The ASCII table is a table of symbols that are associated with a number. C++ recognizes characters as numbers as well, so we can do something handy.

http://www.asciitable.com/

A capital letter on the table is always 32 spaces below its lowercase couterpart, so whenever you are checking for matches, check for matches 32 spaces down too... Like so...

1
2
3
4
5
6
7
8
if (guess == guessword[i] || guess == guessword[i] - 32)
    return 0;
		
if (guess == secretword[i] || guess == secretword[i] - 32)
{
    guessword[i] = guess;
    matches++;
}
Nice! Last question. I entered SINGAPORE, It is correct but it dosen't go to the prompt "Congratulations! You've guessed the correct Country!!!" What should I do? Thank you again. :)
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
#include <iostream>
#include <ctime>
#include <string>
#include<conio.h>
using namespace std;
const int maxtries=5;

//PRE-DEFINED FUNCTION(S):
int letterFill (char, string, string&);

//MAIN
int main(){
string name;
char answer, letter;
int wrongguess=0;
string word;

string words[] ={
"singapore",
"china",
"japan",
"vietnam",
"philippines",
"australia",
"indonesia",
"malaysia",
"taiwan",
"switzerland",
};


menu:
srand(time(NULL));
int n=rand()% 10;
word=words[n];
string unknown(word.length(),'*');

system("CLS");
cout << "\n\n\t\t\tWelcome to Guess the Country!";
cout << "\n\n\t\t    Each letter is represented by a star.";
cout << "\n\n\t\tYou have to type only one letter in one try.\n\n\n";
cout<<"Press any key to proceed to the game..";

getch();

system("CLS");

cout << "\n\n\t\tYou have " << maxtries << " tries to try and guess the word.\n\n";
cout << "\t\t\t NOTE: USE SMALL LETTERS ONLY! ";
cout << "\n\t\t  ======================================";

while (wrongguess < maxtries){

cout << "\n\n\t\t\t\t" << unknown;
cout << "\n\n\t\tGuess a letter: ";
cin >> letter;


if (letterFill(letter, word, unknown)==0){
system("CLS");

cout << endl <<"\t\tWhoops! That letter isn't in there! Please enter another one." << endl;
wrongguess++;

}else{
 system("CLS"); 
cout << endl << "\t\tYou're guess is correct! Please enter another letter." << endl;
} 

cout <<"\t\tYou have " << maxtries - wrongguess;
cout << "guesses left." << endl;

if (word==unknown)
{
system("CLS");

cout << "\n\n\t\tCongratulations! You've guessed the correct Country!!! ";
cout << "\t\t\t\t\t Your answer was "<<word<<"\n\n";
answer:
cout << "Do you want to try again? (y/n): ";
cin>>answer;
if (answer=='y' || 'Y'){
  goto menu;
}else if (answer == 'n' || 'N'){
cout<<"Thank you! Please press any key to terminate the game.";
getch();
return 0;
}else{
cout<<"Error! Please enter the correct letter! ";
goto answer;
}
}
}

if(wrongguess == maxtries){
system("CLS");
cout << "\nSorry, you have just exceeded the total amount of guess available." << endl;
cout << "The word was : " << word << endl;

cout << "Do you want to try again? (y/n): ";
cin>>answer;
if (answer=='y' || 'Y'){
  wrongguess=0;
  goto menu;
  }else if (answer == 'n' || 'N'){
cout<<"Thank you! Please press any key to terminate the game.";
getch();
return 0;
}
else{
cout<<"Error! Please enter the correct letter! ";
goto answer;
}
}


getch();
return 0;
}//END OF MAIN

//FUNCTION(S):
int letterFill (char guess, string secretword, string &guessword){
    
int i;
int matches=0;
int len=secretword.length();
for (i = 0; i< len; i++){

if (guess == guessword[i] || guess == guessword[i] - 32)
    return 0;		
if (guess == secretword[i] || guess == secretword[i] - 32)
{
    guessword[i] = guess;
    matches++;
}
}
}
#bump
Your code would be much easier to read and understand if you adopted a sensible indentation policy.

Also, as others have already mentioned, avoid goto statements like the plague. They're very problematic. C++ provides some proper structural constructs that you can use to do this. I strongly recommend you learn them sooner, rather than later, so that you can get out of the bad habit of using goto when it's not necessary.

The problem you're seeing is almost certainly due to the fact that your letterFill() function is a mess. You only ever return a value if (guess == guessword[i] || guess == guessword[i] - 32) is true. If that condition is never true, then the code reaches the end of the function without returning a value, which will give you undefined behaviour.

If your compiler doesn't warn you about this, then I'm amazed. I'd recommend you find a better one.

If your compiler does warn you about this, then I'm amazed you ignored it.

EDIT: Also, subtracting 32 from the letter is a really clumsy way of trying to make your comparisons case-insensitive. You're much better off using tolower to convert the guessed character to lower-case before the comparison, since you know the actual words are all in lower-case.


Last edited on
Also, subtracting 32 from the letter is a really clumsy way of trying to make your comparisons case-insensitive. You're much better off using tolower to convert the guessed character to lower-case before the comparison, since you know the actual words are all in lower-case.


@MikeyBoy: Very good advice! I didn't even consider that. Thank you for correcting my advice. :)
Topic archived. No new replies allowed.