Scanf problem

This is a discussion on Scanf problem within the C Programming forums, part of the General Programming Boards category; I'm having a problem with my program skipping over the 2nd scanf function. I am new to programming and am ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    Scanf problem

    I'm having a problem with my program skipping over the 2nd scanf function. I am new to programming and am not sure why this is.
    when I run this I do not get prompted for a 2nd input, it just skips right over it.
    Code:
    int
    main(void)
    {
        int success,m,n,ans,input;
        char op;    
        printf("Enter a simple arithmetic expression.\n");
        success = scanf("%d%c%d\n",&m,&op,&n);
        switch(op)
        {
            case'+':
            ans = m + n;
            printf("What is the answer to %d%c%d?\n",m,op,n);
            scanf(" %d\n" , &input);
            if(ans == input)
            {
                printf("correct!\n");
            }
            else
            {
                printf("No, the correct answer is %d\n",ans);
            }
            break;
            case'-':
            ans = m - n;
            break;
            case'*':
            ans = m * n;
            break;
            case'/':
            ans = m / n;
            break;
            case'%'
            ans = m % n;
            break;
        }
        printf("%d\n",success);
        return 0;

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Very common complaint. When you hit enter, the input stream gets a newline put into it. That newline char is NOT included in your variable, but it's left in the buffer. When the next scanf() is reached, the buffer is read. If a char will foul things up at that time, then it will be "skipped".

    You can fix it by adding a blank space before the first % in the second scanf(), or by putting a getchar() line of code, after the first scanf(). that will fix the problem.

    Since you've already tried the first method, I strongly suggest the latter, ( the getchar() ), and put it after each of the two scanf()'s, just to be thorough.
    Last edited by Adak; 02-12-2012 at 06:23 PM.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    Thank you very much! The getchar()worked perfectly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf problem
    By mostafa xx in forum C Programming
    Replies: 3
    Last Post: 01-16-2011, 03:39 PM
  2. Problem with scanf
    By yoavagami in forum C Programming
    Replies: 2
    Last Post: 01-02-2008, 08:14 AM
  3. scanf problem
    By thebhoy6 in forum C Programming
    Replies: 2
    Last Post: 03-18-2006, 03:48 PM
  4. scanf problem
    By a1dutch in forum C Programming
    Replies: 8
    Last Post: 04-19-2005, 05:14 AM
  5. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM

Tags for this Thread


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