Thread: don't have any clue how to do this

  1. #46
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Why are you doing

    Code:
     n = n / 12;
    Surely the formula wants "n" in months, the user inputs years, so you would want

    Code:
    n *= 12;
    Or am I not reading your assignment properly?

    Also, wouldn't "n" be better stored as an integer?

    And change

    Code:
    printf("Your monthly payment is: %f", p);
    to

    Code:
    printf("Your monthly payment is: %f\n", p);
    Also, the rate they input (r) is per annum as a percentage, the formula wants it as a decimal per month, so you need a

    Code:
    r = r / 1200;
    In there before you use (r) in a calculation.



    Assuming you are using the gcc compiler, you want to have a compile line similar to

    gcc foo.c -ofoo -lm

    Where you replace foo.c with the name of your source code and foo with the name you want for your executable.
    Last edited by SKeane; 10-09-2006 at 05:47 AM.

  2. #47
    Registered User
    Join Date
    Oct 2006
    Posts
    38
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main(void)
    
    {
    
        double r, p, x, n, y, a;
    
        printf("Enter the amount of the loan ->");
    
        scanf("%lf\n", &n);
    
        n *= 12;
    
        printf("Enter the interest rate ------>");
    
        scanf("  %lf  \n", &r );
    
        printf("Enter the term (in years) ---->");
    
        scanf("%lf\n", &a );
    
        r = r / 12;
    
        p = a * (r * (pow ((double) x , (double) n)) ) / ( (pow ((double) x ,
    ((double) n)) )- 1 );
    
        printf("your monthly payment is :%f\n", p);
    
        return 0;
    
    }
    output:

    Enter the amount of the loan -> #.##
    #.##
    Enter the interest rate ------>#.##
    Enter the term (in years) ---->##
    your monthly payment is : #######

    no matter what numbers i put into the first 4 scanf prompts... the monthly payment is not correct. And how do i get rid of the second prompt? i don't want to enter the loan twice...

    thanks for taking the time.
    Last edited by newcstudent; 10-09-2006 at 01:51 PM.

  3. #48
    Registered User
    Join Date
    Oct 2006
    Posts
    38

    I still don't know what i need to fix...

    if you can help me. i'd appreciate it.

  4. #49
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Abandon scanf if you don't understand all of its subtlties.
    User Input: Strings and Numbers [C]

    Don't use a value such as x until after it has a value.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #50
    Registered User
    Join Date
    Oct 2006
    Posts
    38

    Unhappy that's not useful

    the point of the assignment is to learn scanf. please help me with what i'm trying to do. stop making me feel pathetic.=(

  6. #51
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by newcstudent
    the point of the assignment is to learn scanf.
    Oh. great.

    Quote Originally Posted by newcstudent
    please help me with what i'm trying to do. stop making me feel pathetic.=(
    Not what I'm trying to do. Read the FAQs in the link I posted.

    If you really want to have fun, try this:
    http://groups.google.com/groups?q=sc...ek&sa=N&tab=wg

    [edit]
    1. Okay, declare some variables.
      Code:
         double i, p, n, a;
    2. Get input.
      Code:
         printf("Enter the amount of the loan ->");
         scanf("%lf", &p);
         printf("Enter the interest rate ------>");
         scanf("%lf", &i );
         printf("Enter the term (in years) ---->");
         scanf("%lf", &n );
    3. Apply formula to values.
      Code:
         n *= 12;
         a = p * (i * pow(1 + i, n)) / (pow(1 + i, n) - 1);
    4. View output.
      your monthly payment is :60.434457
      Is this what you expect?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #52
    Registered User
    Join Date
    Oct 2006
    Posts
    38
    exactly like that....only it doesnt compile

  8. #53
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It does for me. Unfortunately, I can't see your computer screen. [Translation: post your code.]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #54
    Registered User
    Join Date
    Oct 2006
    Posts
    38
    This is yours
    Code:
    #include <stdio.h>
    #include <math.h>
    
    main()
    {
    double i, p, n, a;
    printf("Enter the amount of the loan->");
    scanf("%lf\n", &p);
    printf("Enter the interest rate ->");
    scanf("lf\n", &i);
    printf("Enter the term (in years) ->");
    scanf("%lf\n",&n);
    n *= 12;
    a = p*(i*pow(1+i, n))/ (pow(1+i, n) -1);
    printf("Your monthly payment is--->%lf", a);
    }
    This is mine
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main(void)
    
    {
    
        double p, x, y, a;
        float r;
        int n;
        printf("Enter the amount of the loan ->");
    
        scanf("%lf", &a);
    
        printf("Enter the interest rate ------>");
    
        scanf("%f", &r);
    
        r=r/1200;
    
        printf("Enter the term (in years) ---->");
    
        scanf("%i\n",&n);
    
        n = n / 12 ;
    
        p = a * (r * (pow ((double) r+1 , (double) n)) ) / ( (pow ((double)
    1+r , ((double) n))-1 ) );
    
        printf("your monthly payment is :%f\n", p);
    
        return 0;
    
    }

  10. #55
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Okay, so your ability to copy and paste is lacking. (And no, that's not mine. You've decided to change the scanf lines, and I know how to specify a double for printf, I also know about main, and I use indentation.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #56
    Registered User
    Join Date
    Oct 2006
    Posts
    38
    oh. sry. i added \n but only because i was trying to make your code work.

    i can't actually copy paste because of the text editor i'm running is in linux. i'm using putty. what are you talking about about printf? is something wrong?

  12. #57
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by newcstudent
    i added \n but only because i was trying to make your code work.
    Which is why I mention that scanf is not for beginners. Read piles and piles of posts and FAQs until you feel the same.

    Quote Originally Posted by newcstudent
    what are you talking about about printf? is something wrong?
    Not yours, your "rendition" of "mine" -- %lf is undefined for printf until C99 (where it is ignored).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #58
    Registered User
    Join Date
    Oct 2006
    Posts
    38
    i tried your code exactly as yours is... it didn't work...

    my code is what i had already before you gave me your code. it doesn't work either.

  14. #59
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm going to repost what Dave posted. He posted enough of his program such that all you have to do is wrap it around main(void) and provide a correct printf() call.

    Code:
    #include <stdio.h>
    
    int main ( void )
    {
       /* Define some variables.*/
       double i, p, n, a;
    
       /* Get input */   
       printf("Enter the amount of the loan ->");
       scanf("%lf", &p);
       printf("Enter the interest rate ------>");
       scanf("%lf", &i );
       printf("Enter the term (in years) ---->");
       scanf("%lf", &n );
    
       /* Apply formula to values */
       n *= 12;
       a = p * (i * pow(1 + i, n)) / (pow(1 + i, n) - 1);
    
       /* View output. */
       printf("your monthly payment is :%g\n", a);
       return 0;
    }
    I've marked differences between your code and Dave's. You be careful about saying who's wrong around here - arguments with the mods will only work when you are right. And do your work. There's an incredible amount of spoonfeeding and a lack of effort on your part.

    It works for me:
    $ ./a
    Enter the amount of the loan ->1000
    Enter the interest rate ------>1.8
    Enter the term (in years) ---->2
    your monthly payment is :1800

  15. #60
    Registered User
    Join Date
    Oct 2006
    Posts
    38
    i new the %lf was different, i said so. i missed the %g i'm gonna go try it again.

    i appreciate you being so explicit. but i resent your analogy between me and a baby. Everybody's gotta start somewhere and

    "%lf is undefined for printf until C99 (where it is ignored)."

    makes no sense to me. it's a worthless sentence to my 2 week old programming ears. also, do NOT say i didn't do any work. i posted my code above. it's a basic code and maybe YOU wouldn't have to do any work to arrive at it... but i have spent 2 sleepless nights on it trying to make it work. also, i'm getting frustrated because 2 different people told me not to use scanf since i'm not familiar with it... IT'S A HOMEWORK ASSIGNMENT, THATS THE POINT. lastly, i've only had about 4.5 hours of sleep in the last two days and i have a test in history and c tomarrow... i'll be irritable until wednesday, sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Replies: 8
    Last Post: 04-19-2006, 09:07 AM
  3. No clue what the problem is...
    By FingerPrint in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2006, 03:16 PM
  4. A really weird problem. I understand the error, but not a clue why I get the errors.
    By Finchie_88 in forum Networking/Device Communication
    Replies: 1
    Last Post: 02-20-2005, 09:48 AM
  5. No CLUE what to do...
    By stewade in forum C Programming
    Replies: 7
    Last Post: 05-03-2004, 12:02 AM