Thread: Why does it skip a scanf()?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    Why does it skip a scanf()?

    Code:
    #include <stdio.h>
    #include <math.h>
    main() {
    
            int mon;
            double price, finance, rate , mpay, tpay, r;
    
            printf("Enter the price of the house (in $):");
            scanf("%.2f", &price);
            if (price<0)
                    printf("Please enter positive price\n");
            else {
            printf("Enter the amount to be financed (in $):");
            scanf("%.2f", &finance);
            if ((finance<0)||(finance>0.8*price))
                    printf("Please enter appropriate values for finance\n");
            else {
            printf("Enter the annual percentage rate (in percent):");
            scanf("%.2f", &rate);
            if (rate<=0)
                    printf("Please enter a positive rate\n");
            else {
            r=(double)rate/1200;
            printf("Enter the term in number of months:");
            scanf("%d", &mon);
            if ((mon<=0)||(mon%12!=0)||(mon>480))
                    printf("Please enter appropriate value for number of months\n");
            else {
                    mpay=finance*r*pow((1+r),mon)/(pow((1+r),mon)-1);
                    tpay=mon*mpay;
                    printf("\nThe price of the house is: %.2f $\n", price);
                    printf("The amount to be financed is: %.2f $\n", finance);
                    printf("The annual percentage rate is: %.2f percent\n", rate);
                    printf("Your monthly payment is : %.2f $\n", mpay);
                    printf("Your total payment will be: %.2f $\n", tpay);
    }
    }}}}


    When i run this code, it asks for the first price, and after that, it skips to the the message "Please enter correct rate". like it doesnt even go to the second scanning part where it asks for the finance. Please Help
    Last edited by Salem; 10-21-2010 at 02:21 AM. Reason: Added [code][/code] tags

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please use code tags.

    Check the position of your braces.

    Where is the matching closing brace for the first "else" opening brace ?

    Jim

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    SourceForge.net: Leaves data in input buffer - cpwiki

    And this program is C, not C++, so it's in the wrong forum.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rags_to_riches
    And this program is C, not C++, so it's in the wrong forum.
    Moved.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple XOR Program
    By dolfaniss in forum C Programming
    Replies: 8
    Last Post: 05-24-2010, 01:27 PM
  2. some questions about scanf
    By winggx in forum C Programming
    Replies: 1
    Last Post: 03-28-2010, 06:16 PM
  3. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  4. scanf skips lines of code in DOS using Dev-C++ and Windows XP
    By jenovanomusuko in forum C Programming
    Replies: 9
    Last Post: 12-21-2008, 03:10 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM