Memory problems w/ Hangman program

Hello! I'm having issues with this piece of code that I translated from C++ to C. The problem is that I'm running a code analysis on visual studio code and it returns no errors, but when I run the code it says the word is 87 characters and I get buffer overflows despite me trying to protect against them. If I can get any assistance, I'd really appreciated it.

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  #include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>

#define MAX_LENGTH 50

// function prototypes
/* The function instruct describes the use 
   and purpose of the program. */
void instruct();

/* the function getWord reads the word
   entered */
void getWord(char[]);

/* the function findLetter searches the word
   for a letter and returns the number of times
   the letter appears in the word */
int findLetter (char letter, int number, char word[]);

/* the function getGuess prompts the user for
   the letter and returns the letter */
char getGuess();

/* the function display displays the letters
   found in the word */
void display (const bool alphabet[]);

int main()
{
	// declare variables
	/* a string variable word to read in the word,
	   a character variable guess to enter the letter,
	   an integer variable number for the length of the word,
	   an integer variable occurrence for the number
	   of times the letter appears in the word,
	   and a Boolean array alphabet to indicate
	   if the letter has been found*/
	char word[MAX_LENGTH];
	char guess;
	int number, occurrence;
	bool alphabet[26] = {false};
	
	// call the function instruct
	instruct();
	// call the function getWord to read in the word
	getWord(word);
	// find the length of the word
	number = strlen(word);
	// display a message showing the length of the word
	printf("The word is %d characters\n", number);
	// call the function getGuess to read the letter
	guess = getGuess();

	// loop until a 0 is entered
	while (guess)
	{
		/* call the function findLetter to find the number
		   occurrences of the letter in the word */
		occurrence = findLetter(guess, number, word);
		// display the number of occurrences of the letter
		printf("There are %d %c's.",occurrence, guess);
		/* set the array element corresponding with the 
		   letter to true */
		if (occurrence)
			alphabet[guess-97] = true;
		// call the function getGuess to read the letter
		guess = getGuess();
	}
	display (alphabet);

	return 0;
}

// function prototypes
/* The function instruct describes the use 
   and purpose of the program. */

void instruct()
{
	printf("This program prompts the user for\t"
          "a word and then a letter.\t"
		  "The word is searched to find the letter.\t"
		  "The user can enter more letters until a\t"
		  "zero is entered.\nThe program displays the\t"
		  "number of times a letter is found, and all\t"
		  "the letters found in the word.\n\n"); 
}


/* the function display displays the letters
   found in the word */
void display (const bool alphabet[])
{
	printf("You found these letters\n");
	int temp = 0;
	/* loop through the array for the
	   letters found */
	for (int count = 0; count < 26; count++)
	{
		if (alphabet[count])
			printf("%c", alphabet[count]),'\t';
	}
	printf("\n");
}

/* the function getWord reads the word
   entered */
void getWord(char word[])
{
	printf("Please enter a word:\n");
	scanf_s("%c", word, MAX_LENGTH+1);
	return;
}

/* the function getGuess prompts the user for
   the letter and returns the letter */
char getGuess()
{
	char guess = '\0';
	while (guess < 97 || guess > 122)
	{
		printf("\nWhat letter would you like to guess? (Enter zero to quit.");
		scanf_s("%c", &guess, MAX_LENGTH+1);
		guess = tolower(guess);
		if (guess == '0')
		return 0;
	}
	return guess;
}
	
/* the function findLetter searches the word
   for a letter and returns the number of times
   the letter appears in the word */
int findLetter (char letter, int number, char word[])
{
	// declare variables
	/* declare an integer variable foundLetter 
	   initialized to zero to count the number of times 
	   the letter appears in the word, an integer
	   variable start initialized to 0 to indicate
	   the starting search location in the word,
	   an integer variable stop intialized to the
	   length of the word to indicate the end
	   location in the word, a string variable newWord
	   initialized to word to hold the current word
	   to be searched, a string::size_type variable
	   position to indicate if a match has been found */
	int foundLetter = 0, start= 0, stop = number;
	char newWord[MAX_LENGTH + 1];
	strcpy_s(newWord, MAX_LENGTH+1, word);
	bool position = false;
	
	// loop through for all uppercase and lowercase

	for (int i = 0; i < MAX_LENGTH; i++)
		newWord[i] = tolower(newWord[i]);

	while (newWord[start] != '\0')
	{
		if (newWord[start] == letter)
		{
			foundLetter++;
			start++;
		}
		else;
		start++;
	}
	// return the number of occurrences of the letter
	return foundLetter;
}
I think you want %s in that scanf, not %c
(in get word, that is).
Last edited on
Thank you! That's one issue fixed.

Now I'm getting an exception that my stack at the variable "word" is overflowing. I think it's with the strcpy, but I changed that around and still nothing.
I don't see anything. run it in your debugger or something to see where it is crashing. you could try taking the +1 off in the strcpy_s call. if you think its the strcpy, put an exit(0) there to see if it crashes before it quits, is a quick way to check.
Last edited on
Will give it a try. Thank you!
Topic archived. No new replies allowed.