Thread: Need C Help Please

  1. #1
    Registered User
    Join Date
    Jan 2010
    Location
    Spanish Town, Jamaica, Jamaica
    Posts
    33

    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  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
    Location
    Spanish Town, Jamaica, Jamaica
    Posts
    33
    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
    Location
    Spanish Town, Jamaica, Jamaica
    Posts
    33
    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
    Location
    Spanish Town, Jamaica, Jamaica
    Posts
    33
    Thanks for the help i will keep you posted on my project

Popular pages Recent additions subscribe to a feed