I'm officially stuck

This is a discussion on I'm officially stuck within the C Programming forums, part of the General Programming Boards category; Why is this code only assigning the ascii value to the arrray. I want it to print out the letters ...

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    I'm officially stuck

    Why is this code only assigning the ascii value to the arrray. I want it to print out the letters ex:
    array element 0= a
    array element 1 = b
    what Am i doing wrong.?

    Here are the two scripts i coded. I thought the second would work, but it didn't. I was so excited to wake up this morning cause i thought i had solved it but .....
    Code:
     code 1
    
    /* alpaarray.c intializes a 26 element array to the ltrs of the alphabet and
    * displays it */
    
    #include<stdio.h>
    #define SIZE 26
    
    int main(void)
    
    {
    
        int ndx;
        char alphabet[SIZE];
        char ltr;
    
        /* outer loop to count to 26 */
    
        for (ndx = 0; ndx < SIZE; ndx++)
        {
            for (ltr = 'a'; ltr <= ('a' + ndx); ltr ++)
                alphabet[ndx] = ltr;/* assigning only ascii value */
            printf("Alphabet element number %3d is %3d\n", ndx, alphabet[ndx]);
    
        }
    
    
    
    
         getchar();
         return (0);
    
    }
    Code:
    /* ltrarray2.c intialises a character array to ltr of the alphabet */
    
    #include <stdio.h>
    #define SIZE 26
    
    int main(void)
    
    {
    
        int row;
        char alpha[SIZE], ltr;
    
        /* set up rows */
    
        for (row = 0, ltr = ('a' + row); row < SIZE; row++,ltr++)
            alpha[row] = ltr;
    
        /* print elements of row */
        for (row = 0; row < SIZE; row++)
            printf("Element %3d  of Alpha array is %3d\n", row, alpha[row]);
    
    
    
        getchar();
        return (0);
    
    
    }

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    14
    You have to use the conversion specifier %c to print the character the integer stored in memory represents. for example:

    Code:
    char variable = 'a';
    
    printf("%d = %c", variable, variable);
    would print the output:

    97 = a

    edit
    Just some more thought on this.... your PC's memory can only hold numerical data. So if you're storing a string in the memory, you're really just storing the ASCII integers assigned to whatever letters you are using.
    Last edited by LittleJohn; 01-04-2004 at 03:53 PM.

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Just some more thought on this.... your PC's memory can only hold numerical data. So if you're storing a string in the memory, you're really just storing the ASCII integers assigned to whatever letters you are using.
    Thank you , I understand that a char is really still an integer, which is why my code outputted the ascii value. However why the hell didn't it it print out the character value if i had already assigned the chara - never mind. I get it now. Seems I still have to implicityl declare it as a character.

    *smacks forhead* god its so simple and so fu***ing annoying. I assumed that once i had assigned the characte value to it.
    Code:
     for (ltr = 'a'; ltr <= ('a' + ndx); ltr ++)
                alphabet[ndx] = ltr;
    Then it was saved as a character. But I forgot that even when printing it out, I can choose how it will be outputted. Thank you, very much.

    The friggin annoyances of C i keep forgetting them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 08:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 05:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 08:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 09:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21