How can i loop the main function?

135 views Asked by At

EDITED I am doing a bank management system project, when i get to loop my main function the output is not what i expect. the code works fine and outputs the expected data when not looping but as soon as i get to the looping part things get mixed up. PS : this is my first post on stackoverflow so if you can help with telling me whats the correct way to ask questions here then ill be delighted

I have edited the code to the advice of someone who commented and repaired the code which works fine at any compiler but in VS Code it doesnt print as the other compilers

*VS Code* 
Enter Your Name : O

Enter Your Account Number : 1
------------------------------
          Main Menu
------------------------------
1) Deposit
2) Withdraw
3) Transfer
4) Account Details
5) Transaction Details
6) Exit

Please Select The Desired Service
1

Deposit
Please Enter the Amount To Be Deposited
10000
10000.000000 Have Been Deposited To Account 1
Balance : 10000.00-----
u

1) Deposit

*GDP Compiler*
Enter Your Name : O

Enter Your Account Number : 1
------------------------------
          Main Menu
------------------------------
1) Deposit
2) Withdraw
3) Transfer
4) Account Details
5) Transaction Details
6) Exit

Please Select The Desired Service
1
Deposit
Please Enter the Amount To Be Deposited
10000
10000.000000 Have Been Deposited To Account 1
Balance : 10000.00
------------------------------
          Main Menu
------------------------------
1) Deposit
2) Withdraw
3) Transfer
4) Account Details
5) Transaction Details
6) Exit

Please Select The Desired Service


*CODE*
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>

char client_name[20];
double deposit_amount=0;
int account_number;
double total_amount=0;
double withdraw_amount=0;
double transfer_amount=0;
int trans_acc_number;

void menu()
{
    printf("------------------------------\n");
    printf("          Main Menu\n");
    printf("------------------------------\n");
    printf("1) Deposit\n");
    printf("2) Withdraw\n");
    printf("3) Transfer\n");
    printf("4) Account Details\n");
    printf("5) Transaction Details\n");
    printf("6) Exit\n\n");
}
int deposit()
{
    time_t tm;
    time (&tm);
    FILE *ptr = fopen("Account.txt","a");
    printf("Deposit\n");
    printf("Please Enter the Amount To Be Deposited\n");
    scanf("%lf",&deposit_amount);
    total_amount=total_amount+deposit_amount;
    printf("%f Have Been Deposited To Account %d\n",total_amount,account_number);
    printf("Balance : %.2f",total_amount);
    fprintf(ptr,"£%.3f Have Been Deposited To Account %d\n",total_amount,account_number);
    fprintf(ptr,"Date & Time of Transaction is %s\n",ctime(&tm));
    fclose(ptr);
}
void withdraw()
{
    time_t tm;
    time (&tm);
    FILE *ptr =fopen("Account.txt","a");
    printf("Withddraw\n");
    printf("Please Enter the Amount To Be Withdrawn\n");
    scanf("%lf",&withdraw_amount);
    total_amount=total_amount-withdraw_amount;
    printf("£%f Have Been Withdrawn From Account %d\n",withdraw_amount,account_number);
    printf("Balance : %.3f\n",total_amount);
    fprintf(ptr,"£%.3f Have Been Withdrawn from Account %d\n",total_amount,account_number);
    fprintf(ptr,"Date & Time of Transaction is %s\n",ctime(&tm));
    fclose(ptr);
}
void transfer()
{
    FILE *ptr =fopen("Account.txt","a");
    printf("Transfer\n");
    printf("Enter Amount To Be Transfered : ");
    scanf("%lf",&transfer_amount);
    printf("\nEnter The Account Number : ");
    scanf("%d",&trans_acc_number);
    if (total_amount<transfer_amount)
    {
        printf("Insufficient Balance");
    }
    else 
    {
        total_amount=total_amount-transfer_amount;
        printf("Transfer Successful\n");
        printf("Balance : %f",total_amount);
        fprintf(ptr,"£%f Have Been Transfered to Account %d\n",transfer_amount,trans_acc_number);
        fclose(ptr);
    }  
}
void account_details()
{
    printf("Account Details\n");
    printf("Account Name : %s\n",client_name);
    printf("Account Number : %d\n",account_number);
    printf("Balance : £%.3f\n",total_amount);
}
void transaction_details()
{
    FILE *ptr;
    ptr=fopen("Account.txt","r");
    char c = fgetc(ptr);
    if (c=EOF)
    {
        printf("No Transactions have been made\n");
    }
    else 
    {
        printf("Transaction Details\n");
        while (c!=EOF)
        {
            printf("%c",c);
            c=fgetc(ptr);
        }
    }
}
void last_details()
{
    printf("Account Details\n");
    printf("Account Name : %s\n",client_name);
    printf("Account Number : %d\n",account_number);
    printf("Balance : £%.3f\n",total_amount);
}
int  main()
{
    int cycle = 1;
    FILE *ptr = fopen("Account.txt","w");
    int mm_choice;
    printf("Enter Your Name : ");
    fgets(client_name,19,stdin);
    fprintf(ptr,"\nName : %s\n",client_name);
    printf("\n");

    printf("Enter Your Account Number : ");
    scanf("%d",&account_number);
    fprintf(ptr,"Account No.: %d\n",account_number);
    fclose(ptr);
    while (cycle ==1)
    {
        menu();
        printf("Please Select The Desired Service\n");
        scanf("%d",&mm_choice);
        switch (mm_choice)
        {
            case 1:
                system("cls");
                deposit();
                break;
            case 2:
                system("cls");
                withdraw();
                break;
            case 3: 
                system("cls");
                transfer();
                break;
            case 4:
                system("cls");
                account_details();
                break;
            case 5:
                system("cls");
                transaction_details();
                break;
            case 6:
                system("cls");
                last_details();
                cycle = 0;
                break;
            default:
                printf("Please Enter A Valid Number.\n");
        }
    }
    
    return 0;
}
1

