Hangman Problem

This is a discussion on Hangman Problem within the C Programming forums, part of the General Programming Boards category; Good day everyone! I would like to ask some problem about the program I am currently working (hangman to be ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    9

    Hangman Problem

    Good day everyone!

    I would like to ask some problem about the program I am currently working (hangman to be specific). Since I am just a beginner, there are many things I would like to clarify. I would like to ask your help so that I can enable more to see what are my errors in my code. It is too many to ask. Please bear with me and sorry for the inconvenience.

    1. I do have 4 categories (three rounds). The user will choose a category. For every round, a different category. You cannot choose the same category in different rounds. After choosing a category, the game will provide a random word. At this moment, i have done only the random part (and it might some errors too.)

    2. I am also having difficulty in underscores and spaces. You see, I only guessed that part. I do not know if that will work (or maybe it will destroy my whole program.)

    3. Will you give me some advices on how to check inputs and how to display the letters which are used already for a specific round? I am not merely asking for code (though it will be gladly accepted it) but some detailed explanations.

    4.
    Code:
     int inputerror(char b, char* h)
    {
        int i;
        char alpha[27];
        strcpy(alpha, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    
        // change letters used from the alphabet to '_'
        for (i = 0; alpha[i] != '\0'; i++)
        {
            if ((alpha[i]) == h)
            {
                alpha[i] = '_';
            }
        }
    
        // pad with spaces so it appears in the middle of the screen
        printf("              ");
    
        for (i = 0; alpha[i] != '\0'; i++)
            printf("%c ", alpha[i]);
        printf("\n\n");
    
    }
    I am having difficulty with this code.
    a. under what library(i.e stdio, stdlib) is strlwr, tolower and _tolower? do you think it requires in my code?
    b. Is this code ok?

    Here are just some of my questions. Please see the attached file below for the whole code. If you see other errors, please tell me. I really need your advices regarding with this case. Hope that you can help me as soon as possible.

    Thanks in advance!
    Attached Files Attached Files
    • File Type: c 33.c (10.0 KB, 50 views)

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    under what library(i.e stdio, stdlib) is strlwr, tolower and _tolower
    strlwr : #include<string.h>
    tolower : #include<ctype.h>
    This line is incorrect:
    Code:
    if ((alpha[i]) == h)
    Here alpha[i] is a char whereas h is a pointer which is nothing but an address. So how are comparing them?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    9
    1. can you give an example on how to use strlwr? Because when I used strlwr, I always receive an error.

    2. The code in #4 is used to display alphabets. When the user inputs a letter, the letter will be marked as an underscore. (whether the letter is in the puzzle or not)

    sample output:
    >> A B C D E F G H I J K L ...
    >>user inputs 'A'
    >>_ B C D E F G H I J K L ...
    >>user inputs E
    >>_ B C D _ F G H I J K L ...

    --)something like that..

    Maybe like this?
    Code:
    int inputerror(char b)
    {
        int i;
        char alpha[27];
        strcpy(alpha, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    
        for (i = 0; alpha[i] != '\0'; i++)
        {
            if ((alpha[i]) == b)
            {
                alpha[i] = '_';
            }
        }
    
        printf("              ");
    
        for (i = 0; alpha[i] != '\0'; i++)
            printf("%c ", alpha[i]);
        printf("\n\n");
    
    }

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Edit:

    I didn't notice you'd changed the variable from h to b.
    Last edited by Adak; 08-31-2009 at 04:36 AM.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    9
    >>Adak
    Actually, I removed the "char* h"
    If you want you can see my code for further checking (it is part of my attachment). I know it is kind of lousy, but i would like to hear everyone's opinion regarding to my code.

    Thanks!
    Attached Files Attached Files
    • File Type: c mp.c (14.3 KB, 28 views)

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One problem is that few people want to d/l and wade through a large program, for the sake of giving our opinions.

    With variable names like h or b, I would find it tough to be fair with the rest of the code. Variable names should *always* give you some clue as to what their purpose is, in the function or program:

    Think of guessLetr, gLetr, gletter, guessChar, gChar, tryLetr, tryChar - *so* much more intuitive to use and understand, at a glance. C language itself, has so many built in functions that use that technique: strlen= string length, atoi=change alpha letters to an integer, etc.

    The other problem is that you're not being specific enough. I'm not even sure if your program *has* a problem at this time, or not.

    If it does, then specify what the problem is: a compiler error, (and which compiler error, and where is it saying the error occurred?), or is it a linker error (and which linker error is it?), or is it an error in the logic, and if so, in which function, and exactly *what* is the problem?

    All of this, you're just skipping over, and these kinds of details are *exactly* what we'd need to efficiently troubleshoot a problem with your program.

    The questions to ask yourself, are:

    1) Is the program accurate?
    2) Is the program efficient to run and maintain?
    3) Is the user interface (if it has one), a good one?

    You can elaborate a lot on #2, but the central idea is how's the run time, and how's the clarity of the code?

    #3 would include design, colors, layout, and many other factors. There's a lot of details that go into a good user interface.

    Show us the problem, with the smallest example you can, and tell us what you've tried to do to fix it. (We'd just probably waste our time repeating some of that, otherwise). And do include all the details.

    We get a lot of these "hey, i need help with this program", and I want to post back "hey, how about fixing my car?". Of course, I'm not going to tell you stink about my car...

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    9
    Frankly speaking, i find your answer as a sarcastic one (at a first glance)
    Though, it gave me a (indirect) help.
    I will finish correcting my errors as early as possible then if i find something weird, i am going to post the main part which i am confused with. Is that better enough?

    ok, question.
    Code:
        int n, z;
        n=rand();
        srand((unsigned int)time(0));
        n=rand()%z;
        printf("Please choose a category. Press:\n");
        printf("1 --> Food Trip!\n");
        printf("2 --> Phrase/Quotes anyone?\n");
        printf("Category No:");
        scanf("%d", &categoryno);
     if (categoryno == 1)
            {
                strcpy(Salita[1].Word, "Mcdonald's\0");
                strcpy(Salita[0].Word, "Food Stalls\0");
                strcpy(Salita[2].Word, "Banana\0");
            }
            else if (categoryno == 2)
            {
                strcpy(Salita[1].Word, "You never know what you have till you lose it. You never get it back./n -snatcher/0");
                strcpy(Salita[2].Word, "Beware, so long as you live, of judging men by their outward appearance.\n -Jean de La Fontaine\0");
                strcpy(Salita[0].Word, "Curiosity. The reason why most of us haven't commited suiucide long ago.\n -Anonymous\0");
            }
    I checked my code if this is working (via printf("%s")). and I noticed that it prints twice. Why is that so?
    Last edited by chixm8_49; 08-31-2009 at 06:25 AM.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to make "Salita" and "catagoryno" into variables in your program.

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    9
    I actually declared it. I tried to rewrite the whole code but this time, it has a different effect. My program does not working anymore!
    Code:
    //function declaration
    void Category (void);
    void A (void); //first category
    void B (void); //2nd category
    void C (void);//3rd category
    void R1(void);//round1
    
    //under 
    void Category (void)
    {
       printf("Please choose a category:");
       printf("1-->A");
       printf("2-->B");
       printf("3-->C");
       scanf("%d", &categoryno);
    
    if(categoryno==1)
    {
      A();
    }
    }
    
    //under 
    void A(void)
    {
       srand((unsigned int)time(0));
        n=rand()%z;
    
        strcpy(Salita[0], "...\0");
        strcpy(Salita[1], ",,,\0");
        strcpy(Salita[2], "///\0");
    
        switch(n)
        {
            case 0: Salita[0][100]; break;
            case 1: Salita[1][100]; break;
            case 2: Salita[2][100]; break;
        }
        return Salita[n];
    }
    
    void R1(void) /*the game will always start here.
    {
    
    }
    >>The round1 will be the start of the game. The word to be played in round1 (and other rounds as well) will be played based on the category chosen by the user. (i.e. if the user chose A, a phrase will be chosen under the category A). But the problem is, whenever I run the program, it looks like the random part is either ignored or something abnormal in it. My guess is that it is either because of my function declaration or my logic behind in one of my functions. Help, anyone?

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    9
    I am having an error as I am editing my code

    "warning: passing arg 1 of 'Round1' from the incompatible pointer type"

    what does that mean? And how to solve such warning?

    Code:
    #include<stdio.h>
    
    void Round1(char *Salita);
    
    char Salita[3][100];
    
    int main (void)
    {
       //the user will ask the user the categories to be used for nth round
      //from the if statements, the code will produce Salita[n]
       //function call
       Round1(&Salita[n]; //is this correct? 
       ...
    }
    
    void Round1(char *Salita)
    {
       ....
    }
    Last edited by chixm8_49; 09-04-2009 at 10:57 PM.

  12. #12
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Salita[n] is itself an address and you are passing the address of an address, so either you should take the argument as char **Salita or should change the call to Round1(Salita[n]). Take Salita[n] as (*(Salita+n)+0)==&Salita[n][0] i.e the base address of the nth row.
    PS: I'm assuming Round1(&Salita[n] as a typo.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  13. #13
    Registered User
    Join Date
    Aug 2009
    Posts
    9
    >>yup, it is a typo. My apologies.

    I have already done the changes by it seems there is still an error.
    I am receiving an error "cannot convert 'char*' to 'char**' for argument '1' to 'void Round1(char**)';

    Code:
     if(countmain==0)
                    {
                        Round1(Salita[n]);
                    }
    Code:
    void Round1(char **Salita)
    {
        int count1;
        length=strlen(*Salita);
        for(i=0;i<length;i++)
            display[i]='-';
        display[i]='\0';
        ...
    }

  14. #14
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Now you're calling in the wrong way. You should call as Round1(&Salita[n]). Read my previous post for more clarification.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  15. #15
    Registered User
    Join Date
    Aug 2009
    Posts
    9
    err...i have already done that. But i am having errors like:
    "passing arg1 of 'Round1' from the incompatible pointer type"

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman game problem
    By piradie in forum C Programming
    Replies: 9
    Last Post: 12-30-2008, 03:29 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21