Thread: Calculation Help

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    10

    Unhappy Calculation Format

    I can not get the second calculation to work at all. Any hints as to what I might be doing wrong would be greatly appreciated.

    Thank you for your time.

    reddtee
    Last edited by reddtee; 11-23-2001 at 03:47 PM.

  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
    Well there are a bunch of warnings to get rid of first - in particular, one of your scanf's is broken (conversion and parameter are inconsistent)

    You would have seen this had you printed out your values after you had read them in - real simple debug thing to do.

    > main()
    cpr-test.c:10: warning: return-type defaults to `int'
    Whilst the default is indeed int - implicit declarations like this are poor style, and are to be phased out.

    cpr-test.c: In function `main':
    > printf("\n\nYour monthly payment will be: $%lf\n", payment);
    cpr-test.c:49: warning: use of `l' length character with `f' type character

    > scanf("%f", &payment);
    cpr-test.c:68: warning: float format, double arg (arg 2)

    > scanf("%d", &duration);
    cpr-test.c:72: warning: int format, float arg (arg 2)

    > }
    cpr-test.c:122: warning: control reaches end of non-void function
    End main with a
    return 0;
    statement

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    10
    Thank you for your reply Salem. Being new to the world of C, I didn't even know there was a debug I could perform to let me know about errors. I will try and work on these errors before I go any further.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    10
    OK, I have most of the errors fixed. The new file has been uploaded. I appreciate any and all input.

    reddtee

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Looks depressingly identical to the old one
    cpr-test.c: In function `main':
    cpr-test.c:49: warning: use of `l' length character with `f' type character
    cpr-test.c:68: warning: float format, double arg (arg 2)
    cpr-test.c:72: warning: int format, float arg (arg 2)
    cpr-test.c:122: warning: control reaches end of non-void function

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    10
    I guess being a newbie, I don't understand what the errors are. I have tried to research them and made some changes to the code. I know time is precious but if there is anyway you could explain the errors to me a little more, I might be able to learn something from it.

    However, the last thing I want to do is depress you.

    Once again, thank you.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > cpr-test.c:49: warning: use of `l' length character with `f' type character
    You don't need the 'l'

    > cpr-test.c:68: warning: float format, double arg (arg 2)
    > cpr-test.c:72: warning: int format, float arg (arg 2)
    Both of these mean that the conversion character (eg. %f), is inconsistent with the pointer-to-variable parameter you also provide.
    So check your scanf manual carefully, and make sure you're using the right % conversion for your variable.


    > cpr-test.c:122: warning: control reaches end of non-void function
    main should end with
      return 0;
    and not just fall off the end

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    10
    OK, here goes - I took out the 'l' in my scanf statements.

    I made sure all my printf's and scanf's variables matched my variable declarations.

    I placed a return 0; at the end of main.

    However, I'm still getting this error on the following code. It compiles ok, but still gives me the error. I have tried several different ways but can not rid the error.

    Conversion may loose significant digits.

    printf("\nWould you like to compute another loan duration (y or n)?");
    getchar();
    ch = getchar();
    if (ch == 'n' || ch == 'N')
    break;

    I have uploaded my corrected code.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    ch should be declared as an int

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    10
    Wow - I think I'm trying to make this too difficult.

    Thank you so much, my program now compiles with no errors (and I also feel a little bit smarter).

    Your quick replies are very much appreciated.

    Now, back to my original question: I'm having calculation problems with my second and third calculations. The first one works perfect. Any suggestions?

    reddtee

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Look, I don't know what you're playing at, but this is still the same code, containing the same errors as it had originally.

    cpr-test.c:10: warning: return-type defaults to `int'
    cpr-test.c: In function `main':
    cpr-test.c:49: warning: use of `l' length character with `f' type character
    cpr-test.c:68: warning: float format, double arg (arg 2)
    cpr-test.c:72: warning: int format, float arg (arg 2)
    cpr-test.c:122: warning: control reaches end of non-void function

    Make sure you've actually uploaded what you think you have, by downloading it yourself.

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    10
    I'm really sorry that you feel I'm going out of my way to upset you. That is not the case. I may be a little ignorant, because I am new at C but I have always been a polite and thankful person. Yes - I have downloaded the program myself.

    I don't know what I am doing wrong, but it is not on purpose Salem. So that you don't get upset any further, I will paste my code below. Now, if you do not have the patience to deal with beginners, maybe you can refer someone to me that does.

    #include <stdio.h>
    #include <conio.h>
    #include <math.h>

    int main()
    {
    char option;
    int ch;
    double principal, rate, r, duration;
    double payment, x, y;

    option = ' ';

    while (option != 'Q' && option != 'q')
    {
    clrscr();
    printf("%s%s%s%s%s%s%s",
    " LOAN CALCULATOR\n\n",
    " Choose one of the following four options:\n\n",
    " (P) Compute the amount of the loan (P)ayment.\n",
    " (A) Compute the loan (A)mount.\n",
    " (D) Compute the (D)uration of the loan.\n",
    " (Q) (Q)uit this program.\n\n",
    " Enter the appropriate letter here: ");
    scanf("\n%c", &option);
    switch (option)
    {
    case 'P': case 'p':
    clrscr();
    while (1)
    {
    printf("%s%s",
    "\n\n\nTo compute the amount of the loan payment, ",
    "input the following information:\n\n");
    printf("Enter the total amount of the loan:");
    scanf("%f", &principal);
    printf("Enter the annual interest rate:");
    scanf("%f", &rate);
    printf("Enter the number of years to repay the loan:");
    scanf("%f", &duration);

    x = 1 + rate / 12;
    y = duration * 12;
    payment = (principal * rate / 12) / (1 - (1 / pow(x,y)));
    printf("\n\nYour monthly payment will be: $%.2f\n", payment);
    printf("\nWould you like to compute another loan payment (y or n)?");
    getchar();
    ch = getchar();
    if (ch == 'n' || ch == 'N')
    break;
    }
    break;
    case 'A': case 'a':
    clrscr();
    while (1)
    {
    printf("%s%s",
    "\n\n\nTo compute the total loan amount, ",
    "input the following information:\n\n");
    printf("Enter the preferred payment amount:");
    scanf("%f", &payment);
    printf("Enter the annual interest rate:");
    scanf("%f", &rate);
    printf("Enter the number of years to repay the loan:");
    scanf("%f", &duration);
    r = rate / 100;
    x = 1 + r;
    y = duration * 12;
    principal = (payment * r) * (1 - (1 / pow(x,y)));
    printf("\n\nYour total loan amount will be: $%.2f\n", principal);
    printf("\nWould you like to compute another loan amount (y or n)?");
    getchar();
    ch = getchar();
    if (ch == 'n' || ch == 'N')
    break;
    }
    break;
    case 'D': case 'd':
    clrscr();
    while (1)
    {
    printf("%s%s",
    "\n\n\nTo compute the duration of a loan, ",
    "input the following information:\n\n");
    printf("Enter the total amount of the loan:");
    scanf("%f", &principal);
    printf("Enter the monthly loan payment:");
    scanf("%f", &payment);
    printf("Enter the annual interest rate:");
    scanf("%f", &rate);
    r = rate / 100 / 12;
    x = payment - r * principal;
    y = r + 1;
    duration = (log(payment) - log(x)) / (log(y));
    printf("\n\nThe duration of your loan will be: %.2f years\n", duration);
    printf("\nWould you like to compute another loan duration (y or n)?");
    getchar();
    ch = getchar();
    if (ch == 'n' || ch == 'N')
    break;
    }
    break;
    case 'Q': case 'q':
    break;
    }
    }
    getch();
    return 0;
    }

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When you say you're having problems with the second and third calculation, what exactly do you mean? Choices 2 and 3 from the menu? The second and third times through the loop? Or what?

    I suspect, but am just guessing, that you're doing your math wrong:

    r = rate / 100 / 12;

    This is the same thing as:

    r = rate / 8.3333333;

    Is that what you want? (100/12 = 8.33333)


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

  14. #14
    Registered User
    Join Date
    Nov 2001
    Posts
    10
    Quzah, thanks for the reply. I should have been more specific, I meant choices 2 and 3 on the menu.

    r = rate / 100 / 12;
    What I wanted to do here is take the user input of an annual interest rate and convert it to decimal format and then divide it by 12 months in a year to get a monthly interest rate.

    For example:
    User input of 8% annual interest rate
    / by 100 to get 0.08
    / 12 to get 0.006666666..... interest charged monthly

    However, even if I take this line of code out completely, choices 2 and 3 on the menu will show 0.00, regardless of user input. I have several test cases that will work for menu choice 1 but not the other two.

    I do not have a strong math background (yet) so I was afraid it was my calculations.

    reddtee

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What I find helpful is to add "debug" lines. I'll note them with comments.

    Code:
    case 'A': case 'a': 
        clrscr(); 
        while (1) 
        { 
            printf("%s%s", 
                "\n\n\nTo compute the total loan amount, ", 
                "input the following information:\n\n"); 
            printf("Enter the preferred payment amount:"); 
            scanf("%f", &payment); 
    
            printf("Enter the annual interest rate:"); 
            scanf("%f", &rate); 
    
            printf("Enter the number of years to repay the loan:"); 
            scanf("%f", &duration); 
    
            /*** DEBUG ***/
            printf("DEBUG: payment = %f, rate = %f, duration = %f\n",
                payment, rate, duration );
    
            r = rate / 100.0; 
            /*** DEBUG ***/
            printf("DEBUG: rate = %f, r = %f\n", rate, r );
    
            x = 1.0 + r; 
            /*** DEBUG ***/
            printf("DEBUG: x = %f\n", x );
    
            y = duration * 12.0; 
            /*** DEBUG ***/
            printf("DEBUG: y = %f\n", y );
    
            principal = (payment * r) * (1 - (1 / pow(x,y))); 
            /*** DEBUG ***/
            printf("DEBUG: principal = %f\n", principal );
    
            printf("\n\nYour total loan amount will be: $%.2f\n", principal); 
            printf("\nWould you like to compute another loan amount (y or n)?"); 
            getchar(); 
            ch = getchar(); 
            if (ch == 'n' || ch == 'N') 
        break; 
        } 
    break;
    Do something similar for your third block. You may want to provide "debug" lines for any complex math also:

    printf( "(1 - (1 / pow(x,y))) = %f\n", (1 - (1 / pow(x,y))) );

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Date calculation program
    By putty88 in forum C Programming
    Replies: 5
    Last Post: 04-17-2009, 07:24 AM
  2. Problem with modulo calculation
    By manutdfan in forum C Programming
    Replies: 6
    Last Post: 01-12-2009, 03:37 PM
  3. C# calculation problem
    By Diablo02 in forum C# Programming
    Replies: 13
    Last Post: 10-25-2007, 08:42 PM
  4. Help with calculation and loop
    By Betty in forum C Programming
    Replies: 7
    Last Post: 04-10-2006, 05:37 AM
  5. help with pay calculation program
    By shugstone in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2003, 09:38 AM