There are 1 answers

1
NoDakker On

What is usual and customary for having processing that is to repeat until some specific exit request is selected is to encapsulate the processing cycle within a "while" loop and then either exiting the loop with a "break" statement or setting a condition that causes the termination or exit of the loop.

First off, when testing out your code, the compiler came back with a warning about an equality test that you would probably need to address.

void transaction_details()
{
    FILE *ptr;
    ptr=fopen("Account.txt","r");
    char c = fgetc(ptr);
    if (c=EOF)    /* Suspect that this was supposed to be "=="   */
    
/home/craig/C_Programs/Console/Banker/main.c|93|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|

Correcting that, and reviewing the good comments above, following is a refactored version of your "main" function with a "while" loop.

int  main()
{
    int cycle = 1;                          /* Flag for "while" loop    */
    FILE *ptr = fopen("Account.txt","w");
    int mm_choice;
    printf("Enter Your Name : ");
    //fgets(client_name);
    fgets(client_name, 19, stdin);
    fprintf(ptr,"\nName : %s\n",client_name);
    printf("\n");

    printf("Enter Your Account Number : ");
    scanf("%d",&account_number);
    fprintf(ptr,"Account No.: %d\n",account_number);
    fclose(ptr);

    while(cycle == 1)
    {
        menu();
        printf("Please Select The Desired Service\n");
        scanf("%d",&mm_choice);
        switch (mm_choice)
        {
        case 1:
            system("clear");    /* Built this on a Linux system - so switched to "clear"    */
            deposit();
            break;
        case 2:
            system("clear");
            withdraw();
            break;
        case 3:
            system("clear");
            transfer();
            break;
        case 4:
            system("clear");
            account_details();
            break;
        case 5:
            system("clear");
            transaction_details();
            break;
        case 6:
            system("clear");
            last_details();
            cycle = 0;          /* Used to exit the "while" loop    */
            break;              /* Added this with the use of a flag in lieu of "exit"  */
        default:
            printf("Please Enter A Valid Number.\n");
        }
    }
    return 0;
}

The key points here are as follows.

  • The functionality that is desired to be repeated was placed inside of a "while" loop.
  • Due to the use of a "switch" block and its use of "break" statements, a flag test was used in the "while" loop to provide the needed exit functionality when the user was done executing the program.

Just as an FYI, I built this on a Linux system, so I needed to do a small tweak to the system commands to clear the terminal screen.

With that bit of refactored code, following was some test output at the terminal.

craig@Vera:~/C_Programs/Console/Banker/bin/Release$ ./Banker 
Enter Your Name : Craig

Enter Your Account Number : 110000011
------------------------------
          Main Menu
------------------------------
1) Deposit
2) Withdraw
3) Transfer
4) Account Details
5) Transaction Details
6) Exit

Please Select The Desired Service
1


Deposit
Please Enter the Amount To Be Deposited
100.11
100.110000 Have Been Deposited To Account 110000011
Balance : 100.11------------------------------
          Main Menu
------------------------------
1) Deposit
2) Withdraw
3) Transfer
4) Account Details
5) Transaction Details
6) Exit

Please Select The Desired Service
6

Account Details
Account Name : Craig

Account Number : 110000011
Balance : £100.110

I'm sure there is more polishing up to be done to this program, and there are other ways to provide a processing cycle, this should give you food for thought on the setup and usage of a user cycle.