Thread: Monthly Payment Program Help

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    17

    Monthly Payment Program Help

    I cant figure out why it won't print out the value for the month.
    After it asks for my last value, it suddenly closes.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #include <ctype.h>
    
    int main()
    
    {
       int term;
        double rate, monthp, princ, months;
        
      rate = rate/1200.0;
        months =term*12.0;
      
      printf("Please enter the the principle on your payment: ");
        scanf("%lf", &princ);
        printf("Please enter the rate on your payment: ");
        scanf("%lf", &rate);
        printf("Please enter the term of your payment in years: ");
        scanf("%d", &term);
      
     
       
        monthp = ((rate + (rate/(pow((1 + rate),(months))-1))) * princ);
    
       
        
        printf("Monthly Payment is: %lf", monthp);
        
        
        
        
        
        getchar();
        return 0;
        }

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Ask yourself this question, how many times did you press the enter button?
    Code:
    printf("Please enter the the principle on your payment: ");
        scanf("%lf", &princ);
        printf("Please enter the rate on your payment: ");
        scanf("%lf", &rate);
        printf("Please enter the term of your payment in years: ");
        scanf("%d", &term);
    You may want to take a closer look at this FAQ again for some other options to just getchar().
    Last edited by AndrewHunter; 07-13-2011 at 10:09 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    17
    3 times (ok?)

    I tried putting in system("PAUSE"); after my last print command but i get a giant number.

    I've compiled and ran other programs, but none had this kind of problem.
    However, I tested the equation by testing some numbers out before and they came out to the correct answer and the last print command worked.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The scanf and printf functions are for specifically formatted IO. If you want it to read a number then a newline, make it do so:
    Code:
    scanf( "%d\n", &someint );
    You can of course check the return value against what the expected value is to ensure it has read what you wanted.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Oops, I meant to send you to the Flushing the input buffer FAQ.

    The one while loop will clear out the input buffer so you won't have to stack getchar() to keep the console open. Once the buffer is flushed one getchar() will do the job, e.g.
    Code:
    ...your code...
    while ((ch = getchar()) != '\n' && ch != EOF);
    getchar();
    return(0);
    You would have do declare ch, it is an int.

    EDIT: what quzah said.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    17
    Thanks! 1 program down 2 to go! I'll be back...

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    10
    Quote Originally Posted by sharingan_gv View Post
    I cant figure out why it won't print out the value for the month.
    After it asks for my last value, it suddenly closes.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #include <ctype.h>
    
    int main()
    
    {
       int term;
        double rate, monthp, princ, months;
        
    /*in here you can't know what rate and months will be*/
      
    rate = rate/1200.0;         /* what is the first value of rate?? */
        months =term*12.0;     /*and what is the first value of term?? */
      
      printf("Please enter the the principle on your payment: ");
        scanf("%lf", &princ);
        printf("Please enter the rate on your payment: ");
        scanf("%lf", &rate);
        printf("Please enter the term of your payment in years: ");
        scanf("%d", &term);
      
     
       
        monthp = ((rate + (rate/(pow((1 + rate),(months))-1))) * princ);
    
       
        
        printf("Monthly Payment is: %lf", monthp);
        
        
        
        
        
        getchar();
        return 0;
        }
    read my comments i put into your code...

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Like this....

    Code:
        printf("Monthly Payment is: %lf \n", monthp);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calculate monthly payment
    By yacek in forum C Programming
    Replies: 2
    Last Post: 10-04-2010, 07:59 PM
  2. monthly payment from math to C++
    By summerwine in forum C++ Programming
    Replies: 4
    Last Post: 06-10-2010, 05:31 PM
  3. May monthly contest
    By PJYelton in forum Contests Board
    Replies: 43
    Last Post: 05-18-2005, 06:37 PM
  4. New Monthly Contest!
    By PJYelton in forum Contests Board
    Replies: 50
    Last Post: 03-22-2005, 08:27 PM
  5. Bet Payment: Kermi
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-20-2003, 08:00 PM