Thread: Program doesn't print all letters entered by user

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

    Program doesn't print all letters entered by user

    Program doesn't print all letters entered by user

    Code:
     #include<stdio.h>int main (void)
    {
    	int i;
    	char array[15];
    	int size;
    	
    	printf("promote user to enter size off array  :" );
    	scanf("%d",&size);
    	
    	for(i=0; i<size; i++)
    	{
    		printf("print array element : ", array[i]);
    		scanf("%c",&array[i]);
    	}
        
        return 0;
    
    
    }
    promote user to enter size off array :5
    print array element : print array element : a
    print array element : print array element : b
    print array element :

    Why does it only print two letters only ?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Perhaps you should print the variable after you have the user enter the value?

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Nothing change happen it gives a same result

    Code:
    #include<stdio.h>
    
    int main (void)
    {
        int i;
        char array[15];
        int size;
         
        printf("promote user to enter size off array  :" );
        scanf("%d",&size);
         
        for(i=0; i<size; i++)
        {
            scanf("%c",&array[i]);
    		printf("print array element : ", array[i]);
            
        }
         
        return 0;
     
     
    }
    promote user to enter size off array :5
    print array element : a
    print array element : print array element : b
    print array element : print array element :

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    The problem is that 1. scanf() leaves the end of line character in the input buffer and 2. when using the "%c" format character leading whitespace is not skipped.

    To fix the problem you can either enter all the characters without pressing the enter key, "abcde", or force the "%c" format specifier to skip leading whitespace by using a space before the specifier " %c".

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > scanf("%c",&array[i]);
    If you want to read the next printable character (ignoring all white space), then add a leading space to your scanf format string.
    scanf(" %c",&array[i]);
    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.

  6. #6
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    > scanf("%c",&array[i]);
    If you want to read the next printable character (ignoring all white space), then add a leading space to your scanf format string.
    scanf(" %c",&array[i]);
    Thanks salem I have another question where i am struggling




    I want to store multiple strings entered by user

    Code:
    #include<stdio.h>
    
    int main(void)
    
    
    {
    	unsigned int i;
    	
            char name[5][5]= {"bil","del","sen", "jan","gan"};
    	
    	for(i=0; i <5;i++)
    	{
    		printf("candidate name = %s \n",name[i]);
    	}
    	
    	
    	return 0;
    	
    }
    candidate name = bil
    candidate name = del
    candidate name = sen
    candidate name = jan
    candidate name = gan

    How to store multiple strings entered by user?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Pretty much the same way

    char names[5][10];

    Then a loop of
    scanf( "%s",names[i] );
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-05-2016, 06:37 PM
  2. program doesn't print data from socket
    By johnmerlino in forum C Programming
    Replies: 2
    Last Post: 05-07-2014, 01:35 PM
  3. C Program to print letters in given notation
    By vishaleee in forum C Programming
    Replies: 4
    Last Post: 02-12-2014, 12:19 AM
  4. Program doesn't wait for user input!
    By gaurav_13191 in forum C Programming
    Replies: 8
    Last Post: 07-13-2011, 08:25 AM
  5. Replies: 6
    Last Post: 07-10-2002, 07:45 PM

Tags for this Thread