简体   繁体   中英

Using data inside a binary file in a conditional statement

It's my first time asking here, so I'm sorry in advance if my post is a bit messy. I'm a freshman and my finals is to make an atm program.

My program uses switch statements for the options: the first one asks the user for their info, (account number, account name, PIN, initial deposit), while the second one is for the actual transactions: Balance check, Deposit and Withdrawal. Before you could do any one of those options, of course I'll have to check if the info (in this case the account number and PIN) matches the ones in my file.

The file is in binary. My problem is that my code for reading the file works fine if i want to display the contents of my file (all the data entries that I populated the file with are shown), but when i want to use the contents of the file (searching inside the file for a match of the user inputted account number) it only reads the first line of the file, thus only the first data entry works, while the one's after that are not read.

My question is, what am i doing wrong in my code? its confusing because if I change my code to display the contents it shows everything, meaning it reads the whole file. but when i want to search the file using a conditional statement, only the first data entry gets read. Thank you very much for your time

tldr: can't use data in file for conditionals (verify the data inside the file, cuz my code doesn't read it whole apparently, except for the first entry) but if i print the contents it reads it fully

my outputs 1.My data entries data entries 2.Entering the Account no. of the 1st one (desired output) desired output 3.Entering The account no. of the 2nd one (Problem part) problem

my main code

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include<process.h>

struct account
{
    int no;
    char name[100];
    int pin;
    float id;
}; 

main()
{    
    FILE *fptr;
    fptr = fopen("accrec.dat","ab");
    if (fptr == NULL)
    {
        printf("File does not exists \n");
        return 0;
    }
    system("cls");
    struct account accrec;
    int i,tpin[4];
    char step1,ch;

    printf("\t\tWelcome to Banking System\n\t");
    printf("\nA.Open an Account\n");
    printf("\nB.Bank Transaction\n");
    printf("\nC.exit\n");

    scanf("%c",&step1);
    switch (step1)
    {

        case 'A':
            printf("Open a New Account\n");
            printf("\nEnter the following information\n");

            printf(" \n5-digit  Account number:");
            scanf("%d",&accrec.no);

            getchar();
            printf("\nAccount Name:");
            scanf("%[^\n]s",&accrec.name);

            printf("\n4-digit Account PIN:");
                /*for(i=0;i<4;i++)
                {
                ch = getch();
                tpin[4] = ch;
                ch = '*' ;
                printf("%c",ch);
                }mask works but does not allow PIN saving     */
            scanf("%d",&accrec.pin);


            printf("\nInitial deposit:");
            scanf("%f",&accrec.id);

            fwrite(&accrec,sizeof(struct account),1,fptr);

            fclose(fptr);

            break;

        case 'B':
            {
            fptr = fopen("accrec.dat","rb");
            int choice;
            int accno = 0;
            printf("Enter Your Account no.");
            scanf("%d",&accno);
            while (fread(&accrec,sizeof(account),1,fptr)!=NULL)
            {
            if(accno == accrec.no)
            {
            printf("\tWelcome to PUPQC Banking System\n");
            printf("1.Balance Inquiry\n");
            printf("2.Deposit\n");
            printf("3.Withdrawal\n");
            printf("4.quit\n");
            scanf("%d",&choice);
            }
            else 
            {
                printf("account doesn't exist\n");
                exit(1);
            }   


            fclose(fptr);

            switch (choice)
            {
                case 1:

                    printf("BALANCE INQUIRY\n");
                    printf("Current Balance:");
                    fptr = fopen("accrec.dat","rb");
                    if (fptr == NULL)
                    {
                    printf("File Cant be read");
                    exit(1);
                    }

                    printf("Account No: %d\n",accrec.no);

                    while(fread(&accrec,sizeof(struct account),1,fptr)!=NULL);
                    {
                    printf("Initial Deposit is %0.2f\n",accrec.id);
                    }       
                    printf("%d\n",&accrec.id);

                    break;

                case 2:
                    float dv;
                    printf("DEPOSIT\n");
                    printf("Current Balance:");
                    fptr = fopen("accrec.dat","rb");
                    if (fptr == NULL)
                    {
                    printf("File Cant be read");
                    exit(1);
                    }
                    while(fread(&accrec,sizeof(struct account),1,fptr)!=NULL);
                    {
                    printf("%0.2f\n",accrec.id);
                    }
                    printf("Enter amount to deposit:\n");
                    printf("Deposit Value:");
                    scanf("%0.2f",&dv);
                    accrec.id = accrec.id + dv; 
                    fwrite(&accrec,sizeof(struct account),1,fptr);  
                    fclose(fptr);
                    break;

                case 3:
                    float wv;
                    printf("WITHDRAWAL\n");
                    printf("Current Balance:");
                    fptr = fopen("accrec.dat","rb+");
                    if (fptr == NULL)
                    {
                    printf("File Cant be read");
                    exit(1);
                    }
                    while(fread(&accrec,sizeof(struct account),1,fptr)!=NULL);
                    {
                    printf("%0.2f\n",accrec.id);
                    }
                    printf("Enter amount to withdraw:\n");
                    printf("Withdrawal Value:");
                    scanf("%0.2f",wv);
                    accrec.id = accrec.id - wv; 
                    fwrite(&accrec,sizeof(struct account),1,fptr);  
                    fclose(fptr);

                    break;

                case 4:
                    printf("thank you for your patronage \n");
                    return 0;
                    break;

                default:
                    printf("error 404");
                    break;
                }
            }
        }
        break;

        case 'C':
            printf("Thank you! Have a good day");
            return 0;
            break;

        case 'D':
            fptr = fopen("accrec.dat","rb");
            if (fptr == NULL)
            {
                printf("File Cant be read");
                exit(1);
            }

            printf("Data From file\n");

            while(fread(&accrec,sizeof(struct account),1,fptr)!=NULL)
            printf("\n acc.no is %d\n acc.name is %s\n PIN %d\n Initial Deposit is %0.2f\n ",accrec.no,accrec.name,accrec.pin,accrec.id);
            fclose(fptr);
            getch();

            break;

        default:
            printf("invalid input! please select form the options given\n");

    }

} 

