Thread: Help!!! Problems with arrays.

  1. #1
    Unregistered
    Guest

    Question Help!!! Problems with arrays.

    The following code is the game "The hangma"". The player guesses either a letter or the whole word. The program is working if the user inputs a letter, how can I do it if the user wants to input the whole word? Thanks a lot.

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

    #define max_l 20
    #define true_love 1
    #define falsa 0
    #define max_wrong_char 9



    int main(void)
    {
    char c;
    int i;
    int j;
    char *word;
    char guess[max_l];
    int len;
    int found;
    int wrong_char=0;
    int rest;



    // input word to guess

    printf("imput word to guess\n ");
    scanf("%s",word);


    // word= toupper(*word);


    // lenght of the word to guess
    len=strlen(word);
    for (i=0;i<len; i++) guess[i]='*';


    // loop

    for (wrong_char=0; wrong_char<max_wrong_char
    {

    // foun=falsa as default

    found= falsa;

    // display guess so far
    for ( j=0; j<len; j++)
    printf("%c", guess[j]) ;

    // use inputs letrra
    printf("\n input letter\n");
    scanf("\n%c",&c);

    // if la palabra es esncontrada

    for (i=0;i<len;i++)
    {
    if (word[i]==c)

    {

    guess[i]=c;
    found= true_love;
    } //end if
    }//end for

    // if la palabra no es esncontrada

    if (found==falsa)
    {
    wrong_char++;
    rest= max_wrong_char-wrong_char;
    printf(" Wrong letter - number of lifes %d\n ", rest);
    }

    if (rest==0)
    printf("you are dead");




    } // end of for para wrong
    getch();
    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    This is the worst gets I've ever seen (sorry for that). First of all, you need to allocate space for your buffer (word). Second it's better to use fgets to prevent buffer overflow:
    Code:
    char word[100];
    fgets(word, 100, stdin);

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    What you need to do is always read a string (when asking for input letter) and calculate the length of the string (to determine if it's a character or a word). Change all the scanf functions in fgets functions. Remember that the fgets function also reads the \n character in buffer.

    Code:
    char word[1024]; 
    char input[1024]; 
    ...
    // input word to guess 
    printf("imput word to guess\n "); 
    fgets(word, 100, stdin); 
    word[strlen(word)-1] = 0;  /* remove \n char */
    ...
    // input letter (or word)
    printf("\n input letter\n"); 
    fgets(input, 100, stdin); 
    input[strlen(input)-1] = 0;  /* remove \n char */
    ...
    if(strlen(input) > 1)
    {
      /* It's a word */
      if(strcmp(word, input) == 0)
        printf("YOU WIN\n");
      else
        printf("Wrong guess\n");
      }
    else
    {
      /* It's a letter. Here you can use input[0] in stead of c */
    }
    B.T.W. Your main should return a value!

  4. #4
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    take a look at this sry its not documented but it was my 3'rd program ever and i wasn't very keen on documenting at that time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with arrays
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 03-31-2009, 07:21 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. My Arrays!!!, My Arrays!!! Help!?
    By llcool_d2006 in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2006, 12:07 PM
  4. Problems with my arrays in this program
    By shadowctr in forum C++ Programming
    Replies: 7
    Last Post: 11-20-2005, 07:27 PM
  5. Problems with Strings and Arrays.
    By SlyMaelstrom in forum C++ Programming
    Replies: 13
    Last Post: 04-15-2005, 02:13 PM