Code getting stuck...

Hello all,

Some of you may have noticed that I have recently been posting some ridiculously simple questions for about a week, but I have really been putting forth the effort to comprehend the process and I have done much of just that since then. Here is my current problem; I am writing a project for school and the concept is a simple letter guessing game. I believe I have the program coded correctly and it does compile. However, when I run the code, it has no problem calling the Instructions() and printf/scanf (requests number of games from user) in the beginning of main(), but after it prints line 26 the program gets stuck and nothing happens. No error messages or crashes, it just sits there. I have no idea if this helps pinpoint the problem but two things I have noticed when I run the code are

1) The cursor is still on line 26. It looks like:
------------Game 1-------------_
with the underscore at the end being the blinking cursor.

2) The computer gets noticeably louder when I run the code and almost immediately quiets down back to normal when I exit the console.

Again, I have no idea if that info helps anyone with this, I am just trying to include as much as I can to assist with the issue.

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
#include <stdio.h>
#include <ctype.h>

#define MAX_GUESSES 5

void Instructions();
int PlayGuess(char solution);
char GetLetter();
int CompareLetters(char guess, char solution);


int main ()
{
	int GameCounter; int NumGames; char CorrectLetter; FILE* infile;
	
	infile = fopen("inputLet.txt", "r");
	
	Instructions();
	
	printf("Number of games you wish to play: ");
	scanf("%d", &NumGames);


	for (GameCounter=1; GameCounter<=NumGames; GameCounter++)
	{
		printf("------------GAME %d------------", GameCounter);
		fscanf(infile, "%c", &CorrectLetter);
		PlayGuess(CorrectLetter);
		if(PlayGuess(CorrectLetter)==1)
		{
			printf("Congratulations! You have made the correct choice.");
		}
		else
		{
			printf("You have run out of guesses and have lost the game.");
		}
	}

	return 0;
}

void Instructions ()
{
	printf("Hello, and welcome to the letter guessing game.\n");
	printf("You will be allowed five guesses for up to four games, depending on how many you wish to play.\n");
	printf("The goal is simple; just keep guessing different letters until \nyou choose the correct one.\n");
	printf("After each guess, you will be informed if the solution is alphabetically behind or ahead of your guess.");
	printf("If the correct letter is chosen, the game is over and you have won!\n");
	printf("If you fail to choose the correct letter after your fifth try, you lose.\n");
	printf("Okay, now lets begin.\n");
}

int PlayGuess(char solution)
{
	int NumGuesses=0; int WinOrLose=0;

	while (NumGuesses < MAX_GUESSES && WinOrLose==0);
	{
		
		char guess;
		guess = GetLetter();
		CompareLetters(guess,solution);
		if(CompareLetters(guess,solution)==1)
		{
			WinOrLose = 1;
		}
		else
		{
			NumGuesses++;
		}
	}
	if (WinOrLose==1)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

char GetLetter()
{
	char guess;
	printf("Please enter your letter guess: ");
	scanf("%c", &guess);
	guess = tolower(guess);
	return guess;
}

int CompareLetters(char guess, char solution)
{
	if(guess == solution)
	{
		return 1;
	}
	else
	{
		//numerize alphabet?
		if (guess < solution)
		{
			printf("Your guess is alphabetically before the correct letter.");
		}
		else
		{
			printf("Your guess is alphabetically after the correct letter.");
		}
		return 0;
	}

}



I have found this site to be an invaluable source of information, both from the tutorials/lessons and especially the forum users. I really appreciate all the assistance you all have given me and the time contributions that go along with that. Thanks again everybody.
Last edited on
Hi Justin,

Some small things that may help you:

When using any of the scanf functions, always make use of it's return value to check how successful the function call was.

Also, one has to get the format string exactly right - with correct spaces etc, otherwise there will be errors.

Unfortunately the example in this link doesn't show any checking.

http://www.cplusplus.com/reference/cstdio/scanf/

When opening files, always make sure that it worked by checking the file pointer returned is not equal to NULL.

http://www.cplusplus.com/reference/cstdio/fopen/


If you ever come to use fprintf, I like to to use sprintf first, see how how successful that was, then use fprintf to write the string in the file. Still check the return value for the fprintf call to see if it worked.

With line 24, the correct way to do something times NumGames is:

1
2
3
for (GameCounter = 0; GameCounter < NumGames; GameCounter++) {
     // your code
}


This will save you one day when you come to use arrays, as your current method will result in over stepping the array boundaries

Two other things to learn from this:

1. Always try to think of what might go wrong - and write code to take care of any reasonable error producing situation. Validating input data goes a long way towards avoiding problems;
2. Always read all the documentation for the functions you are using.

Hope all goes well 8+)
Last edited on
Thanks, TheIdeasMan.
I will definitely be looking over those sources when I have a moment. Do you have any idea what is going on with line 26 though? I keep trying to figure it out but I cannot see the issue.
Justin5978 wrote:
Do you have any idea what is going on with line 26 though? I keep trying to figure it out but I cannot see the issue.


The fscanf on line 27 probably didn't work - that is why you need to check the return value.

line 28 seems redundant - line 29 is better because it makes use of the return value.

Have you tried using a debugger like gdb? There is an article on this site on how to use it. This will show you the values of all your variables as you step through the code 1 line at a time. Your IDE might have it's own debugger with a GUI interface which should be easy to use. If this is all too hard try putting cout statements everywhere to see how the values change & deduce where things go wrong.

Another thing - rather than use a #define use a const int variable instead - put it in main().
Topic archived. No new replies allowed.