嘿伙计们我正在构建一个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;
}
答案 0 :(得分:0)
首先:启用编译器警告。如果您使用gcc
,请在命令行中添加-Wall
。如果您使用IDE,则打开警告的选项应该在编译器设置中。
当您使用警告进行编译时,您会发现使用printf
和scanf
时遇到一些问题。修复那些!
接下来,您遇到Balance
main
未获得更新的问题。 C使用call by value,这意味着您对函数参数所做的更改在调用函数中不可见。要解决这个问题,您可以将balance参数声明为指针并传递Balance
的地址。或者您可以返回新的Balance
,就像您在代码中一样。您唯一忘记的是将新值存储到Balance
中的main
。
更改main
中的行:
Balance = process(Choice, Balance);
更改了process
:
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。