I have been working on a hangman game for my class which is due today and just now it decided to no longer provide any output from my code. If someone could please give it a look so I can go back to possibly submitting this assignment, I would be very appreciative. I dont know what changed specifically.
char **readWordList(char *, int *);
int main(int argc, char *argv[]) {
char **wordList;
char inputFile[100];
int count = 0;
int i;
if (argc != 2) {
printf("You need to provide the word list file name.\n ");
printf("Usage: $0 filename\n");
return -1;
}
wordList = readWordList(argv[1], &count); //function (target input[s], y placeholder var)
if (wordList == NULL) {
printf("Read word failed\n");
exit(1);
}
printf("fortnite");
int a = 0; //placeholder variables
int b = 0;
int c = 0;
int v = 0;
int g = 0;
int hit = 0;
srand(time(NULL)); //random variable for word selection
int r = rand() % 3;
int chances = 10;
char *word = wordList[r]; //address word from line.txt
char guess;
char misses[10];
int lettercount = 0;
//make blank variable by reading random accessed word
for (size_t a = 0; word[a] != 0; a ) {
lettercount ;
}
char space[lettercount];
// write underscores in place of spaces in temporary array
for (size_t c = 0; word[c] != '\0'; c ) {
space[c] = 95;
}
char blank[lettercount * 2]; //equal to array size empty array
// borrowing my spacename function, but seems to make artifacts in blank input now
int t = 0;
int k = 0;
while (space[t] != '\0') {
k = 2 * t;
if (k > lettercount * 2 - 2) {
blank[k] = space[t];
break;
}
blank[k] = space[t];
blank[k 1] = ' ';
}
while (chances > 0) {
printf("Chances:%d\n", chances);
printf("Misses:%d\n", misses);
printf("Word:%s\n", blank);
printf("Guess[Q]:");
scanf("%c\n", &guess);
while (word[b] != '\0') {
if (guess = 'Q') {
exit(0);
}
if (guess = word[b]) {
v = b * 2;
blank[v] = guess;
b ;
hit = 1;
}
b ;
}
if (hit != 1) {
misses[g] = guess;
}
if (hit = 1) {
hit = 0;
}
chances--;
g ;
}
}
CodePudding user response:
There are multiple problems in the code, including some serious ones:
the
#include
lines are missing.the
readWordList
function is missing.if (guess = 'Q')
setsguess
to'Q'
and evaluates to true. You should writeif (guess == 'Q')
if (guess = word[b])
... same problem.printf("Misses:%d\n", misses);
should beprintf("Misses:%s\n", misses);
andmisses
should be initialized as the empty string.printf("Word:%s\n", blank);
has undefined behavior asblank
is not null terminated.while (space[t] != '\0')
may iterate too far asspace
does not have a null terminator since it's length islettercount
and all elements have been set to95
('_' in ASCII). Yet since you never incrementt
, you actually have an infinite loop. Use a simplefor
loop instead:for (t = 0; i < lettercount; t )
scanf("%c\n", &guess);
reads a character and consumes any subsequent white space, so the user will have to type another non space character and a newline forscanf()
to return. You should instead usescanf(" %c", &guess);
while (word[b] != '\0')
will iterate beyond the end of the array after the first guess because you do not resetb
to0
before this loop. Furthermore,b
is incremented twice in case of a hit. You should usefor
loops to avoid such silly mistakes.if (hit = 1) { hit = 0; }
... the test is incorrect (it should use==
) and you could just writehit = 0;
or better sethit
to0
before the inner loop.inputFile
is unused.
You should compile with gcc -Wall -Wextra -Werror
to avoid such silly bugs that can waste precious time.
CodePudding user response:
i fixed it, i lost a variable which provided the chance that a while loop would end for the spacing array function
sorry for being annoying