Why does it skip a scanf()?

This is a discussion on Why does it skip a scanf()? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <math.h> main() { int mon; double price, finance, rate , mpay, tpay, r; printf("Enter the price ...

  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 03:21 AM. Reason: Added [code][/code] tags

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,916
    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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    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
    22,265
    Quote Originally Posted by rags_to_riches
    And this program is C, not C++, so it's in the wrong forum.
    Moved.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02:27 PM
  2. some questions about scanf
    By winggx in forum C Programming
    Replies: 1
    Last Post: 03-28-2010, 07: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, 05:34 AM

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