Help! Can't read decimal number

This is a discussion on Help! Can't read decimal number within the C Programming forums, part of the General Programming Boards category; Hi, I've written the following program, and somehow b'cos of the compiler i'm using. it won't read in a decimal ...

  1. #1
    Unregistered
    Guest

    Help! Can't read decimal number

    Hi,
    I've written the following program, and somehow b'cos of the compiler i'm using. it won't read in a decimal number.

    The below file is my fprint.h file , which 'allows' me to read in floats
    --------------------------------------------------------------------------------
    void Fprint(double,int);

    void Fprint(double num,int dec)
    {
    double num2,num3;
    long j,y,z,i;
    y=(long)num;
    if (dec>0){
    i=dec;
    dec=1;
    while (i>0){
    dec=dec*10;
    i--;
    }
    num2=(double)y;
    num3=(num-num2)*dec;
    z=(long)num3;
    num3=((num-num2)*dec)*10;
    j=(long)num3;
    if (j%10>4){
    z++;
    }
    printf("%ld.%ld ",y,z);
    }
    else{
    printf("%ld ",y);
    }
    }

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

    #include <stdio.h>
    #include <fprint.h>

    main()
    {
    int i;
    int balance; /*the amount read in as bank balance*/
    int a_interest; /*an annual interest rate read in*/

    float value_a; /*interest rate for annually*/
    float value_m; /*interest rate for monthly*/
    float value_d; /*interest rate for daily*/
    float interest;

    float balance_a; /*value of bank account in 10yrs after annually*/
    float balance_m; /*value of bank account in 10yrs after monthly*/
    float balance_d; /*value of bank account in 10yrs after daily*/

    printf("Welcome to option [3].\n");
    printf("Calculate interest on a bank account balance.\n\n");
    printf("Please enter your bank account balance: ");
    scanf("%d", &balance);

    printf("Please enter an (annual) interest rate: ");
    scanf("%d", &a_interest);

    interest =(float)a_interest;
    value_a =(interest/100) + 1;
    value_m =((interest/100)/12) + 1;
    value_d =((interest/100)/365) + 1;

    balance_a=balance;
    balance_m=balance;
    balance_d=balance;

    for (i = 0; i < 10; i++)
    {
    balance_a = balance*value_a;
    }

    for(i = 0; i < 10*12; i++)
    {
    balance_m = balance*value_m;
    }

    for(i = 0; i < 10*365; i++)
    {
    balance_d = balance*value_d;
    }

    printf("\n");
    printf("STATEMENT OF ACCOUNT:\n");
    printf("The balance of account in 10 years time adding interest annually is $");
    Fprint(balance_a, 2);
    printf("\n");
    printf("The balance of account in 10 years time adding interest monthly is $");
    Fprint(balance_m, 2);
    printf("\n");
    printf("The balance of account in 10 years time adding interest daily is $");
    Fprint(balance_d, 2);
    printf("\n\n");

    }

    -------------------------------------------------
    eg: if my initial balance = 100, and interest rate = 5.5
    it would read in as 5 only....

    Is there any method that I could force it to read in the interest rate ?

    thanks!

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Change your %d to %f...

  3. #3
    Unregistered
    Guest

    Thumbs down

    I'm using a compiler known as pacifc c by hitech, the url is
    http://www.hitech.com.au/products/pacific.html

    somehow, when i changed the %d to %f and declares a_interest to a float, it won't let me enter a sum after i entered my balance.
    my compiler doesnt read in floats, is there any way i could force the int to umm something then convert it back to the total i want
    say entered 5.5, read in and a_interest * 100 = x

    annual balance = x /100
    etc
    im not sure. just a thought.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trouble with creating a loop to read certain number of inputs
    By import tuner650 in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2008, 08:28 PM
  2. how to read hard disk serial number
    By toufiq_raja in forum C Programming
    Replies: 4
    Last Post: 08-18-2005, 11:08 PM
  3. how to read hard disk serial number
    By toufiq_raja in forum C++ Programming
    Replies: 3
    Last Post: 08-18-2005, 05:32 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 05:25 AM
  5. finding the decimal part of a number
    By Geo-Fry in forum C++ Programming
    Replies: 13
    Last Post: 07-31-2003, 01:43 PM

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