Thread: Bank account management project help

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    5

    Bank account management project help

    Hi. This is my first C project (I self taught myself C) and have started a basic project where users can create, update, view and manage, check and delete bank accounts. It's all terminal/command line based with no fancy GUI or graphics involved.

    Basically, it looks like this:
    Bank account management project help-selection_021-png

    A user will select a number to either log in or create a bank account, then they can log into that bank account. Heres the problem. I have this welcome page (when user starts the application):
    Bank account management project help-outline1-jpg

    When, for example, the user clicks one, they will be brought to this page:
    Bank account management project help-outline2-jpg

    However, when I tried the above, the welcome text won;t go away. I have two functions: Welcome() and Login(). The Welcome() function just displays the welcome page as shown above, same with the Login() function. However, when I try an if statement to see if the user either pressed 1 or 2, say they pressed one, the welcome function won't go away, It will display the welcome page and the login page together. What I want is, say when user presses 1, it will display only the login page and not the welcome page anymore. Similarily, if user presses 3 in the login page, it will send the user back to the welcome page and clear the login page. How would I do this? I had in mind, an array of function pointers, say if user presses 1, execute the Login() pointer and set the Welcome() pointer to NULL. so far, this is my code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    void Login()
    {
       char uname[20];
       char pwd[20];
       int accNum;
    
    
       printf("\n\tLogin to see your Bank Account\n");
     
       printf("\t  Enter Account Username: ");
       scanf("%[^\n]%*c", uname);
       printf("\n\t  Enter Password: ");
       scanf("%[^\n]%*c", pwd);
       printf("\n\t  Enter Account Num (required): ");
       scanf("%d", &accNum);
    
    
       printf("\n\tYou entered '%s' for username", uname);
       printf("\n\tYou entered '%s' for password", pwd);
       printf("\n\tYou entered '%d' for account number", accNum);
    }
    
    
    void Welcome()
    {
       printf("\n\tWelcome to the Bank Management System\n");
       
       printf("\t  1. Login to your account\n");
       printf("\t  2. Create new account\n\n");
    }
    
    
    
    
    int main()
    {
       void(*welcome_ptr)() = &Welcome; //Declare pointer to Welcome()
       void(*login_ptr)() = &Login; //Declare pointer to Login()
       
       (*welcome_ptr)();
    
    
       int choice;
       printf("\t  Enter your choice: ");
       scanf("%d", &choice);
     
       if(choice == 1)
       {   
          (*login_ptr)();
       }   
    
    
       return 0;
    }
    Last edited by RicsterB; 05-22-2020 at 05:02 PM.

  2. #2
    Registered User
    Join Date
    May 2020
    Posts
    5
    Instead of saying "page", I could have said Welcome menu and Login menu.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,068
    There's no such thing as a page in simple console programming.
    It's all just one continuous scrolling stream of text.

    You can get the 'page' feel by finding out whatever 'clear screen' option there is for your particular OS/Compiler/Terminal combination.

    Your function pointers are a needless complication.


    Oh, and dark blue on black is a horrible colour scheme.
    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.

  4. #4
    Registered User
    Join Date
    May 2020
    Posts
    5
    Quote Originally Posted by Salem View Post
    There's no such thing as a page in simple console programming.
    It's all just one continuous scrolling stream of text.

    You can get the 'page' feel by finding out whatever 'clear screen' option there is for your particular OS/Compiler/Terminal combination.

    Your function pointers are a needless complication.


    Oh, and dark blue on black is a horrible colour scheme.
    So theres no need for the pointers? Just execute the function as is, and clear the console using: system("clear")
    BTW, i'm building this app on Linux.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,068
    Pretty much.

    printf("\033[2J"); // should also clear the screen.
    printf("\033[H"); // move cursor home

    ANSI escape code - Wikipedia
    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
    Join Date
    May 2020
    Posts
    5
    I got another question, when I try to execute the following:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    void Login()
    {
       char uname[20];
       char pwd[20];
       int accNum;
    
    
       printf("\n\tLogin to see your Bank Account\n");
     
       printf("\t  Enter Account Username: ");
       scanf("%[^\n]%*c", uname);
       printf("\n\t  Enter Password: ");
       scanf("%[^\n]%*c", pwd);
       printf("\n\t  Enter Account Num (required): ");
       scanf("%d", &accNum);
    
    
       printf("\n\tYou entered '%s' for username", uname);
       printf("\n\tYou entered '%s' for password", pwd);
       printf("\n\tYou entered '%d' for account number", accNum);
    }
    
    
    
    
    int main()
    {
       printf("\033[2J"); // clears the terminal
       printf("\033[H"); // move cursor home
    
    
       printf("\n\tWelcome to the Bank Management System\n");
       printf("\t  1. Login to your account\n");
       printf("\t  2. Create new account\n\n");
    
    
       int choice;
       printf("\n\n\tEnter your choice: ");
       scanf("%d", &choice);
    
    
       if(choice == 1)
       {   
          printf("\033[2J"); // should also clear the screen.
          printf("\033[H"); // move cursor home
      
          Login();
       }   
       else if(choice != 1 || choice != 2)
       {   
          printf("You must enter either 1 or 2");
       }   
    
    
       return 0;
    }
    I get the following:
    Bank account management project help-selection_003-png

    If I enter 1, I get the following:
    Bank account management project help-selection_004-png

    Bank account management project help-selection_007-png



    But consider the following code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    void Login()
    {
       char uname[20];
       char pwd[20];
       int accNum;
    
    
       printf("\n\tLogin to see your Bank Account\n");
     
       printf("\t  Enter Account Username: ");
       scanf("%[^\n]%*c", uname);
       printf("\n\t  Enter Password: ");
       scanf("%[^\n]%*c", pwd);
       printf("\n\t  Enter Account Num (required): ");
       scanf("%d", &accNum);
    
    
       printf("\n\tYou entered '%s' for username", uname);
       printf("\n\tYou entered '%s' for password", pwd);
       printf("\n\tYou entered '%d' for account number", accNum);
    }
    
    
    
    
    int main()
    {
       Login(); 
    
    
       return 0;
    }
    The I get the following:
    Bank account management project help-selection_005-png

    Bank account management project help-selection_006-png

    Why is it that when I try to execute the Login() function in an if statement, It skips the enter username and password part and does to the enter account num part?
    Last edited by RicsterB; 05-23-2020 at 02:56 PM. Reason: Adding another picture

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,068
    You have to think about what this leaves on the input stream
    > scanf("%d", &choice);

    To understand what this actually means
    > scanf("%[^\n]%*c", uname);

    The \n that the %d conversion leaves on the input stream is exactly what %[^\n] needs to complete sucesessfully without doing anything.
    It's the same problem as mixing scanf with fgets.

    For simple programs, an input flush can be achieved with
    Code:
    int ch;
    while ( (ch=getchar()) != EOF && ch != '\n' );
    But generally, I would favour separating input from conversion as follows.
    Code:
    char buff[BUFSIZ];
    fgets(buff, BUFSIZ, stdin );  // this returns a result that should be checked
    sscanf(buff, "%d", &choice);  // this returns a result that should be checked(*)
    If you always use fgets() to read a line of input, then users always have a consistent experience.
    All the ways the sscanf could fail to deal with the whole line properly don't affect future inputs.


    (*) One result that scanf won't check on any of the numeric conversions is overflow.
    If those are important, use strtol() strtoul() and strtod()
    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

Similar Threads

  1. Bank Account Problem
    By JayBlay77 in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2009, 08:41 AM
  2. Bank Account Program
    By ace96320 in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2008, 03:26 AM
  3. bank account program
    By JJH35 in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2006, 06:05 PM
  4. Bank Account
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-14-2003, 02:27 AM
  5. Simple Bank Account Uni Project Help!!!
    By griff in forum C Programming
    Replies: 3
    Last Post: 05-09-2003, 05:49 PM

Tags for this Thread