Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Hangman Program

Options
  • 12-11-2012 9:32pm
    #1
    Registered Users Posts: 27


    hi guess i am new here and think that i may be in the wrong place but im looking for some help with some code im writing in C. this code thati have is meant to pic a random word of one of the 5 pre picked words, then the user must guess a letter and then the letter is scanned through the array and prints wether the letter is correct or incorrect (simple hangman game really). there is a problem in that the random word isnt working and the letter scanning doesnt seem to be working either. if some one could help me i would be most greatful! thanks in advance. and im using borland compiler if that makes a difference.
    This is the code here:

    /*pro

    gram to show if else statements for hang man 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)
    {

    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


Comments

  • Registered Users Posts: 7,501 ✭✭✭BrokenArrows


    What do you mean by they are not working. Whats happening vs what should be happening.


  • Registered Users Posts: 27 haze man


    well at the moment the rand code is picking a number between 1 and 5 this number dictates which of the arrays should be used, how ever the rand code doesnt seem to be working but i have been told borland isnt the best for randomness so that not a major problem. the major problem is that the arrays arnt getting assigned to the word that is too be guessed, at the moment i have hard coded the word 'trick' to be guessed and even this doesnt work. as when you run the program and guess the first letter to be 't' the program reads this to be incorrect when it should read it as being right


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    You are using the same seed every time you run it, so you get the same (pseudo) random number. Try using the current time as the seed instead of just 0.

    srand(0); <- your problem

    As for your arrays, you may have a scope problem. Declare the array first, before you do any conditional logic. Then assign your characters. Right now you are declaring array twice but in different scopes, that is all kinds of messed up. Only the outer array gets used, but nothing ever gets written into this.

    You probably want to do something like this:

    #define MAX_SIZE 10
    char array[MAX_SIZE];

    if (something == 0)
    strcpy(array, "trick");
    else if (something == 1)
    strcpy(array, "hedge");

    Note when you put text in inverted commas it will treat it as a string and null terminate it. This means "hedge" is actually 'h' 'e' 'd' 'g' 'e' '/0'. The strcpy function makes it easy to copy strings around, but it only works if the /0 is present.


  • Registered Users Posts: 27 haze man


    cool thanks for that man but do you know the way you have assigned the array at the start but when you get into the if statements you dont state the array MAX_SIZE to be apart of it, how come you have done this?


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Because I am only declaring the array once. The second time array is used is to copy stuff into it, not to actually allocate it.


  • Advertisement
  • Registered Users Posts: 27 haze man


    ohh right i see now thanks very much!


  • Registered Users Posts: 27 haze man


    srsly78 wrote: »
    You are using the same seed every time you run it, so you get the same (pseudo) random number. Try using the current time as the seed instead of just 0.

    srand(0); <- your problem

    As for your arrays, you may have a scope problem. Declare the array first, before you do any conditional logic. Then assign your characters. Right now you are declaring array twice but in different scopes, that is all kinds of messed up. Only the outer array gets used, but nothing ever gets written into this.

    You probably want to do something like this:

    #define MAX_SIZE 10
    char array[MAX_SIZE];

    if (something == 0)
    strcpy(array, "trick");
    else if (something == 1)
    strcpy(array, "hedge");

    Note when you put text in inverted commas it will treat it as a string and null terminate it. This means "hedge" is actually 'h' 'e' 'd' 'g' 'e' '/0'. The strcpy function makes it easy to copy strings around, but it only works if the /0 is present.

    when i use the strcpy i get an error of undefined function, do need to put in a certain #inckude or something??


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Yes.

    Bookmark this: http://en.wikipedia.org/wiki/C_string_handling

    Lots of handy links, like this one (reference for strcpy) -> http://en.cppreference.com/w/c/string/byte/strcpy

    That tells you: Defined in header <string.h>

    Which means you gotta put this at the top of your source code:
    #include <string.h>


  • Registered Users Posts: 673 ✭✭✭Marsden


    Delete your code when your done here, Michael wont be too happy if he see's you on boards getting help.:D And he checks.


  • Registered Users Posts: 27 haze man


    srsly78 wrote: »
    Yes.

    Bookmark this: http://en.wikipedia.org/wiki/C_string_handling

    Lots of handy links, like this one (reference for strcpy) -> http://en.cppreference.com/w/c/string/byte/strcpy

    That tells you: Defined in header <string.h>

    Which means you gotta put this at the top of your source code:
    #include <string.h>

    thanks


  • Advertisement
  • Registered Users Posts: 27 haze man


    ok so i have made some progress with my code for hangman, but at the moment when i run the application and guess the letter a symbol appears, i have no idea what this means or how to get rid of it, any help would be great! here is the code i think it is to do with while loop scanning the letter at the end.
    /*program to show if else statements for hang man code */

    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_SIZE 10
    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(time(NULL)) ;
    int num = rand() %5 +1 ;

    //printf("%d", num);

    flushall();

    /*assignment of random words to arrays */
    char array[5];

    if (num == 1)
    {
    char array[5] = "trick";
    printf("array is %d \n", num);
    //printf("%c", array[1]);
    //printf("%s", array);
    }
    else
    if(num == 2)
    {
    char array[5] = "boats";
    printf("array is %d \n", num);
    //printf("%c", array[1]);
    //printf("%s", array);
    }
    else
    if(num == 3)
    {
    char array[5] = "tools";
    printf("array is %d \n", num);
    //printf("%c", array[1]);
    //printf("%s", array);
    }

    else
    if(num == 4)
    {
    char array[5] = "guide";
    printf("array is %d \n", num);
    //printf("%c", array[1]);
    //printf("%s", array);
    }

    else
    if(num == 5)
    {
    char array[5] = "brick";
    printf("array is %d \n", num);
    //printf("%c", array[1]);
    //printf("%s", array);
    }

    //printf("array is %d \n", array);
    int lives = 6 ;


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

    char letter;
    char array1[5] = {1 || 2 || 3 || 4 || 5};
    char answer1[] = {'*','*','*','*','*'} ;
    int i;
    int wrong = 0;
    int correct = 0;

    letter = (char)getchar();

    while (lives != 0)
    {
    for (i = 0 ; i < 5 ; i++)
    {
    if (letter = array)
    {
    correct = correct ++;
    answer1 = array;
    printf("correct %d \n", correct);
    }
    else
    {
    wrong = wrong ++;

    }
    flushall();
    }
    flushall();

    if (correct > 0)
    {
    lives = lives;
    printf("lives are %d \n", lives);
    }
    else
    {
    lives = lives --;
    printf("lives are %d \n", lives);
    }

    correct = 0;
    wrong = 0;

    //print to show word being guessed
    printf("word guess is %.5s \n \n", answer1);
    letter = (char)getchar();
    }

    }


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Start by removing all the commented out lines and other redundant code, it's a mess.


  • Registered Users Posts: 673 ✭✭✭Marsden


    Haze man, you do know the due date for that was Friday.:(


  • Registered Users Posts: 27 haze man


    srsly78 wrote: »
    Start by removing all the commented out lines and other redundant code, it's a mess.

    ok ill do that, can you see anything that could be making this not work??


Advertisement