problem with scanf() in while

This is a discussion on problem with scanf() in while within the C Programming forums, part of the General Programming Boards category; my problem is that it is not working that i want to do. here is the code: Code: #include<stdio.h> main() ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    42

    problem with scanf() in while

    my problem is that it is not working that i want to do.
    here is the code:
    Code:
    #include<stdio.h>
    main()
    {
    	int a,b,sum;
    	char a='y';
    do
    {printf("Enter two number to get the sum\n");
    scanf("%d%d",&a,&b);
    sum=a+b;
    printf("the sum is %d\n",sum);
    printf("do you want to continue?(y/n)\n");
    scanf("%c",&a);
    }
    while(a=='y');
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    How does scanf know when one integer stops and the next begins?

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rags_to_riches View Post
    How does scanf know when one integer stops and the next begins?
    LOL... spacey comment there.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    42
    my wonder is that why the code doesn't loop till the answer is no. but the code work fine when i put \n in scanf() function. why? can anyone help me put light on that? thanks in advance.
    Last edited by hugoguan; 11-23-2010 at 03:43 AM.

  5. #5
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39

    Question

    Nice try! Turn on compiler warnings. It should help some.

    • standard says to use int main (void)
    • main must return an int
    • scanf can eat up whitespace before entries. try scanf " %d %d" and scanf " %c"
    • conflicting types for 'a.' is it a char or an int?
    Last edited by hellork; 11-23-2010 at 04:58 AM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    42
    a is char

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    30
    when you input two numbers, separated by space, followed by the "Enter" key, you effectively put in stdin stream the following characters "1 2\n". If you don't put "\n" in your scanf", the carriage return character "\n" remains in the stdin stream. As a result, the next scanf which reads a character will always read this carriage return "\n".
    That's the problem you're having here. But your variable "a" is actually defined twice, first time integer, second time character, so you should have problem with that as well...

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    42
    chameleons, thanks you so much. i seem get more clear with that.

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Quote Originally Posted by CommonTater View Post
    LOL... spacey comment there.
    Yeah, I guess that was a bit spacey, eh? Shows you how often I use scanf in that way!

    Sorry for the brain fart.

    The right way to handle the OP's situation is to create a function like this:
    Code:
    void remove_extra_chars()
    {
        int c;
        // Consume all the character remaining in the input buffer up to and including the newline
        while ((c = getchar()) != '\n') ;
    }
    and call it after the scanf()s.
    Last edited by rags_to_riches; 11-23-2010 at 08:50 AM.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rags_to_riches View Post
    Yeah, I guess that was a bit spacey, eh? Shows you how often I use scanf in that way!

    Sorry for the brain fart.

    The right way to handle the OP's situation is to create a function like this:
    Code:
    void remove_extra_chars()
    {
        int c;
        // Consume all the character remaining in the input buffer up to and including the newline
        while ((c = getchar()) != '\n') ;
    }
    and call it after the scanf()s.
    Or...
    Code:
    while (getchar() != EOF);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with RPC and scanf
    By Ricardo_R5 in forum C Programming
    Replies: 11
    Last Post: 01-08-2007, 05:15 PM
  2. Problem with scanf float..
    By aydin1 in forum C Programming
    Replies: 6
    Last Post: 03-06-2005, 12:35 PM
  3. scanf problem
    By gsoft in forum C Programming
    Replies: 3
    Last Post: 01-04-2005, 11:42 PM
  4. problem with looping scanf
    By crag2804 in forum C Programming
    Replies: 6
    Last Post: 09-12-2002, 08:10 PM
  5. scanf problem
    By Flikm in forum C Programming
    Replies: 2
    Last Post: 11-05-2001, 12:48 PM

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