Thread: Trouble Printing out Array

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    13

    Trouble Printing out Array

    I was able to have the user enter in their desired letter but I have a problem with printing out the arrays. Here is what I have:
    Code:
        for(i=0; i<SIZE; i++)
        {
        StoreUserLetter[i]=GetGuess();
        }
        printf("%c", StoreUserLetter[i]);
    The variable SIZE is to keep the user from entering a certain amount of character if anyone is curious.
    and here is the user defined function:
    Code:
    char GetGuess()
    {
        char guessofuser;
        printf("Enter your guess: ");
        scanf(" %c", &guessofuser);
        return guessofuser;
    }
    What could be wrong? It does not print out anything.
    Last edited by denZer; 11-05-2011 at 11:32 PM.

  2. #2
    Registered User DevoAjit's Avatar
    Join Date
    Jun 2011
    Location
    Ludhiana, Punjab, India, India
    Posts
    32
    But how many time u r trying to run your printf.. what is the value of SIZE here. if we take SIZE=20;
    then the last character of loop will be work... isn't it. Please clear it.... wht u want 2 see?

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    13
    Just 5 times as a test, I already did #define SIZE 5.

    It should display all 5 characters but it doesn't; instead it displays a hieroglyphic like symbol.
    Last edited by denZer; 11-06-2011 at 12:05 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > printf("%c", StoreUserLetter[i]);
    You're printing the subscript of an array when the loop has exited (i >= SIZE) now.

    Use another loop.
    Code:
        for(i=0; i<SIZE; i++)
        {
        StoreUserLetter[i]=GetGuess();
        }
    
        printf("You guessed:");
        for(i=0; i<SIZE; i++)
        {
        printf("%c", StoreUserLetter[i]);
        }
        printf("\n");
    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.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    13
    Here is what I came up with:
    Code:
        while(i<SIZE)
        {
        StoreUserLetter[i]=GetGuess();
        printf("\n%c", StoreUserLetter[i]);
        i++;
        }
    The thing is, I rather have all the letters printed out after the user enters 5 characters. How can I approach to doing this?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by denZer View Post
    The thing is, I rather have all the letters printed out after the user enters 5 characters. How can I approach to doing this?
    You might try reading the answers already posted, for starters. Salem's last post has already answered this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    13
    Oh wow, sorry. It was late at night and I thought Salem was just quoting me, my fault.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble printing an array of chars
    By ppata in forum C Programming
    Replies: 8
    Last Post: 11-11-2010, 09:39 PM
  2. trouble printing linked list
    By deathrattle in forum C Programming
    Replies: 3
    Last Post: 12-02-2008, 05:33 PM
  3. Having trouble printing from an array
    By Sir Andus in forum C Programming
    Replies: 2
    Last Post: 10-30-2006, 01:48 PM
  4. trouble printing
    By dPmunky in forum C Programming
    Replies: 6
    Last Post: 11-18-2002, 02:43 PM
  5. Trouble Printing ASCII Character
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2002, 08:04 PM