Thread: Array Help

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Array Help

    Hi,

    Could anybody shed any light as to this little problem:

    I have declared an array of 26 characters to be used to hold lettes that have been guessed in a game of hangman. The array is then printed after each guessed letter to inform the user! My problem is is that as well as printing the characters is also prints strangee characters regulalry following the letters. The code that I am using for this little bit is:

    Code:
    char alphabet[15];
    
    alphabet[j] = letter;  /* where letter is the one chosen by the user*/
      j++;
    
    printf("\n\nThe letters that you have chosen so far ar:  %s", alphabet);
    This is just extracted from the main body of the code, but this is the only part I am having trouble with.

    Thanks

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    %s expects nul-terminated string and not just array of characters, do you put a nul terminator after the last char you want to print? And do you have a place for it?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    char alphabet[15];
    I have declared an array of 26 characters ...
    The two bits of information do not match up...

    As to the printf problem, you would need to terminate the string with a zero (so you should also make sure that your array can hold 27 characters to make space for the zero at the end).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Hahaha...sorry I had not yet changed that length of the string to 26

    How would I go about terminating the string with a zero? Is that done when the array is declared?

    Thanks for the quick responses.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Location
    India, Gujarat
    Posts
    22
    exactly what u want to do?

    ok

    first u make the ur alphabet array null using "memset()" and then assign one by one letter

    like:

    char alphabet[26];
    memset(alphabet, 0, 26);

    alphabet[j] = letter; /* where letter is the one chosen by the user*/
    j++;

    printf("\n\nThe letters that you have chosen so far ar: %s", alphabet);
    Last edited by vipul_vgp; 02-24-2009 at 04:29 AM.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by darren78 View Post
    Hahaha...sorry I had not yet changed that length of the string to 26

    How would I go about terminating the string with a zero? Is that done when the array is declared?

    Thanks for the quick responses.
    2 ways - zero initialize whole array and make sure you do not overwrite the last char
    Code:
    char alphabet[27] = {0};
    or after you finish writing last char to be shown at the index i - put 0 at the index i+1
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by vart View Post
    2 ways - zero initialize whole array and make sure you do not overwrite the last char
    Code:
    char alphabet[27] = {0};
    or after you finish writing last char to be shown at the index i - put 0 at the index i+1
    Thanks very much. You've made a stressed student very happy!!!!

    Game now done! Just need to tidy up the code and add comments!!!

    Thanks again!

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