need help on code please.

This is a discussion on need help on code please. within the C Programming forums, part of the General Programming Boards category; Hi ppl Im having a bit of trouble with one of my c programs. Im sort of new to c ...

  1. #1
    Unregistered
    Guest

    Unhappy need help on code please.

    Hi ppl
    Im having a bit of trouble with one of my c programs. Im sort of new to c prog. so im not too sure if im like failing to notice an obvious mistake. anyway heres what the problem is:

    im trying to code a prog that will do investment calculations. It'll accept input of type int and double (or float). I ask for each variable to be entered, scanf it and then plug it all into an equation:
    CODE
    -----------------------
    #include <stdio.h>
    #include <math.h>
    main()
    {

    int t1, t2, t3, n1, n2, n3;
    double p1, p2, p3, a1, a2, a3, i1, i2, i3;

    printf(" \n Investment Calculator ");
    printf(" \n --------------------- ");
    printf(" \n ");
    printf(" \n Entering data for Investment A: \n");
    printf(" \n Please Enter the Principal : ");
    scanf(" %lf ", &p1);

    /* testing */

    /*printf(" \n entered : %.2f", p1); */

    printf(" \n Please Enter the Annual Interest Rate :");
    scanf("%lf", &i1);
    printf(" \n Please Enter the Number of Compouding Periods per Year: ");
    scanf("%d", &n1);
    printf(" \n Please Enter the Number of Years over which you are calculating this Investment : ");
    scanf("%d", &t1);
    a1 = p1 * pow(1.0 + (i1 / n1), (n1 * t1));

    printf(" \n The final amount is : %.2f", a1);





    return 0;

    }

    --------------

    i was test running the prog as i coded and i came accross a strange problem. After it asks to enter the Principal value and i enter it and press ENTER nothing happenes. The prog just freezes. i put some testing code in but that never came up on the screen either (later i uncomented it). When i tried to close the prog once i accidentaly presses ctrl_x and enter after it and the rest of the prog just flashed before me and exited (didnt ask for other inputs - the text just printed and ------ end). i cant c what the problem is so im asking you experienced ppl to help me out.

    thanx a lot

    p.s.
    i also tried to make the double variables into float type. didnt do anything.
    i have also tried coding the prog with functions in it but that had the same problem:

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

    double inv(double, double, int, int);
    void quit(void);
    void proc(void);


    main()
    {

    char qw;

    printf(" \n ");
    printf(" \n Investment Calculator ");
    printf(" \n --------------------- ");
    printf(" \n To continue press c. To quit press q. > ");
    scanf("%c", &qw); /* get choice */
    fflush(stdin); /* flush (clear) input */

    while(qw != 'c' && qw != 'q')
    {
    printf("\n please enter (c) or (q) > ");
    scanf("%c", &qw);
    fflush(stdin);

    }
    if (qw == 'q')
    {
    quit();
    }
    else if (qw == 'c')
    {

    proc();
    }



    return 0;

    }

    void proc(void)
    {

    int t1, t2, t3, n1, n2, n3;
    double p1, p2, p3, a1, a2, a3, i1, i2, i3;

    printf(" \n Entering data for Investment A: \n");
    printf(" \n Please Enter the Principal : ");
    scanf(" %lf ", &p1);
    printf(" \n Please Enter the Annual Interest Rate :");
    scanf("%lf", &i1);
    printf(" \n Please Enter the Number of Compouding Periods per Year: ");
    scanf("%d", &n1);
    printf(" \n Please Enter the Number of Years");
    printf("\n over which you are calculating this Investment : ");
    scanf("%d", &t1);

    a1 = inv(p1, i1, n1, t1);

    printf(" \n Investment A: %lf ", a1);

    }

    double inv(double p, double i, int n, int t)
    {
    double A;

    A = p * pow(1.0 + (i / n), (n * t));

    return A;
    }

    void quit(void) /* function to terminate the prog */
    {
    printf("\n Exiting the program.....\n");
    printf("\n");
    exit(1);
    }

  2. #2
    Registered User MisterBadger's Avatar
    Join Date
    Mar 2002
    Posts
    16

    Smile ...spaced out...

    I noticed a space before %lf as commented below. Removing this allowed the program to run.

    I added the line: i1/=100.0; which gave a correct outcome to the compounded values. I'm not an investments guy and I can't comment on the formula further down the program.

    I cast part of the formula to ensure a "double" outcome from the calculation where the denominator was an "int". I may be wrong, but I think makes certain of accuracy. I think its possible for the decimal part to be truncated. Perhaps someone will post a view on this, please?!

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()  // I made it int main()... : )
    {
    int t1, t2, t3, n1, n2, n3;
    double p1, p2, p3, a1, a2, a3, i1, i2, i3;
    
    /* Decided to initialize all variables to zero... */
    
    t1=t2=t3=n1=n2=n3=0;
    p1=p2=p3=a1=a2=a3=i1=i2=i3=0.0;
    
    printf(" \n Investment Calculator ");
    printf(" \n --------------------- ");
    printf(" \n ");
    printf(" \n Entering data for Investment A: \n");
    printf(" \n Please Enter the Principal : ");
    scanf("%lf", &p1);  // There was a space in front of the %lf...
    
    /* testing */
    
    printf(" \n entered : %.2lf", p1);  //made it %.2lf...
    printf(" \n Please Enter the Annual Interest Rate :");
    scanf("%lf", &i1);
    
    i1/=100.0; // This line gives correct final amount... is this a "formula thing"...
    			  // See the results with and without this line...
    
    printf(" \n Please Enter the Number of Compouding Periods per Year: ");
    scanf("%d", &n1);
    printf(" \n Please Enter the Number of Years over which you are calculating this Investment : ");
    scanf("%d", &t1);
    a1 = p1 * pow(1.0 + ((double)i1 / n1), (n1 * t1));
    
    // i1 is double, n1 is int: I cast as double to avoid "integer division" where
    // accuracy of the decimal in the result can be lost...
    
    printf(" \n The final amount is : %.2lf", a1);  // also changed to %.2lf here...
    
    return 0;
    }
    MisterBadger hopes this is helpful and will gladly receive advice on investments!

    Happy Easter weekend!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 09:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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