scanf getting {space}

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AlexC
    New Member
    • Apr 2010
    • 9

    scanf getting {space}

    I have a problem with scanf, when I run program scanf just gets [space], so I'm confused.
    ***
    1 EnQ
    2 DeQ
    3 Print
    4 Exit
    enter your choice: 1
    enter the char:
    1 EnQ
    2 DeQ
    3 Print
    4 Exit
    .... Help me please.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int n = 0;
    char Q[8];
    int tmp;
    char *H = Q;
    char *T = Q;
    int count = 0;
    
    
    
    int main(){
    	int choice;
    	
    menu:
    	printf("\n1 EnQ\n 2 DeQ\n 3 Print\n 4 Exit\n");
    	printf("Enter your choice:");
    	scanf("%d", &choice);
    	switch(choice){
    		case 1:{ 
    			EnQ();
    			goto menu;
    			   }
    		case 2:{
    			DeQ();
    			goto menu;
    			   }
    		case 3:{
    			Print();
    			goto menu;
    			   }
    		case 4:
    			return 0;
    	}
    	return 0;
    }
    
    
    int EnQ(){
    	if(count == 0){
    		printf("Enter the Char:");
    		scanf("%c", &Q[n]);
    		n++;
    		count++;
    }	else if(count == 7)
    			printf("The array is full");
    else if(n == 7){
    		n = 0;
    		printf("Enter the char:");
    		scanf("%c", &Q[n]);
    		count++;
    		T = Q[n];
    		n++;
    }else{
    	printf("Enter the char:");
    	scanf("%c", &Q[n]);
    	n++;
    	count++;
    }
    	tmp = n;
    	return 0;
    }
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    You should use break; I.L.O. goto menu;as the ending statement in each case:

    Comment

    • AlexC
      New Member
      • Apr 2010
      • 9

      #3
      I don't think that this is the problem.

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        I tried to compile it, and it does not even compile, even after I inserted dummy functions for Deq and Print. Maybe it is some old executable left after previous successful builds.
        ps - apparently it scans the end-of-line as the character that is left after readingthe integer. You may just discard all non-printables and read one more char again.

        Comment

        • AlexC
          New Member
          • Apr 2010
          • 9

          #5
          did exactly as you said, now program is working, but i still wonder why it didn't work at first. Thanks anyway.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Because stdin is line buffered (normally), that means that no data is passed to your program until the user presses enter so in input a number you have to type digits and then the return key. All the digits plus the newline that is generated by the return key are entered into stdins input buffer.

            When you call scanf("%d", &choice); it reads the digits and leaves the newline because that is what you told it to do. So when you call scanf("%c", &Q[n]); the newline character '\n' is still in the input buffer and no more data is actually requested form the user.

            IMO the best way round this is to split reading data and converting data into 2 separate steps. All data is entered in printable characters scanf tries to merge the 2 functions of reading data and converting data. If instead you read input from the user and then convert the data read you can get round this problem by always reading a line at a time; a simplistic implementation of this is

            Code:
            char inbuf[100];
            int value;
            
            fgets(inbuf, sizeof inbuf, stdin);  // Read a line include '\n' from stdin
            value = atoi(inbuf);

            Comment

            Working...