Thread: Errors in my Hangman Program

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    7

    Errors in my Hangman Program

    Hello, I received 5 errors in my Hangman programming project and I'm not too sure how to fix them. I was wondering if anyone could help?
    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;
    }
    Attached Files Attached Files
    Last edited by Salem; 04-23-2020 at 05:14 AM. Reason: Inlined the code

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,712
    > int guessedletter[11]=(0,0,0,0,0,0,0,0,0,0,0);
    Use curly braces here.
    Code:
    foo.c:28:23: error: invalid initializer
     int guessedletter[11]=(0,0,0,0,0,0,0,0,0,0,0);
    > for (loopIndex=0; loopIndex<worldlength;loopIndex++)
    Learning how to spell 'word' as not 'world'.
    Code:
    foo.c:34:30: error: ‘worldlength’ undeclared (first use in this function)
      for (loopIndex=0; loopIndex<worldlength;loopIndex++)
    > if (yourletter[loopIndex] == 1)
    yourletter is a single char, it's not an array.
    Code:
    foo.c:38:16: error: subscripted value is neither array nor pointer nor vector
      if (yourletter[loopIndex]==1){
    > 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

    ^
    Yeah, you're missing something.
    Mostly, this is bad indentation causing you to type the wrong code in the wrong place.




    Full code, and full error messages.
    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?
    A development process
    Then don't try to write whole programs in one edit session.
    A little and often goes a long way to keeping on top of things.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hangman program help
    By Justin Curtis in forum C Programming
    Replies: 1
    Last Post: 11-16-2013, 10:10 AM
  2. Help with Hangman program
    By Trank07 in forum C Programming
    Replies: 1
    Last Post: 11-24-2007, 10:00 PM
  3. hangman game, copied staight from book.. 31 errors?!?!?!?!
    By Blizzarddog in forum C++ Programming
    Replies: 10
    Last Post: 10-25-2002, 12:20 AM
  4. Hangman program Help!
    By venomgts550 in forum C++ Programming
    Replies: 5
    Last Post: 04-18-2002, 06:08 PM
  5. Hangman program
    By venomgts550 in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2002, 06:00 PM

Tags for this Thread