Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Hangman C code

  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hangman C code

    whats wrong with my hangman code?? it isnt assigning the arrays to be the word guessed, i know at the moment i have hard coded 'trick' to be guessed but even that doesnt work. Thanks in advance hope you can help!
    here is the code:

    #include <stdio.h>
    #include <stdlib.h>
    main()
    {
    printf("Welcome To Hangman \n \n \n") ;
    printf("To play this game you have to guess the letter of the word that is picked \n \n") ;
    printf("If you guess correct you will not lose any lives \n") ;
    printf("However an incorect guess will cause you to lose one life \n") ;
    printf("Good Luck! \n \n") ;


    //random word select
    srand(0) ;
    int num = rand() %5 +1 ;

    printf("%d \n", num) ;

    flushall();

    //assignment of random words to arrays
    char array ;

    if ( num == 1 )
    {
    char array[] = {'b','r','i','c','k'} ;
    }

    else
    if ( num == 2)
    {
    char array[] = {'t','r','i','c','k'} ;
    }

    else
    if ( num == 3)
    {
    char array[] = {'b','o','a','t','s'} ;
    }

    else
    if ( num == 4 )
    {
    char array[] = {'m','e','n','u','s'} ;
    }

    else
    if ( num == 5 )
    {
    char array[] = {'g','u','i','d','e'} ;
    }

    //To scan the users imput to the word to be guessed

    int lives = 10 ;

    printf("You have %d lives \n \n", lives) ;

    char array1[] = {'t','r','i','c','k'} ;
    char letter;
    int i;

    for (i = 0 ; i < 6 ; i++)
    {
    letter = (char)getchar();
    scanf("%c", &letter);

    if (letter == array1[i])
    {

    lives = lives ;
    printf("Corrent you now have %d lives left \n \n", lives) ;
    }
    else
    {
    lives = lives --;
    printf("Incorrect you now have %d lives left \n \n", lives) ;
    }

    flushall();

    if ( lives <= 0)
    printf("You lose") ;


    }


    getchar() ;

    }// end to main

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,722
    Thanks
    41
    Thanked 191 Times in 190 Posts
    according to this link your arrays should be something like
    Code:
    char array1[] = "trick";

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •