Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
srand(time(NULL));
char guessWords[][16] = {
"Strickland",
"Curie",
"Becquerel",
"Lorentz",
"Bragg",
"Planck",
"Einstein",
"Bohr",
"Hertz",
"Millikan",
"Chadwick",
"Dirac",
"Pauli",
"Born",
};
int randomIndex = rand() % 15;
int numLives = 6;
int numRight = 0;
int oldRight = 0;
int wordlength = strlen(guessWords[randomIndex]);
int guessedletter[11] = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
int quit = 0;
int loopIndex = 0;
char guess[16];
char yourletter;
while (numRight < wordlength) {
for (loopIndex = 0; loopIndex < worldlength; loopIndex++) {
if (guessedletter[loopIndex] == 1) {
continue;
}
if (yourletter[loopIndex] == 1) {
printf("%c", guessWords[randomIndex][loopIndex]);
} else {
printf("_");
}
}
printf("So far, the number of letters that are right are:%d\n", numRight);
printf("Hello and welcome to this game of Hangman where you'll be guessing the "
"surnames of winners of the Nobel prize in physics!\n"
"Please enter a letter!");
fgets(guess, 16, stdin);
if (strncmp(guess, "quit", 4) == 0) {
quit = 1;
break;
}
yourletter = guess[0];
printf("yourletter:%c", guessedletter);
oldRight = numRight;
}
if (quit == 1) {
printf("You quit early!!!!!!");
} else if (numLives == 0) {
printf("\n You lose! The answer was:<%s>, you lose!\n", guessWords[randomIndex]);
} else {
printf("\n Congratulations!You won!\n");
printf("\n\n New Turn....\n Hangman Word:");
if (oldRight == numRight) {
numLives--;
printf("Unfortunately, you guess was wrong\n ");
if (numLives == 0) {
break;
}
} else {
printf("Well done, your guess was right\n");
}
return 0;
}
// There should be another brace here, or a few lines previous to it.
$ gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:6:7: warning: implicit declaration of function ‘time’ [-Wimplicit-function-declaration]
srand(time(NULL));
^
foo.c:28:25: warning: left-hand operand of comma expression has no effect [-Wunused-value]
int guessedletter[11]=(0,0,0,0,0,0,0,0,0,0,0);
^
foo.c:28:27: warning: left-hand operand of comma expression has no effect [-Wunused-value]
int guessedletter[11]=(0,0,0,0,0,0,0,0,0,0,0);
^
foo.c:28:29: warning: left-hand operand of comma expression has no effect [-Wunused-value]
int guessedletter[11]=(0,0,0,0,0,0,0,0,0,0,0);
^
foo.c:28:31: warning: left-hand operand of comma expression has no effect [-Wunused-value]
int guessedletter[11]=(0,0,0,0,0,0,0,0,0,0,0);
^
foo.c:28:33: warning: left-hand operand of comma expression has no effect [-Wunused-value]
int guessedletter[11]=(0,0,0,0,0,0,0,0,0,0,0);
^
foo.c:28:35: warning: left-hand operand of comma expression has no effect [-Wunused-value]
int guessedletter[11]=(0,0,0,0,0,0,0,0,0,0,0);
^
foo.c:28:37: warning: left-hand operand of comma expression has no effect [-Wunused-value]
int guessedletter[11]=(0,0,0,0,0,0,0,0,0,0,0);
^
foo.c:28:39: warning: left-hand operand of comma expression has no effect [-Wunused-value]
int guessedletter[11]=(0,0,0,0,0,0,0,0,0,0,0);
^
foo.c:28:41: warning: left-hand operand of comma expression has no effect [-Wunused-value]
int guessedletter[11]=(0,0,0,0,0,0,0,0,0,0,0);
^
foo.c:28:43: warning: left-hand operand of comma expression has no effect [-Wunused-value]
int guessedletter[11]=(0,0,0,0,0,0,0,0,0,0,0);
^
foo.c:28:23: error: invalid initializer
int guessedletter[11]=(0,0,0,0,0,0,0,0,0,0,0);
^
foo.c:34:30: error: ‘worldlength’ undeclared (first use in this function)
for (loopIndex=0; loopIndex<worldlength;loopIndex++){
^
foo.c:34:30: note: each undeclared identifier is reported only once for each function it appears in
foo.c:38:16: error: subscripted value is neither array nor pointer nor vector
if (yourletter[loopIndex]==1){
^
foo.c:52:11: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("yourletter:%c",guessedletter);
^
foo.c:66:9: error: break statement not within loop or switch
break;
^
foo.c:72:1: error: expected declaration or statement at end of input
}
^
foo.c:32:6: warning: variable ‘yourletter’ set but not used [-Wunused-but-set-variable]
char yourletter;
^
Bored with so many errors?