Hi,
I am currently learning C#, but when I made a program that allows the user to guess a word (in Dutch that's called "Galgje") it repeats the while-statement twice, ignoring the scanf(). I searched with Google and found out that the program is too fast for the user input. I tried different functions for user input (gets() and getline()) with no luck.
Does anyone have a suggestion on how to fix this?
Thanks in advance,
~G
The code:
#include <stdio.h>
#define MAX_WORD_LENGTH 30
void prn_galg(int i) {
switch (i) {
case 0 :
printf("Amount of wrong letters: %d\n\n", i);
break;
case 1 :
printf("Amount of wrong letters: %d\n\n", i);
break;
case 2 :
printf("Amount of wrong letters: %d\n\n", i);
break;
case 3 :
printf("Amount of wrong letters: %d\n\n", i);
break;
case 4 :
printf("Amount of wrong letters: %d\n\n", i);
break;
case 5 :
printf("Amount of wrong letters: %d\n\n", i);
break;
case 6 :
printf("Amount of wrong letters: %d\n\n", i);
break;
case 7 :
printf("Amount of wrong letters: %d\n\n", i);
break;
case 8 :
printf("Amount of wrong letters: %d\n\n", i);
break;
case 9 :
printf("Amount of wrong letters: %d\n\n", i);
break;
case 10 :
printf("Amount of wrong letters: %d\n\n", i);
break;
}
}
main() {
/* Declaring variables */
char currentWord[] = {'.', '.', '.', '.', '.', '.', '\0'}; /* The dot word */
char guessWord[] = {'m', 'o', 'n', 'k', 'e', 'y', '\0'}; /* The word that needs to be guessed */
int errors = 0; /* Error amount, if its higher than 10 the loop stops */
bool guessedLetter = false; /* Boolean used to check wether the player entered a correct letter */
int i,n = 1;
char c;
char d;
printf("Welcome to Galgje, want to try to guess the word?\nYou will loose if you have more than 10 errors.\n\nThis is the word you need to guess: %s\n\n", currentWord);
while (currentWord != guessWord) {
printf("===== GUESS %d =====\n\n%s", n, "Enter a letter:");
scanf("%c", &c);
for (i = 0; i < 6; i++) {
if (guessWord[i] == c) {
currentWord[i] = c;
guessedLetter = true;
}
}
if (guessedLetter == false) {
errors++;
printf("\nThat letter was incorrect.\n\n");
} else {
guessedLetter = false;
printf("\nThat letter was correct.\n\n");
}
printf("%s%s\n\n", "The word including the letters you guessed: ", currentWord);
prn_galg(errors);
n++;
}
}