my structure

    struct account
    {
        int no;
        char name[100];
        int pin;
        float id;
    }; 

code for finding a match of account number in my file (the part i'm having trouble with)

    fptr = fopen("accrec.dat","rb");
    int choice;
    int accno = 0;
    printf("Enter Your Account no.");
    scanf("%d",&accno);
    while (fread(&accrec,sizeof(account),1,fptr)!=NULL)
    {
        if (accno == accrec.no)
        {
            printf("\tWelcome to Banking System\n");
            printf("1.Balance Inquiry\n");
            printf("2.Deposit\n");
            printf("3.Withdrawal\n");
            printf("4.quit\n");

            scanf("%d",&choice);
        }
        else 
        {
            printf("account doesn't exist\n");
            exit(1);
        }   
    }

    fclose(fptr);

The expected output is that my conditional statement works properly and reads my whole file for matches.

try to change the exit(1) to continue; , the exit(1) will make your program quit the while loop and exit the program immediately whenever accno != accrec.no .

in fact, you don't need the else statement inside the while loop, you should put it outside the while loop. for example:

scanf("%d",&accno);
boolean found = false;
while (fread(&accrec,sizeof(account),1,fptr)!=NULL)
{
    if (accno == accrec.no)
    {
        found = true;
        scanf("%d",&acpin);*/
        printf("\tWelcome to Banking System\n");
        printf("1.Balance Inquiry\n");
        printf("2.Deposit\n");
        printf("3.Withdrawal\n");
        printf("4.quit\n");

        scanf("%d",&choice);
    }
}
if (!found) {
    printf("account doesn't exist\n");
}

fclose(fptr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM