Need C Help Please

This is a discussion on Need C Help Please within the C Programming forums, part of the General Programming Boards category; Code: I wrote a c programming to calculate monthly mortgage but it seems to be outputting the incorrect data this ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    31

    Exclamation Need C Help Please

    Code:
    I wrote a c programming to calculate monthly mortgage but it seems to be outputting the incorrect data this is my code below. Don't seem to know where im going wrong. Thank you alot
    
    
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main()
    {
    
        int duration;
        int number_of_months = duration * 12;
        int principal;
        float interest = interest/12;
    
        float section_1 = principal * (interest * (pow((1 + interest), number_of_months)));
        float section_2 = pow((1 + interest), number_of_months) - 1  ;
    
        float monthly_mortgage = section_1 / section_2;
    
    
    
        printf("What is the principal: \n");
            scanf("%d", &principal);
                //fflush(stdin);
    
        printf("What is the interest: \n");
            scanf("%f", &interest);
                //fflush(stdin);
    
        printf("What is the duration of the loan: \n");
            scanf("%d", &duration);
    
    
        system("CLS");
        fflush(stdin);
    
            printf("YOUR MORTGAGE IS: %.2f", monthly_mortgage);
    
            return 0;
    
    }
    Last edited by teensicle; 03-23-2011 at 11:05 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    8 posts, and still no code tags - it's not looking good for your learning skills I'm afraid.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok...

    Please edit your post to put the code into code tags...
    << !! Posting Code? Read this First !! >>

    Plus a few things...
    do not use fflush(stdin) ... it doesn't do what you think it does.

    You are actually doing the calculations before you know the values of your variables. You need to re-order things. C works from the top of a file downward... so you need to get your user input before you run the calculations.

    The correct form of main is ... int main (void)

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    31
    Thank You! Corrections To My Code!!!

    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main(void)
    {
    
        int duration;
        int number_of_months;
        int principal;
        float interest;
    
        float section_1;
        float section_2;
    
        float monthly_mortgage;
    
    
    
        printf("What is the principal: \n");
            scanf("%d", &principal);
    
        printf("What is the interest: \n");
            scanf("%f", &interest);
    
        printf("What is the duration of the loan: \n");
            scanf("%d", &duration);
    
    
    
        number_of_months = duration * 12;
        interest = interest/12;
    
        section_1 = principal * (interest * (pow((1 + interest), number_of_months)));
        section_2 = pow((1 + interest), number_of_months) - 1  ;
    
        monthly_mortgage = section_1 / section_2;
    
    
    
    
    
    
        system("CLS");
    
            printf("YOUR MORTGAGE IS: %.2f", monthly_mortgage);
    
            return 0;
    
    }

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    And does it work now?

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    31
    When do i know when to use "void" though i am yet to completely understand

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The void in int main (void) tells the compiler you are not expecting command line inputs.

    In a function with void function (int x) you are telling it the function does not return a value.
    In a function with int function (void) you are telling it the function does not accept values.

    It's a way of saying "nothing here"...

    In contrast a function with (for example) int function() says the function accepts any number of unspecified parameter values...

    So when you use int main() you are misinforming the compiler.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    31
    Thanks for the help i will keep you posted on my project

Popular pages Recent additions subscribe to a feed

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