Thread: arrays and functions help

  1. #1
    Unregistered
    Guest

    arrays and functions help

    i am trying to make a real simple game of hangman with just one word mouse. The code i have done so far is accepting characters but is not recognising any of them to be in the array why is this?
    also i need to add another array guess_word[5] = {'-','-','-','-','-'} which will be where the letters are assigned to as they are cotrerectly selected by the user...but how do i code it so the letters are passed to there correct posistion?
    (without using strings) thank you

    #include <stdio.h>

    #define letters 5

    int search(const char list[], int letternum, char letter);

    int main(void)
    {
    char word[letters] = {'m','o', 'u', 's', 'e'};
    char letter;
    int location;
    int tries = 0;

    do
    {
    printf("choose a letter: ");
    scanf("%1s", &letter);

    location = search(word, letters, letter);

    if (location) printf("%c was found\n", letter);
    else printf("%c was not found\n", letter);

    ++tries;

    }while(tries < 6);

    return 0;
    }

    int search(const char list[], int letternum, char letter)
    {
    int i = 0, found = 0;

    while (i < letternum && !found)
    {
    if (list[i] == letter) found = 1;

    else i++;
    }

    return found;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I thought hangman was played in a way where if you guessed a letter that's in the word then all occurences of that letter in the word are printed:
    Code:
    /* pseudocode, may not work 
    ** and definitely won't be pretty
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define CHANCES 10
    
    int main ( void )
    {
      size_t i,
             tries = 0;
      char *word = "mouse",
           mask[] = "-----", 
           ch;
      puts ( mask );
      printf ( "Hangman\nChoose a letter: " );
      while ( ( ch = getchar() ) != EOF ) {
        for ( i = 0; i < strlen ( word ); i++ ) {
          if ( word[i] == ch )
            mask[i] = word[i];
        }
        tries++;
        if ( strcmp ( mask, word ) == 0 ) {
          printf ( "You win!\n" );
          return EXIT_SUCCESS;
        }
        else if ( tries >= CHANCES ) {
          printf ( "You lose\nPlease deposit 25 cents and try again\n" );
          return EXIT_SUCCESS;
        }
        while ( getchar() != '\n' );
        puts ( mask );
        printf ( "Please choose another letter: " );
      }
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    umm only one problem with that..like i said in the original post i dont want to use strings!! just arrays and functions!

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    umm only one problem with that..like i said in the original post i dont want to use strings!! just arrays and functions!
    A string is an array, an array of characters. All that makes it a string is the '\0' character at the end.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i dont want to use strings!! just arrays and functions!
    A string is just an array of char with a nul at the end, what's wrong with that?

    -Prelude
    My best code is written with the delete key.

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Yes, I also fail to see why you wouldn't want to use these generous aspects of the beautiful C language...
    1978 Silver Anniversary Corvette

  7. #7
    Unregistered
    Guest
    the reason i cant use strings is because the task im attempting sopecifys no strings are to be used just functions and arrays.
    And im still clueless on how to assign the correct letters to thwe correct places in the arrays, as i said before it is one single word mouse, and im gonna assume there will be no multiple letters just to make it easier.
    And hints on how to pass the letters to the array would be very helpful, i already learnt alot from this task and the help i receieved on this board so thanks!!

  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    If you musn't use strings, then just put each character in the array and then have a counter variable for an index. Which, it still semi makes it a string just without a null char at the end.
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays, functions, HELP
    By beginner1 in forum C Programming
    Replies: 4
    Last Post: 05-20-2009, 03:29 PM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  4. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  5. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 06:46 PM