Like Tree2Likes
  • 1 Post By rags_to_riches
  • 1 Post By qny

Can't find the error

This is a discussion on Can't find the error within the C Programming forums, part of the General Programming Boards category; 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) ...

  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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    Change the line to

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

  3. #3
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    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
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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, 10: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, 09: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-02-2002, 11:58 PM

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