Thread: array help

  1. #1
    bignood
    Join Date
    Sep 2005
    Posts
    33

    array help

    I am trying to build a HANGMAN program. I want to first ask the user to enter a word, and then I want that word to be the word that you have to guess. My problem is I don't understand how to make an array the same size as the number of letters of whatever the word is.

    Any help is appreciated!

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    char *word;

    /* Get input from user and store it in a "large enough" buffer called input */

    word = malloc(strlen(input) + 1);
    strcpy(word, input);

    /* When done with the word... */
    free(word);
    If you understand what you're doing, you're not learning anything.

  3. #3
    bignood
    Join Date
    Sep 2005
    Posts
    33
    I forgot to mention, I'm not allowed to use strings for the assignment.

  4. #4
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Are arrays of characters allowed, but strings not allowed?

  5. #5
    bignood
    Join Date
    Sep 2005
    Posts
    33
    Here is a lame attempt so far of what I'm trying to do

    Code:
    #include <stdio.h>
    #define SIZE 20
    #define GUESS '*'
    
    int main(void)
    {
    
    char word[SIZE];
    int i;
    char ch;
    int status;
    
    printf("Enter a word > ");
    scanf("%d", &word[i]);
    status = i;
    printf(" %d", status);
    while(status == 1) {
    word[i] = status;
    ++i;
    
    status = scanf(" %c", &ch);
    }
    
    return(0);
    
    }
    Last edited by 1rwhites; 11-08-2005 at 05:01 PM.

  6. #6
    bignood
    Join Date
    Sep 2005
    Posts
    33
    I don't understand how to figure out how many letters are in the word the user enters.
    Last edited by 1rwhites; 11-08-2005 at 05:02 PM.

  7. #7
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    I see a number of problems here.
    Code:
    scanf(" %c" &data);
    won't work as you'd expect -- even assuming that you put the comma between the format string and the target ('scanf(" %c", &data)), you're telling it to skip a space in the input then give you one character, not a word.

    Your function get_data() makes no real sense to me at all.

    I would suggest sitting down and writing out your algorithm (a flow-chart of the program is a good idea in any case) and then work out what the code should look like. Once you know how you want the program to work, you can (if you need to) ask specific questions about how do do specific things. For now, I don't think you're on the right track.

  8. #8
    bignood
    Join Date
    Sep 2005
    Posts
    33

    how many letters using an array

    In my program, I want the user to enter a word. After that, I need for my program to tell me how many letters the user entered. I know I need some sort of an array, and I'm not allowed to use strings.

    Does anybody know how to do this?

    Thanks in advance.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Something like this?
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       int ch, count = 0;
       fputs("Enter a word: ", stdout);
       fflush(stdout);
       while ( (ch = getchar()) != '\n' && ch != EOF )
       {
          ++count;
       }
       printf("You entered a %d-letter word (which I just discarded).\n", count);
       return 0;
    }
    
    /* my output
    Enter a word: hello
    You entered a 5-letter word (which I just discarded).
    */
    [edit]How exactly do you intend to have an array of characters that is not a string, unless you simply mean to use a non-null-terminated char array (which is just a pain in the ass)?
    Last edited by Dave_Sinkula; 11-08-2005 at 05:36 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Easy way using command line arguments.

    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        if(argc != 2){
            printf("Use: <%s> <word>\n", argv[0]);
            return -1;
        }
        
        printf("%s is %d characters long\n", argv[1], strlen(argv[1]));  
        return 0;  
    }
    I like to play pocket pool.

  11. #11
    bignood
    Join Date
    Sep 2005
    Posts
    33
    Is there anyways I can do that without using "stdout"?

  12. #12
    bignood
    Join Date
    Sep 2005
    Posts
    33

    hangman help

    Here is what I have so far...

    Code:
    #include <stdio.h>
    
    #define SIZE 20
    char word[SIZE];
    char guess[SIZE];
    #define STAR '*'
    
    int main (void)
    {
      printf("Enter a word > ");
    
      int i = 0;
      do {
        word[i]=getchar();
        i++;
      } while (word[i-1] != '\n');
    
    int num_stars = i - 1;
    printf("%d", num_stars);
    
                     
      char search;
      
      printf("\nGuess a letter >  ");
      scanf("%c", &search);
      
      for( i = 0; i < SIZE; i++ )
      {
        if( word[i] == search ) break;
      }    
      
      if( i == SIZE ) printf("%c was not found!", search);
      else
      
     printf("%c = %c and was located at position %d\n\n", search, word[i], i);
      
       
        
      return 0;
    }
    I want to have an array "guess" that will take the number of letters of the word the user enters and make them *'s. So for example the user enters word. I want the guess array to look like ****

    Can anybody help me with this?

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop making new threads. Damn. You do know you can reply in the same thread don't you? You have three of them now.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Please keep all questions regarding the same issue to the same thread

  15. #15
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Look out! This is a string:
    "%d"
    And so is this:
    "\nGuess a letter > "
    And this!
    "%c"
    And don't forget these:
    "%c was not found!"
    "%c = %c and was located at position %d\n\n"
    "can't use strings" is a ridiculous limitation, but it's possible and your program is still using them.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM