So, I'm new to C and I have this homework from college that I need to simulate a hangman game, at first I'd made the entire code in an online compiler and it worked all good, but then I decided to download a local compiler and somehow the same code doesn't work :(
First problem: it doesn't recognize the first letter as "a", but recognizes all the others in sequence
Sec problem: when I type 0 to try to guess the entire word, whatever I type, it just ends the program and closes the window
I'm just reproducing the way my teacher did in class, so it is all basic 'cause we're starting to learn
So here goes the code:
#include <stdio.h>
#include <string.h>
#define n 10
int main() {
char s[n], p, p_complete[n], asterisk[n];
int i, counter, p_loop, rights;
rights = 0;
counter = 1;
strcpy(s, "algoritmo");
for (i = 0; i < n - 1; i ) {
asterisk[i] = '*';
};
while(p != '0' && counter < n-1) {
printf("\n\nWord: %s", asterisk);
printf("\n\nType a letter: ");
scanf("\n\n%s", &p);
for (p_loop = 0; p_loop < n-1; p_loop ) {
if (s[p_loop] == p) {
asterisk[p_loop] = p;
rights ;
};
};
counter ;
};
if (p == '0') {
printf("\nType the entire word: ");
scanf("%s", &p_complete);
};
if (strcmp(p_complete, s) == 0 || rights == n - 1) {
printf("\nCongratulations, you won!");
} else {
printf("\nOh no, you lose!");
};
return 0;
}
CodePudding user response:
Sec problem: when I type 0 to try to guess the entire word, whatever I type, it just ends the program and closes the window
It's because of this line:
scanf("%s", &p_complete);
p_complete
is a char
array, which will decay to a pointer to its first element. So, passing &p_complete
is actually passing the address of the pointer to the first element. What you should be doing is
scanf("%s", p_complete);
Some other issues with your code:
- Avoid
#define
s as much as you can. Better useconst
as an alternative. strcpy()
is not not necessary in your case. Moreover, it is error-prone and you should use it with caution.- Declare variables near where you use them.
while (p != '0' ...)
: You are using an uninitialized variable (p
), and this is undefined behaviour.scanf("\n\n%s", &p);
is wrong:p
is achar
, not achar*
. So%c
format must be used.- Why are you writing
};
?;
is not necessary here, and it means an empty statement after}
.
Here's how your code should look like:
#include <stdio.h>
#include <string.h>
int main(void)
{
const size_t size = 10; // size_t: the proper type for sizes
char s[size], p, p_complete[size], asterisk[size];
int counter = 1, rights = 0;
snprintf(s, size, "%s", "algoritmo");
for (size_t i = 0; i < size-1; i ) {
asterisk[i] = '*';
}
asterisk[size-1] = '\0';
while(p != '0' && counter < size - 1) {
printf("\n\nWord: %s", asterisk);
printf("\n\nType a letter: ");
scanf(" %c", &p);
for (size_t p_loop = 0; p_loop < size - 1; p_loop ) {
if (s[p_loop] == p) {
asterisk[p_loop] = p;
rights ;
}
}
counter ;
}
if (p == '0') {
printf("\nType the entire word: ");
scanf(" %s", p_complete);
}
if (strcmp(p_complete, s) == 0 || rights == size - 1) {
printf("\nCongratulations, you won!");
} else {
printf("\nOh no, you lose!");
}
}