Thread: Can't find the error

  1. #1
    Registered User Sid_TheBeginner's Avatar
    Join Date
    Jun 2012
    Location
    India
    Posts
    19

    Post Can't find the error

    Hello guys. Here's my code. I want to average the numbers entered by the user.


    Code:
    #include<stdio.h>
    #include<ctype.h>
    
    
    int main(void)
    {
    	int value = 0;
    	char answer = 'n';
    	int total = 0;
    	int count = 0;
    
    
    	for( ;; ) {
    
    
    		printf("Enter a value: ");
    		scanf("%d", &value);
    		total += value;
    		count++;
    
    
    		printf("Continue?(y/n): ");    
    		scanf("%c", &answer);   // this code is getting skipped
    		
    		if(tolower(answer) == 'n') 	
    			break;
    	}
    
    
    	printf("Avg. is %d", total/count);
    
    
    	return 0;
    }
    Thanks in advance.
    --Sid

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Change the line to

    Code:
    scanf(" %c", &answer);   // this code is getting skipped

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Print the value (and character) of the answer to the second question:
    Code:
            printf("Continue?(y/n): ");   
            scanf("%c", &answer);
            printf("you answered with the character with value %d ('%c')\n", answer, answer);

  4. #4
    Registered User Sid_TheBeginner's Avatar
    Join Date
    Jun 2012
    Location
    India
    Posts
    19
    Code:
    scanf(" %c", &answer);   // this code is getting skipped
    That's totally amazing for me. Could you tell me the reason behind that.

    Thank You very much rags_to_riches and qny

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    This is one of the most frequently asked questions here.

    The new line character generated by hitting the enter key is still in the input buffer. The space before the %c ignores any whitespace (like a new line) in the buffer.

  6. #6
    Registered User Sid_TheBeginner's Avatar
    Join Date
    Jun 2012
    Location
    India
    Posts
    19
    So this happens only with the %c specifier if I got it right. Am i correct?

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    All scanf() specifiers except "%c", possibly "%[...]", and (for very different reasons) "%n" ignore leading whitespace.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot find ( before ; error
    By seannabeast in forum C Programming
    Replies: 4
    Last Post: 12-07-2011, 11:29 AM
  2. Cant Find Error
    By Toonzaka in forum C Programming
    Replies: 3
    Last Post: 08-10-2008, 02:00 PM
  3. one error....can't find it
    By ammanbesaw in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2007, 10:36 AM
  4. Can anyone find why I am getting this error?
    By Drew in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2003, 05:05 PM
  5. can't find the error
    By noor_mirza in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2002, 12:58 AM