Thread: Code doesn't print array value

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    81

    Code doesn't print array value

    Program should be get 6 letter from user and then letter print them
    this i have written
    Code:
    #include <stdio.h>
    
     int main()
    {
        char letter[5];
        
    	int i;
        
    	printf("Enter the Name of letter \n");
    	
    	
    	
    	for (i = 0; i < 5; i++)
    		
    		{
    			scanf("%c \n",&letter);
    			
    	       
    		}
    		
    		
    	for (i = 0; i < 5; i++)
    		
    		{
    			printf("%c \n", letter[i]);
    			
    	       
    		}
    
    
    
    
    	
        return 0;
    }
    program output

    Enter the Name of letter
    a
    b
    c
    d
    e
    f
    e



    %

    why code doesn't print the entered letter ?

  2. #2
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    > why code doesn't print the entered letter ?

    Because you aren't taking the input correctly. I don't know much about C and how to use printf but I'm pretty sure what you're doing is wrong.

    I think this should work:
    Code:
    int main (void)
    {
        char Letters[6]; // should be 6 and not 5 because your question says you wanna read 6 letters from user i.e. from index 0 through 5
    
        int i;
    
        for (i = 0; i < 6; i++)
        {
             scanf("%c \n" , &letter[i]); // instead if &letter[i] you could also use (letter + i)
    Code:
    
        }
    
        for (i = 0; i < 6; i++)
        {
            printf("%c \n" , letter[i]);
        }
    
        return 0;
    }
    
    Last edited by Zeus_; 10-15-2019 at 11:07 AM.

  3. #3
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66
    Hi,

    you're right. I think it would be good to clear with a little function
    the keyboard-buffer to prevent that the next call of 'scanf'
    takes the signs who are in the keyboard-buffer after the last
    call of 'scanf':

    Code:
    #include <stdio.h>
    
    void clearKeyboardBuffer(void)
     {
     while (getc(stdin) != '\n')
        ;
     }
    
    int main(int argc, char **argv)
    {
     char letter[5];
         
        int i;
         
        printf("Enter the Name of letter \n");
         
         
        for (i = 0; i < 5; i++)
         {
          printf("Enter letter %d: ", i);           
          scanf("%c", &letter[i]);
          clearKeyboardBuffer();      
         }
    
        
        for (i = 0; i < 5; i++)
         {
          printf("Nr:%d %c\n", i, letter[i]);
         }
        
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-07-2019, 05:11 PM
  2. Why array doesn't print correct numbers?
    By Arthuoff in forum C Programming
    Replies: 1
    Last Post: 09-18-2018, 11:03 AM
  3. char array doesn't print the char in the array
    By KiRa11Love in forum C Programming
    Replies: 4
    Last Post: 09-07-2012, 05:52 AM
  4. this code doesn't print any thing with gcc ??
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 10-17-2005, 11:06 AM
  5. Doesn't print out?
    By cockroach007 in forum C Programming
    Replies: 8
    Last Post: 11-17-2001, 08:30 AM

Tags for this Thread