gpt4 book ai didi

c - 为我的 C ATM 设置余额

转载 作者:行者123 更新时间:2023-11-30 19:35:43 24 4
gpt4 key购买 nike

嘿伙计们,我正在构建一个 ATM 程序,并且一切正常

我有它弹出的菜单,您可以选择一个选项,它会运行该功能,但是,我一生都不能

设定平衡并让它留下来直到它改变一旦两个选项(存款、取款)之一发生变化,我就需要保存它,因为这是一个后测试循环,它将继续下去,直到选择退出,每次我需要它来更新余额时。

这是我的 C 代码,如果有人可以提供帮助,那就太棒了。

#include <stdio.h>
#include <stdlib.h>

// Function Declarations

int getChoice ();
double withdraw (int Choice, int Balance);
double deposit (int Choice, int Balance);
int VBalance (int Choice, int Balance);
double process (int Choice, int Balance);



int main (void)
{
// Local Declarations

int Choice;
int Balance;


// Statements

do
{
Balance = 2500.00;
Choice = getChoice ();
process (Choice, Balance);
}
while (Choice != 0);


return 0;





} // Main

/*============================process=*/

double process (int Choice, int Balance)
{
// Declarations


// Statements
switch(Choice)
{
case 1: withdraw (Choice, Balance);
break;
case 2: deposit (Choice, Balance);
break;
case 3: VBalance (Choice, Balance);
break;
case 0: exit;
break;
deafult: printf("Sorry Option Not Offered");

} // switch

return 0;
}


/*============================getChoice=*/

int getChoice (void)
{
// Local Declarations
char Choice;

// Statements

printf("\n\n**********************************");
printf("\n MENU ");
printf("\n\t1.Withdrawl Money ");
printf("\n\t2.Deposit Money ");
printf("\n\t3.View Balance ");
printf("\n\t0.Exit ");
printf("\n**********************************");
printf("\nPlease Type Your Choice Using 0-3");
printf("\nThen Hit Enter: ");
scanf("%d", &Choice);

return Choice;

} //getchoice


/*============================withdraw=*/


double withdraw (int Choice, int Balance)
{
// Local Declarations
double amount;


// Statements
printf("Funds:%d", &Balance);
printf("\nPlease Enter How Much You Would Like To Withdraw: ");
scanf("%f", &amount);
Balance = Balance - amount;

return Balance;

} //withdraw


/*============================Deposit=*/

double deposit (int Choice, int Balance)
{
// Local Declarations
double amount;

// Statements
printf("Funds:%d", &Balance);
printf("\nPlease Enter How Much You Would Like To Deposit: ");
scanf("%f", &amount);
Balance = Balance + amount;

return Balance;

} //Deposit


/*============================VBalance=*/

int VBalance (int Choice, int Balance)
{
// Statements
printf("\nYour Current Funds:%d", &Balance);
printf("\nThank Your For Viewing");

return 0;
}

最佳答案

第一:启用编译器警告。如果您使用 gcc,请将 -Wall 添加到命令行。如果您使用 IDE,打开警告的选项应该位于编译器设置中。

当您编译时出现警告时,您会发现使用 printfscanf 时遇到一些问题。解决这些问题!

接下来,您会遇到 main 中的 Balance 未更新的问题。 C 使用按值调用,这意味着您对函数参数所做的更改在调用函数中不可见。要解决这个问题,您可以将余额参数声明为指针并传递 Balance 的地址。或者您可以返回新的 Balance,就像您在代码中所做的那样。您唯一忘记的是将新值存储到 main 中的 Balance

更改了main中的行:

Balance = process(Choice, Balance);

更改了流程:

double process(int Choice, int Balance)
{
// Declarations

// Statements
switch (Choice)
{
case 1: Balance = withdraw(Choice, Balance); // Changed line
break;
case 2: Balance = deposit(Choice, Balance); // Changed line
break;
case 3: Balance = VBalance(Choice, Balance); // Changed line
break;
case 0: exit;
break;
deafult: printf("Sorry Option Not Offered");

} // switch

return Balance; // Changed line
}

第三:Balance被声明为int,但有时您使用double。您应该选择一种类型并坚持下去。由于nature of floating point numbers实际上建议integral type is used for money .

关于c - 为我的 C ATM 设置余额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42243687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com