Odd error

... Any obvious reason I'm getting "First-chance exception at 0x003E5EBB in Clue.exe: 0xC0000005: Access violation reading location 0x003F4000."?

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
 #include <iostream>
#include <string.h>   // strlen()
#include <stdlib.h>  // system()


char words[50] = "Knife Wrench Candlestick Rope Pipe Revolver";
char word[50];
char compare = ' ';

int main()
{
	
	/*
	Buffer words table into word until it meets a space, then compare it with the user input
	*/
	int inc = 0;

	for (int i = 0; strlen(words); ++i){
		if (words[i] != compare){
			inc++;
			std::cout << words[i];
		}
		else if (words[i] == compare){
			std::cout << "\n";
		}
	}

	std::cout << "\n";
	system("Pause");
}
line 18: What is your condition to end the for-loop?
Last edited on
The length of the character array, 'words'... Is that incorrect? It throws the same error with ++i and i++
The loop should end once it gets to the end of the char array.

Therefore the ending condition should be: i < strlen(words)
Last edited on
Cool, thanks!

Here's the next question, why doesn't this print out the input back to the user if it matches the word in 'words'?

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
#include <iostream>
#include <string.h>  // strlen()
#include <stdlib.h>  // system()


char words[50] = "Knife Wrench Candlestick Rope Pipe Revolver";
char word[15];
char compare = ' ';
char input[15];

int main()
{
	
	/*
	Buffer words table into word until it meets a space, then compare it with the user input
	*/
	std::cin >> input;

	int inc = 0;
	for (int i = 0; i <= 43; i++){
		if (words[i] != compare){
			word[inc] = words[i];
			inc++;
		}
		else if (words[i] == compare){
			inc = 0;
			int len = strlen(word);
			for (int x = 0; x <= len; x++){
				word[x] = ' ';
			}
		}
	}

	std::cout << "\n";
	system("Pause");
}
why doesn't this print out the input back to the user if it matches the word in 'words'?


Because you haven't written any code to compare the input with 'words'.
Topic archived. No new replies allowed.