Thread: Help almost there!

  1. #1
    Registered User C-learner's Avatar
    Join Date
    Feb 2014
    Posts
    32

    Post Help almost there!

    I have been working on the bank account program
    Now I am 98% of the way I am just experiencing one problem: the software is reading a structure from a .txt file. I CANNOT POST MY CODES HERE(cant say why) but anyways my program does not read my structs after it has been closed and re-opened, it only reads from the file after I create an account (without closing the program) and select from the main menu an option to modify or read the info from the .txt file...
    In my main there is just a main menu and a password, all the codes associated with files are located in my functions below main. Am I supposed to open the files from main also? I am confused, here is a sample code from one of my functions
    By the way, I open each account created using a in one function and the rest of the functions just work on fetching the information and modifying it using r+

    Code:
    void make_withdrawal()  //function make a withdrawal from an account
    {
        system("cls");
        display_logo();
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
        printf("\n\nWITHDRAWAL PROCESSING IS DONE HERE\n\n");
        printf("PRESS ENTER TO CONTINUE");
        getch();
        system("cls");
        int id=0;
        float withdraw=0;
        if((acc=fopen("Active Accounts.txt","r+"))==NULL)
        {
            printf("\nERROR OPENING FILE.");
            getch();
            return;
        }
        printf("Please enter your account number\n");
        scanf("%d",&id);
        rewind(acc);
        fseek(acc,(id -1) *sizeof (Accounts), SEEK_SET);
        fread(&students, sizeof (Accounts), 1, acc);
        if(id!=students.account_num)
        {
            printf("ACCOUNT WAS NOT FOUND\n");
            printf("Press enter to return to the main menu");
            getch();
            display_main_menu();
        }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    I CANNOT POST MY CODES HERE(cant say why)
    You can't say why...? I'm guessing the code must be for MI6 or the NSA then.

    There are only two things that I can really tell you from this.
    One is that you shouldn't be using global variables. Especially the way that you're using them is going to cause many headaches. It pains me to see someone using global variables when their functions take no arguments and return void .
    The other is that
    Code:
    fread(&students, sizeof (Accounts), 1, acc);
    is probably a bad idea. It will probably work on your own machine, but it's not really very portable because of struct packing and different word sizes of different machines. It may not be a problem now but you should create a function to individually read and write the fields of an Accounts structure.

  3. #3
    Registered User C-learner's Avatar
    Join Date
    Feb 2014
    Posts
    32
    Can you give me an example of your suggestion? a function that individually read and write each field and how would i use it

  4. #4
    Registered User C-learner's Avatar
    Join Date
    Feb 2014
    Posts
    32
    I could send you my whole program in pm or an email or something and let u see it

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Cross-posted here -> c - NOT reading from .txt - Stack Overflow

    I wonder if this is it?
    [C] Bankaccount(feb 16,2014) - Pastebin.com

    > I CANNOT POST MY CODES HERE(cant say why)
    Either you don't want your code to be found by your class-mates (which isn't a problem, since you can always demonstrate "I published first"),
    or
    - you're trying to hide that you found the code elsewhere on the net
    - the terms of the assignment is 'closed book', and you're not allowed to find external help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User C-learner's Avatar
    Join Date
    Feb 2014
    Posts
    32
    Thanks a lot for pointing to my stuff salem, i made like 15 updates(all different pastes) but now i created an account there where i can make my updates without creating new pastes
    This is when i first started it...i went to stackoverflow and posted a question
    c - Reading specified lines below a found string - Stack Overflow
    and now that im almost done i came here too to put an end to this little difficulty im having
    those are my codes all the same can you help me? it reads from the file and perform every operation while the program creates a new account but after it closes it doesnt find the ID number from the file and pbbbt im screwed
    Last edited by C-learner; 02-24-2014 at 12:14 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You start with a new simple program, something like this.
    Code:
    struct rec {
      int id;
      char name[10];
    };
    
    int main ( ) {
      struct rec a;
      a.id = 1;
      strcpy(a.name, "hello");
      FILE *fp = fopen("file","wb");
      fwrite(&a,sizeof(a),1,fp);
      fclose(fp);
      // now try and read it
    }
    Create a new simple project to explore each new issue, then you don't have to worry about posting 100's of lines of code which don't relate to the current problem.

    FWIW, from your pastbin code, you have multiple scanf issues as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User C-learner's Avatar
    Join Date
    Feb 2014
    Posts
    32
    So i gotta write the block of data first using fwrite then create a whole other block of code using fread in-order for me to read from the file structure by structure?

  9. #9
    Registered User C-learner's Avatar
    Join Date
    Feb 2014
    Posts
    32
    is there anyone who can take a look at my whole code? because ive been at it for days and still cant fix it....i took care of the scanf issues and i didnt use fwrite to write data to the file i used fprintf

  10. #10
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Post your entire code here with a detailed explanation of the problems you are having with it and with any compiler warnings/errors that you are encountering.

  11. #11
    Registered User C-learner's Avatar
    Join Date
    Feb 2014
    Posts
    32
    Ok the problem is when i create an account(which is done in a function) i write all the information in the account to a .txt file then try to read a specific database from the file upon entering an account number, i basically read from the file, structure by structure and im not getting any compiling errors...btw the program fetch the desired account and everything i want it to do as long as the program isnt closed, but if it is closed and reopend it doesnt read the data from the file after i enter the account number.It only does what its supposed to do while the program is open and a new account has been created, basically if the create_account() function has not been used sucessfully the others wont be needed they are all dependent on an already created account..here is the whole code try not to be irritated by the indentation(my bad) i'll fix those up when im finish with the program i just wanna get it done right now...If you can help DeadPlanet i'd be very greatful thank you. Everything associated with reading/writing from the file is inside the functions void modify_account(void); ,void make_withdrawal(void); ,void make_deposit(void); and the file is created at first from the function void create_account(void); , if this function isnt used sucessfully then the whole program is useless

    Code:
    #include <stdio.h>
    #include <time.h>               //HEADER FILES
    #include <conio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <Windows.h>
    
     void display_main_menu(void);
     void create_account(void);
     void modify_account(void);      //FUNCTION DECLARATIONS
     void make_withdrawal(void);
     void make_deposit(void);
     void display_logo(void);
     void exit_program(void);
    
    
     FILE *acc;                  //FILE DECLARATION
    
    typedef struct               //STRUCTURE
    {
        char firstname[200];
        char lastname[200];
        char password[200];
        char parish[200];
        int payment_type;
        int account_num;
        int age_calculation;
        int yob;
        float acc_bal;
        float acc_fee;
        float acc_change;
        float change_from_fee;
    }Accounts;
    
    Accounts students={"","","","",0,0,0,0,0,0,0,0}; //INITIALIZING THE STRUCTURE'S ELEMENTS
    
    
    main()                  //MAIN STARTS HERE
    {
        SetConsoleTitle("Program done by Nicholas Cameron...Banking Agency");
        int program_pass=2014; //Password needed to access the program
        int pass=0;
        char option;
    
         display_logo();
        do  //CODES FOR ENTERING THE PASSWORD
         {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
                     printf("Enter password ");
                     scanf("%d", &pass);
                     fflush(stdin);
                     if(pass!=program_pass)
                     printf("\nAccess Denied! \nPassword incorrect. Please try again ");
         }
                    while (pass!=program_pass);
    
    
                    while (pass==program_pass)  // while loop for when password entered is correct
                    {
                        printf(" \nAccess Granted!\n");
                        printf("\n\nPress enter to continue");
                                getch();
                                system("cls");
                                display_main_menu();
                    }
    
    system("pause");
    exit(1);
    }//MAIN ENDS HERE
    
    
    
    
    //FUNCTION DEFINITIONS
    void display_main_menu(void)     //function display main menu
    {
        main_menu:        // goto LABEL FOR MAIN MENU
        system("cls");
        char activity_selection;
        display_logo();
    
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
    printf("Please select from the list below the action you wish to carry out\n\n\t");
    
    printf("     || A. Create an account -(Preferably for new users) ||\n\t");
    printf("     || B. Make a deposit    -(for members only)         ||\n\t");
    printf("     || C. Make a withdrawal -(for members only)         ||\n\t");
    printf("     || D. Modify an account -(for members only)         ||\n\t");
    printf("     || E. Exit              -(for all users)            ||\n\n");
    
    printf("Enter the letter that corresponds with each option\n");
    scanf("%s",&activity_selection);
    system("cls");
    switch(activity_selection)
    {
        case 'A': case'a':
        create_account();
        break;
    
        case 'B': case 'b': //DEPOSIT PROCESS
        make_deposit();
        break;
    
        case 'C': case 'c': //WITHDRAWAL PROCESS
        make_withdrawal();
        break;
    
    
        case 'D': case 'd': //Modifying PROCESS
            modify_account();
        break;
    
        case 'E': case 'e': //Exit process
            exit_program();
        break;
    
        default :
            system("cls");
        printf("The choice you entered appears to be invalid\n");
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
        printf("\n\t      ||                A.<MAIN MENU>            ||\n\t");
        printf("      ||                B.<EXIT>                 ||\n\n");
    
         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
        printf("Please enter the corresponding letter for the task you wish to execute\n\n\n");
        scanf("%s",&activity_selection);
    
        if(activity_selection=='A'||activity_selection=='a')
        {
             goto main_menu; //THIS MOVES TO THE MAIN MENU IF 'A' OR 'a' IS  ENTERED.DIDNT CALL THE display_main_menu() FUNCTION SINCE WE'RE ALREADY  INSIDE IT
        }
        else if(activity_selection=='B'||activity_selection=='b')
        {
            exit(1);  //Exits the program if exit is selected
        }
        else
        {
            printf("Another invalid option entered.Thank you for trying to use this program\n");
            printf("Press any key to exit\n");
            getch();
            exit(1);
        }
        break;
    }
    }
    
     void create_account(void)  //function create account
     {
         int current_year=2014;
         char decision,new_choice,payment_plan;
         int r;
    
            srand((unsigned)time(0));//UNSIGNED PREVENTS RANDOM NEGATIVE NUMBERS FROM GENERATING
            r=(rand()*999999999);
            r=(rand()%9999999999999)+1;
            students.account_num=r;
    
         decision='y';
    
         do
    {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
    printf("This is a list of our current savings plan system which you will choose from\n");
    printf("\n\t      ||                A. One year                    ||\n\t");
    printf("      ||                B. Two years                   ||\n\t");
    printf("      ||                C. Three years                 ||\n\t");
    printf("      ||                D. Four years                  ||\n\t");
    printf("      ||                E. Five years                  ||\n\n");
    
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
    printf("Please note...you should enter the corresponding number for the desired savings plan\n");
    printf("You may now enter the savings plan desired\n");
    scanf("%s",&payment_plan);
    fflush(stdin);
    switch(payment_plan)
    {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
    case 'A': case'a':
        printf("You have decided to use a savings plan of 1 year\n");
        students.payment_type=1;
        break;
    
    case 'B': case'b':
        printf("You have decided to use a savings plan of 2 years\n");
        students.payment_type=2;
        break;
    
    case 'C': case'c':
        printf("You have decided to use a savings plan of 3 years\n");
        students.payment_type=3;
        break;
    
    
    case 'D': case'd':
        printf("You have decided to use a savings plan of 4 years\n");
        students.payment_type=4;
        break;
    
    
    case 'E': case'e':
        printf("You have decided to use a savings plan of 5 years\n");
        students.payment_type=5;
        break;
    
    default:
        system("cls");
        printf("Invalid option entered\n");
        printf("Press any key to continue\n");
        getch();
        system("cls");
        display_main_menu();
        break;
    }
    printf("A minimum opening fee of $100 is required\n");
    printf("Enter the minimum opening fee\n");
    scanf("%f",&students.acc_fee);
    
    if(students.acc_fee<100)
    {
        printf("The fee entered is too low\n");
        printf("Press enter to return to the main menu\n");
        getch();
        display_main_menu();
    }
    else if(students.acc_fee<=100)
    {
        printf("Your account balance is $%.2f.It will be saved in your creating account\n",students.acc_fee);
        printf("Press enter to continue\n");
        getch();
    }
    else if(students.acc_fee>100)
    {
        students.acc_change=students.acc_fee-100;
        printf("You have an existing change of $%.2f",students.acc_change);
        printf("\nIf you wish to retrieve it, a withdrawal must be made after the account has been created\n");
        printf("Press enter to continue\n");
        getch();
    }
    
    students.change_from_fee=students.acc_change;
    students.acc_bal=students.acc_fee;
    system("cls");
    
    
    printf("it is now time to create an account please enter the information required\n\n");
    printf("enter your firstname\n");
    scanf("%s",&students.firstname);
    printf("enter your lastname\n");
    scanf("%s",&students.lastname);
    printf("Enter the name of your parish\n");
    scanf("%s",&students.parish);
    
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
    printf("\nTo  sign up with us, ages must be 12 or older but less than 20 years of  age\n"); //In most high schools ages range from approximately 12-20  years
    printf("You may now enter your year of birth\n");
    scanf("%d",&students.yob);
    
    students.age_calculation = current_year-students.yob;
    printf("Your age is %d ",students.age_calculation);
    if(students.age_calculation >= 12 && students.age_calculation<=20)
    {
        printf("Which is an acceptable age\n");
        printf("Please enter a password\n");
        scanf("%s",&students.password);
        system("cls");
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
        printf("Please Hold, Creating Account...");
        /*Sleep(3000);
        system("cls");
        printf("Accepting Data....");
        Sleep(300);
        system("cls");
        printf("Enabling Data De-Fragmentation.....");
        Sleep(100);
        system("cls");
        printf("Organizing File Memory...");
        Sleep(300);
        system("cls");
        printf("Initiating Data Logs...");
        Sleep(200);
        system("cls");
        printf("Data Checking...");
        Sleep(200);
        system("cls");
        printf("Initializing Validity Check...");
        Sleep(100);
        system("cls");
        printf("Locating Account Addresses..");
        Sleep(200);
        system("cls");
        printf("Initiating Data Storage...");
        Sleep(300);
        system("cls");
        printf("Finalizing DataBase Entry...");
        Sleep(2000);*/
        system("cls");
    
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
        printf("Your account has been successfully created!\n");
        printf("You have been assigned the account number :");
        printf("%d",students.account_num);
        printf("\n\nKeep your account number safe for it will be needed for account access\n\n");
        printf("Your information is as follows:\n");
    
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
        acc=fopen("Active Accounts.txt","a+");//   FILE HOLDING THE SUCCESSFULLY CREATED ACCOUNTS
        if (acc==NULL)
        {
        printf("ERROR OPENING FILE!\n");
        printf("Press enter to continue");
        getch();
        display_main_menu();
        }
        fprintf(acc,"%d",students.account_num);
        fprintf(acc,"%s",students.firstname);
        fprintf(acc,"%s",students.lastname);
        fprintf(acc,"%s",students.parish);
        fprintf(acc,"%d",students.yob);
        fprintf(acc,"%d",students.age_calculation);
        fprintf(acc,"%d",students.payment_type);
        fprintf(acc,"%s",students.password);
        fprintf(acc,"%.2f",students.acc_bal);
        fclose(acc);
    
        printf("================================================================================");
        printf("                 >> Account #       : %d\n",students.account_num);
        printf("                 >> First Name      : %s\n",students.firstname);
        printf("                 >> Last Name       : %s\n",students.lastname);
        printf("                 >> Parish          : %s\n",students.parish);
        printf("                 >> Year of Birth   : %d\n",students.yob);
        printf("                 >> Age             : %d\n",students.age_calculation);
        printf("                 >> Savings Period  : %d year(s)\n",students.payment_type);
        printf("                 >> Password        : %s\n",students.password);
        printf("                 >> Account balance : $%.2f\n",students.acc_bal);
        printf("================================================================================\n");
    
    }
    else if(students.age_calculation >20)
    {
           printf("Were very sorry but you are too old to create an account with us\n\n");
    }
    
    else if(students.age_calculation <12)
        {
        printf("You are too young to create an account here sorry\n\n");
        }
        printf("If a mistake was made you can repeat the procedure of creating another savings\n");
        printf("account\n\n");
        printf("Do you wish to repeat? <y or n>\n\n");
        scanf("%s",&decision);
        system("cls");
    
    
    }
    while(decision=='Y'||decision=='y');
    if(decision=='N'||decision=='n')
    {
        printf("You are now a member of this banking agency!\n");
           SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
        main_or_exit:
        printf("\n\t      ||                A.<MAIN MENU>            ||\n\t");
        printf("      ||                B.<EXIT>                 ||\n\n");
        printf("Please select the desired option <a or b>\n\n");
        scanf("%s",&new_choice);
    
        if(new_choice=='A'||new_choice=='a')
          {
             display_main_menu();
          }
    
        else if(new_choice=='B'||new_choice=='b')
        {
            exit(1);
        }
        else
        {
            system("cls");
            printf("Invalid option entered\n");
            printf("Please read carefully and re-enter the option desired\n");
            goto main_or_exit;
        }
    }
     }
    
    
     void make_withdrawal(void)  //function make a withdrawal from an account
     {
        system("cls");
        display_logo();
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
        printf("\n\nWITHDRAWAL PROCESSING IS DONE HERE\n\n");
        printf("PRESS ENTER TO CONTINUE");
        getch();
        system("cls");
        int id=0;
        float withdraw=0;
        if((acc=fopen("Active Accounts.txt","r"))==NULL)
             {
                 printf("\nERROR OPENING FILE.");
                printf("Press enter to continue");
                getch();
                display_main_menu();
             }
                 printf("Please enter your account number\n");
                 scanf("%d",&id);
                 rewind(acc);
                 fseek(acc,(id -1) *sizeof (Accounts), SEEK_SET);
                 fread(&students, sizeof(Accounts), 1, acc);
        if(id!=students.account_num)
        {
            printf("ACCOUNT WAS NOT FOUND\n");
            printf("Press enter to return to the main menu");
            getch();
            display_main_menu();
        }
        else
        {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
        printf("ACCOUNT FOUND!");
        printf("\n================================================================================");
        printf("                 >> Account #       : %d\n",students.account_num);
        printf("                 >> First Name      : %s\n",students.firstname);
        printf("                 >> Last Name       : %s\n",students.lastname);
        printf("                 >> Parish          : %s\n",students.parish);
        printf("                 >> Year of Birth   : %d\n",students.yob);
        printf("                 >> Age             : %d\n",students.age_calculation);
        printf("                 >> Savings Period  : %d year(s)\n",students.payment_type);
        printf("                 >> Password        : %s\n",students.password);
        printf("                 >> Account balance : $%.2f\n",students.acc_bal);
        printf("================================================================================\n");
         }
     fflush(stdin);
        withdrawal:
        printf("Enter the amount of money you wish to withdraw\n");
        scanf("%f",&withdraw);
        if(withdraw>students.acc_bal)
        {
            printf("The amount you entered is too high it must be less than or equal to your balance\n");
            printf("which is $%.2f\n",students.acc_bal);
            printf("Press enter to re-enter the withdrawal value\n");
            getch();
            goto withdrawal;
        }
        else if(withdraw<=students.acc_bal && withdraw>0)
        {
            students.acc_bal=students.acc_bal-withdraw;
            printf("WITHDRAWAL WAS SUCCESSFUL\n");
            printf("YOUR ACCOUNT BALANCE IS $%.2f",students.acc_bal);
            printf("Press enter to continue\n");
            getch();
            display_main_menu();
        }
        else if(withdraw<=0)
        {
            printf("The amount you entered is too low\n");
            printf("Press enter to re-enter the withdrawal value\n");
            getch();
            goto withdrawal;
        }
            fclose(acc);
     }
    
    
     void make_deposit(void)  //function make deposit to an account
     {
        system("cls");
        display_logo();
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
        printf("\n\nDEPOSIT PROCESSING IS DONE HERE\n\n");
        printf("PRESS ENTER TO CONTINUE");
        getch();
        system("cls");
            int id;
            float deposit=0;
             if((acc=fopen("Active Accounts.txt","r"))==NULL)
             {
                 printf("\nERROR OPENING FILE.");
                printf("Press enter to continue");
                getch();
                display_main_menu();
             }
                 printf("Please enter your account number\n");
                 scanf("%d",&id);
                 rewind(acc);
                 fseek(acc,(id -1) *sizeof (Accounts), SEEK_SET);
                 fread(&students, sizeof (Accounts), 1, acc);
    
        if(id!=students.account_num)
        {
            printf("ACCOUNT WAS NOT FOUND\n");
            printf("Press enter to return to the main menu\n");
            getch();
            display_main_menu();
        }
        else
        {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
        printf("ACCOUNT FOUND!");
        printf("\n================================================================================");
        printf("                 >> Account #       : %d\n",students.account_num);
        printf("                 >> First Name      : %s\n",students.firstname);
        printf("                 >> Last Name       : %s\n",students.lastname);
        printf("                 >> Parish          : %s\n",students.parish);
        printf("                 >> Year of Birth   : %d\n",students.yob);
        printf("                 >> Age             : %d\n",students.age_calculation);
        printf("                 >> Savings Period  : %d year(s)\n",students.payment_type);
        printf("                 >> Password        : %s\n",students.password);
        printf("                 >> Account balance : $%.2f\n",students.acc_bal);
        printf("================================================================================\n");
        }
        fflush(stdin);
        deposits:
        printf("Enter the amount of money you wish to deposit\n");
        scanf("%f",&deposit);
        if(deposit<=0)
        {
            printf("Were very sorry but you cannot deposit $0 or less\n");
            printf("Press enter to re-enter the deposit amount\n");
            getch();
            goto deposits;
        }
        else
        {
            students.acc_bal=students.acc_bal+deposit;
            printf("DEPOSIT WAS SUCCESSFUL\n");
            printf("YOUR ACCOUNT BALANCE IS $%.2f\n",students.acc_bal);
            printf("Press enter to continue");
            getch();
            display_main_menu();
        }
        fclose(acc);
     }
    
     void modify_account(void) //function modify an account
     {
         system("cls");
         display_logo();
         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
         int id;
         printf("\n\nMODIFYING ACCOUNTS IS DONE HERE\n\n");
         printf("PRESS ENTER TO CONTINUE");
        getch();
        system("cls");
            char an='y';
             if((acc=fopen("Active Accounts.txt","r"))==NULL)
             {
                 printf("\nERROR OPENING FILE.");
                printf("Press enter to continue");
                getch();
                display_main_menu();
             }
                 printf("Please enter your account number\n");
                 scanf("%d",&id);
                 rewind(acc);
                 fseek(acc,(id -1) *sizeof (Accounts), SEEK_SET);
                 fread(&students, sizeof (Accounts), 1, acc);
    
        if(id!=students.account_num)
        {
            printf("ACCOUNT WAS NOT FOUND\n");
            printf("Press enter to return to the main menu\n");
            getch();
            display_main_menu();
        }
        else
        {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
        printf("THIS IS YOUR CURRENT ACCOUNT INFORMATION!");
        printf("\n================================================================================");
        printf("                 >> Account #       : %d\n",students.account_num);
        printf("                 >> First Name      : %s\n",students.firstname);
        printf("                 >> Last Name       : %s\n",students.lastname);
        printf("                 >> Parish          : %s\n",students.parish);
        printf("                 >> Year of Birth   : %d\n",students.yob);
        printf("                 >> Age             : %d\n",students.age_calculation);
        printf("                 >> Savings Period  : %d year(s)\n",students.payment_type);
        printf("                 >> Password        : %s\n",students.password);
        printf("                 >> Account balance : $%.2f\n",students.acc_bal);
        printf("================================================================================\n");
        }
        fflush(stdin);
        printf("Members can modify only the following fields:\n");
        printf("First Name\n");
        printf("Last Name\n");
        printf("Parish\n");
        printf("Password\n");
        an_option:
        printf("do you wish to proceed with modifications? <y or n>\n");
        scanf("%s",&an);
        if(an=='Y'||an=='y')
        {
            printf("Enter the first name\n");
            scanf("%s",students.firstname);
            printf("Enter the new last name\n");
            scanf("%s",students.lastname);
            printf("Enter the name of your new parish\n");
            scanf("%s",students.parish);
            printf("Enter the new password\n");
            scanf("%s",students.password);
            fflush(stdin);
            printf("Press enter to continue\n");
            getch();
            system("cls");
            printf("Your Account has been successfully modified.Your new Information is as follows\n\n");
            printf("\n================================================================================");
        printf("                 >> Account #       : %d\n",students.account_num);
        printf("                 >> First Name      : %s\n",students.firstname);
        printf("                 >> Last Name       : %s\n",students.lastname);
        printf("                 >> Parish          : %s\n",students.parish);
        printf("                 >> Year of Birth   : %d\n",students.yob);
        printf("                 >> Age             : %d\n",students.age_calculation);
        printf("                 >> Savings Period  : %d year(s)\n",students.payment_type);
        printf("                 >> Password        : %s\n",students.password);
        printf("                 >> Account balance : $%.2f\n",students.acc_bal);
        printf("================================================================================\n");
        fflush(stdin);
        printf("Press enter to return to the main menu\n");
        getch();
        display_main_menu();
        }
        else if(an=='N'||an=='n')
        {
            printf("As you wish....Press enter to return to the main menu\n");
            getch();
            display_main_menu();
        }
        else
        {
            printf("Invalid answer.Press enter to re-enter the option\n");
            getch();
            goto an_option;
        }
    
        fclose(acc);
    }
    
    
    void display_logo(void)//function to display logo
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
            printf("\t     ===================================================           \n\t");
        printf("     @@@@@@@@@@      THE OLD HARBOUR    @@@@@@@@@@@@@@@@           \n\t");
        printf("     @@@@@@@@@@    HIGH SCHOOL BANKING  @@@@@@@@@@@@@@@@           \n\t");
        printf("     @@@@@@@@@@          SYSTEM         @@@@@@@@@@@@@@@@           \n\t");
        printf("     ===================================================           \n\n");
    
    }
    
    void exit_program(void)
    {
        system("cls");
        display_logo();
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
        printf("Thank you for using this program\n");
        printf("Press any key to exit\n");
        getch();
        exit(1);
    }
    Last edited by C-learner; 02-25-2014 at 11:20 PM.

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    main()
    {
      ....
      exit(1);
    }//MAIN ENDS HERE
    This is a bad way of declaring main. You should be doing it like this:

    Code:
    int main(void)
    {
        ....
        return 0;
    }
    see FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

    This code is also no good:
    Code:
    fflush(stdin);
    FAQ - Cprogramming.com


    Using headers specific to windows is also frowned upon (windows.h, conio.h)

    "goto" statements should also be avoided.
    Fact - Beethoven wrote his first symphony in C

  13. #13
    Registered User C-learner's Avatar
    Join Date
    Feb 2014
    Posts
    32
    ok Click_here i made the changes and got rid of the junk code and checking out the info you pointed to

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Compile with warnings turn on; fix the problems.

    Edit: Logic/Design/Implementation error found: I call it circle logic error, no idea what real name is.
    display_main_menu() calls function; then function calls display_main_menu.

    Edit2: The way I did these type problems was splitting your display_main_menu function into two parts.
    1. display_main_menu that only does display.
    2. action_main_menu function that has the switch code in it; normally this is the only function that calls display_main_menu.
    (I sometimes called display_main_menu once before calling action_main_menu.)

    Tim S.

    Code:
    mingw32-gcc.exe -Wall -O2 -Wmissing-include-dirs -Wfatal-errors -Wno-unused-local-typedefs -Wno-attributes  -c E:\CB-Test_projects\bank\main.c -o obj\Release\main.o
    E:\CB-Test_projects\bank\main.c:38:1: warning: return type defaults to 'int' [-Wreturn-type]
     main()                  //MAIN STARTS HERE
     ^
    E:\CB-Test_projects\bank\main.c: In function 'main':
    E:\CB-Test_projects\bank\main.c:43:10: warning: unused variable 'option' [-Wunused-variable]
         char option;
              ^
    E:\CB-Test_projects\bank\main.c: In function 'create_account':
    E:\CB-Test_projects\bank\main.c:247:1: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[200]' [-Wformat=]
     scanf("%s",&students.firstname);
     ^
    E:\CB-Test_projects\bank\main.c:249:1: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[200]' [-Wformat=]
     scanf("%s",&students.lastname);
     ^
    E:\CB-Test_projects\bank\main.c:251:1: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[200]' [-Wformat=]
     scanf("%s",&students.parish);
     ^
    E:\CB-Test_projects\bank\main.c:264:5: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[200]' [-Wformat=]
         scanf("%s",&students.password);
         ^
    Last edited by stahta01; 02-26-2014 at 08:36 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Edit: Logic error found: I call it circle logic error, no idea what real name is.
    > display_main_menu() calls function; then function calls display_main_menu.
    I call it pointless mutual recursion.

    Functions like 'modify_account' should do exactly that - modify an account - no more, no less.

    The Q/A section at the end, and the decision of what to call next should be in another function. Preferably one which contains
    - a loop
    - a call to the function to display the menu
    - perhaps a switch statement containing calls to modify_account etc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Tags for this Thread