Hi guys!, I have a code here having a problem about double and I'm kinda lost so I don't know what to do. btw thanks for reading my thread, I would greatly appreciate if you can help me fix this thing.

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdbool.h>
#define FLUSH while (getchar() != '\n')

typedef struct {
        char id[6];
        char lname[15];
        char fname[20];
        char address[50];
        double balance;
}INFO;

typedef struct {
        char id[6];
        char doP[12];
        double amountpd;
}fPay;

typedef struct {
         char id[6];
         char doP[12];
         double amountpd;
}fLoans;

void borrow (FILE *);
INFO getInfo (void);
INFO DispData (FILE *);
int main (void)
{
    FILE *fp;
    int choice;

    do {
    system("cls");
    printf ("Loan System\n\n");
    printf ("1] Register Borrower\n2] Record Loan\n3] Record Loan Payment\n4] View Individual Record\n5] Exit\n\n\n");
    printf ("Choice: ");
    scanf ("%d", &choice);
    FLUSH;
          switch (choice)
          {
                 case 1: system ("cls"); 
                         borrow(fp);
                         break;
                 case 4: DispData(fp);
                         break;
                 case 5: return;

                 default: printf ("\nInvalid input...\n");
                          system("pause");
          }

                 }while (choice != 5);
}
void borrow (FILE *fp)
{
     INFO info;

     info = getInfo();
     return;
}
INFO getInfo (void)
{
     INFO info;
     FILE *fp;
     int write, read;

     if ((fp = fopen("Borrower.txt", "ab")) == NULL)
     {
        printf ("Error! Unable to open file");
     }
     else
     {
         printf ("Borrower's Personal Information\n\n");
         printf ("Enter Borrower's ID: ");
         fgets(info.id, sizeof(info.id), stdin);
         FLUSH;
         printf ("Enter Borrower's Lastname: ");
         scanf ("%s", &info.lname);
         FLUSH;
         printf ("Enter Borrower's Firstname: ");
         fgets(info.fname, sizeof(info.fname), stdin);
         printf ("Enter Borrower's Address: ");
         fgets(info.address, sizeof(info.address), stdin);
         printf ("Enter Borrower's Current Balance: ");
         scanf ("%f", &info.balance);
         FLUSH;

     }
           if (write = fwrite (&info, sizeof(INFO), 1 , fp) > 0)
           {
              printf ("\nWrite Successful\n\n\n\n");
           }
           else
           {
               printf ("Unable to write data");
           }

     fclose(fp);     
     if ((fp = fopen ("Borrower.txt", "rb")) == NULL)
     {
             printf ("\n\nUnable to open file...");
     }
     else
     {
         while ((read = fread(&info, sizeof(INFO), 1 , fp)) != 0)
           {
                  printf ("*****************************\n");
                  printf ("ID: %s\n\n", info.id);
                  printf ("First name: %s", info.fname);
                  printf ("Last name: %s\n", info.lname);
                  printf ("Address: %s\n", info.address);
                  printf ("Current Balance: %.2f\n", info.balance); 
                  printf ("\n\n");
           }
     }

     fclose(fp);
     getch();
}
INFO DispData (FILE *fp)
{
     INFO getInfo;
     int read;
     char find[6];
     bool found = false;

     system("cls");
     printf ("Enter the ID of the Reciever: ");
     fgets(find, 6, stdin);
     printf ("Searching record for ID: %s", find);

     if ((fp = fopen("Borrower.txt", "rb")) == NULL)
        {
             printf ("\n\nUnable to open file");
        }
     else
         {
             while ((read = fread (&getInfo, sizeof(INFO), 1, fp)) != 0)
                   {
                          if (strcmp(find, getInfo.id) == 0)
                             {
                                           found = true;
                             }
                   }
                   if (found)
                      {
                             printf ("\n\n--Found--");
                             printf ("\nID: %s\n\n", getInfo.id);
                             printf ("First name: %s", getInfo.fname);
                             printf ("Last name: %s\n", getInfo.lname);
                             printf ("Address: %s\n", getInfo.address);
                             printf ("Current Balance: %f\n", getInfo.balance); 
                             printf ("\n\n");
                      }
                   else
                   {
                       printf ("\n\nUnable to find %s", find);
                   }
         }

             fclose(fp);
             getch();
} 

Would you be kind enough to explain what exactly the problem is? It's too early for guessing games here. ;)

Edited 2 Years Ago by deceptikon

Sorry, I forgot to mention, well the problem is that when I output the double I won't get the right amount or value and I'm also having a problem dealing with decimal value like "0.00". I know its %.2f but it won't work.Thanks in advance

What input do you give it, what output does it give back, what is it about the output that do you not like?

If you're going to use floating point values, you need to read this:
http://floating-point-gui.de/

If you're going to use floating point values for financial information, you need to stop, once you've experimented with it to learn :)
http://spin.atomicobject.com/2014/08/14/currency-rounding-errors/
http://programmers.stackexchange.com/questions/62948/what-can-be-done-to-programming-languages-to-avoid-floating-point-pitfalls

Edited 2 Years Ago by Moschops

This article has been dead for over six months. Start a new discussion instead.