如何循环main函数?

问题描述 投票:0回答:1

我正在做一个银行管理系统项目,当我循环我的主要功能时,输出不是我所期望的。该代码工作正常,并在不循环时输出预期的数据,但一旦我到达循环部分,事情就会变得混乱。 PS:这是我在 stackoverflow 上的第一篇文章,所以如果您能帮助告诉我在这里提问的正确方法是什么,那么我会很高兴

C 代码混合发生在使用 goto 方法或 while (1) 时

#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()
{
    FILE *ptr = fopen("Account.txt","w");
    int mm_choice;
    printf("Enter Your Name : ");
    gets(client_name);
    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);
    **return_here:**
    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();
            exit(0);
        default:
            printf("Please Enter A Valid Number.\n");
    }
    **goto return_here;**
    return 0;
}

c loops goto
1个回答
0
投票

在选择某个特定退出请求之前重复处理的通常做法是将处理周期封装在“while”循环中,然后使用“break”语句退出循环或设置导致循环的终止或退出。

首先,在测试代码时,编译器返回一条有关您可能需要解决的相等测试的警告。

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]|

纠正这个问题,并回顾上面的好评论,下面是带有“while”循环的“main”函数的重构版本。

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;
}

这里的要点如下。

  • 需要重复的功能被放置在“while”循环内。
  • 由于使用了“switch”块及其“break”语句,因此在“while”循环中使用了标志测试,以便在用户执行完程序后提供所需的退出功能。

仅供参考,我在 Linux 系统上构建了这个,所以我需要对系统命令进行一些小调整以清除终端屏幕。

通过重构代码,以下是终端上的一些测试输出。

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

我确信这个程序还有更多的改进工作要做,并且还有其他方法来提供处理周期,这应该会给你关于用户周期的设置和使用的思考。

© www.soinside.com 2019 - 2024. All rights reserved.