Thread: Need a few pointers

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    14

    Question Need a few pointers

    Need some help with this simple hangman program. i keep getting the error :
    Lvalue required in function main()
    an d a warning to do with pointers in the get_word function;
    Could some body tell me how to fix this.

    Thanks,
    Drako.

    /*
    Author - Gearoid Donnellan
    Assignment 11
    */

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    void intro(void);
    char *get_word(void);
    char get_letter(void);
    void check_letter(char word[9], char c,int *mistakes);

    void main(void)
    {
    char word[10], guess;
    int miss = 0;

    intro();
    word = get_word();

    system("CLS");

    while (miss < 3)
    {
    guess = get_letter();
    check_letter(word, guess, &miss);
    }


    }

    void intro(void)
    {
    printf("****************************************** *************\n");
    printf("Hangman by Gearoid Donnellan\n");
    printf("****************************************** *************\n\n");

    printf("Welcome to my hangman game. Player 1 enters a word then\n");
    printf("Player 2 must enter their guesses. Three wrong guessses\n");
    printf("and you lose!\n\n");

    }

    char *get_word(void)
    {

    char word[9];
    printf("Player 1, please enter a word >");
    scanf("%s", word);

    return word;
    }

    char get_letter(void)
    {
    char c;
    printf("\n\nPlease enter a letter >");
    scanf("%c",&c);

    return c;
    }

    void check_letter(char word[9], char c, int *mistakes)
    {
    int i = 0, m = 0, n = 0;

    do
    {
    if (word[i] != c)
    {
    printf("*");
    m += 1;
    }
    else
    {
    printf("%c", c);
    n += 1;
    }
    i += 1;

    }while(i <= strlen(word));
    *mistakes = m;

    printf("You uncovered %d letters\n\n", n);
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I fixed the blatant errors, but the program still doesn't work quite as it should. That part is up to you
    Don't use void main, that makes your entire program undefined. Whenever you use scanf to read string data you'll want to clear the input buffer with a loop, or just avoid using scanf to read string data
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    
    void intro(void); 
    void get_word(char * word); 
    char get_letter(void); 
    void check_letter(char * word, char c,int * mistakes); 
    
    int main(void) 
    { 
      char word[10], guess; 
      int miss = 0; 
      intro(); 
      get_word ( word ); 
      system("CLS"); 
      while (miss < 3) { 
        guess = get_letter(); 
        check_letter(word, guess, &miss); 
      }
      return 0;
    } 
    
    void intro(void) 
    { 
      printf(" ********************************************\n"); 
      printf("Hangman by Gearoid Donnellan\n"); 
      printf(" ********************************************\n\n"); 
      printf("Welcome to my hangman game. Player 1 enters a word then\n"); 
      printf("Player 2 must enter their guesses. Three wrong guessses\n"); 
      printf("and you lose!\n\n"); 
    } 
    
    void get_word(char * word) 
    {  
      printf("Player 1, please enter a word >"); 
      fgets ( word, sizeof word, stdin ); 
    } 
    
    char get_letter(void) 
    { 
      char c; 
      printf("\n\nPlease enter a letter >");
      while ( getchar() != '\n' );
      scanf ( "%c", &c ); 
      return c; 
    } 
    
    void check_letter(char word[9], char c, int *mistakes) 
    { 
      unsigned i = 0, m = 0, n = 0; 
      do { 
        if (word[i] != c) { 
          printf("*"); 
          m += 1; 
        } 
        else { 
          printf("%c", c); 
          n += 1; 
        } 
        i += 1; 
      } while(i <= strlen(word)); 
      *mistakes = m; 
      printf("You uncovered %d letters\n\n", n); 
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM