我一直在编写ATM接口的代码,其中包含一个必须由用户估算才能访问选项的引脚。一旦完成,我有5个选项可供选择,如快速现金,取款,存款和支票余额。一切似乎运行良好唯一的事情是,当用户存款,取款或获得快速现金时,账户余额不会更新到正确的金额。有人可以帮我解决这个问题。我会在下面发布我的代码
#include <stdio.h>
int fastCash(int amount);
int deposit(int deposit);
int withdraw(int balence);
void checkBalence(int balence);
int main()
{
int pin;
int pins = 9999;
int pinTries = 1;
int reciept;
int options;
int options2;
int balence = 300;
int fastCashChoice;
printf("Enter your pin:");// the pin is 9999
scanf("%d", &pin);
while (pinTries <= 3)
{
if (pin == pins)
{
printf("Would you like a reciept:");
//1 is equal to yes and 2 is equal to no
scanf("%d", &reciept);
printf("Choose from the following:\n");
printf("1. Fast cash\n2. Withdraw\n3. Deposit\n4. Check balence\n5. Get card back");
scanf("%d", &options);
while (options <= 5)
{
switch (options)
{
case 1:
fastCash(fastCashChoice);
balence = balence - fastCashChoice;
break;
case 2:
withdraw(balence);
break;
case 3:
deposit(balence);
break;
case 4:
checkBalence(balence);
break;
case 5:
options2 == 2;
break;
}
printf("Would you like anohter transaction: ");// 1 is equal to yes and 2 is equal to no
scanf("%d", &options2);
if (options2 == 1)
{
printf("1. Fast cash\n2. Withdraw\n3. Deposit\n4. Check balence\n5. Get card back");
scanf("%d", &options);
}
else
{
options = 5;
pinTries = 4;
printf("Thank you for useing this ATM, GoodBye\n");
}
}
}
else if (pin != pins)
{
printf("Invalid pin, try again:");
scanf("%d", &pin);
pinTries++;
}
if (pinTries == 3)
{
printf("Sorry, you cant continue, please contact your bank");
}
}
return 0;
}
int fastCash(int amount)
{
int choice;
printf("1. $20.00\n2. 40.00\n3. 80.00\n4. 100.00\n5. Exit");
scanf("%d", &choice);
switch (choice)
{
case 1:
amount = 20;
case 2:
amount = 40;
case 3:
amount = 80;
case 4:
amount = 100;
case 5:
break;
}
return amount;
}
int withdraw(int balence)
{
int withdrawAmount;
printf("Enter the amount you would like to withdraw: ");
scanf("%d", &withdrawAmount);
balence = -withdrawAmount;
return balence;
}
int deposit(int balence)
{
int depositAmount;
printf("Enter an amount you would like to deposit: ");
scanf("%d", &depositAmount);
balence += depositAmount;
return balence;
}
void checkBalence(int balence)
{
printf("Your current balence is: %d\n", balence);
return;
}
答案 0 :(得分:3)
当您将int
(或任何其他非指针变量)传递给函数时,您只传递它的副本。如果函数然后更改它(如deposit
,例如,),它将只更改传递的副本,并且不会影响原始变量。相反,您需要将指针传递给原始值。 E.g:
int deposit(int* balance)
{
int depositAmount;
printf("Enter an amount you would like to deposit: ");
scanf("%d", &depositAmount);
*balance += depositAmount;
}
调用函数应该将指针传递给这个变量而不是变量本身:
case 3:
deposit(&balance);
/* Here-^ */
break;
答案 1 :(得分:2)
在您的代码中,您似乎只是将存款传入该功能,但您不会将其引用回原始balence
变量,因此余额保持在300。
例如,在函数中:
int deposit(int balence)
{
int depositAmount;
printf("Enter an amount you would like to deposit: ");
scanf("%d", &depositAmount);
balence += depositAmount;
return balence;
}
您刚刚发送了balence
,但就像Mureinik所说的那样,您只是传递了原始值而没有更改其值(它只更改了balence
< em> deposit()
内的。
相反,您可以通过引用传入它,或者您可以将balence
作为全局变量移动到代码的顶部,以便所有函数都可以看到它:
//code here.....
void checkBalence();
int balence = 300;
//more code here...
另外,请务必删除balence
函数中的deposit()
调用,以避免本地和全局变量之间存在歧义。
int deposit()
{
/*..original code here..*/
}
...现在,在deposit()
函数中,您的balence
变量现在指向全局balence
。
以下是最终的更正代码:
#include <stdio.h>
int fastCash(int amount);
int deposit();
int withdraw();
void checkBalence();
int balence = 300;
int main()
{
int pin;
int pins = 9999;
int pinTries = 1;
int reciept;
int options;
int options2;
int fastCashChoice;
printf("Enter your pin:");// the pin is 9999
scanf("%d", &pin);
while(pinTries <= 3)
{
if(pin == pins)
{
printf("Would you like a reciept:");
//1 is equal to yes and 2 is equal to no
scanf("%d", &reciept);
printf("Choose from the following:\n");
printf("1. Fast cash\n2. Withdraw\n3. Deposit\n4. Check balence\n5. Get card back");
scanf("%d", &options);
while(options <= 5)
{
switch(options)
{
case 1:
fastCash(fastCashChoice);
balence = balence - fastCashChoice;
break;
case 2:
withdraw(balence);
break;
case 3:
deposit(balence);
break;
case 4:
checkBalence(balence);
break;
case 5:
options2 = 2;
break;
}
printf("Would you like anohter transaction: ");// 1 is equal to yes and 2 is equal to no
scanf("%d", &options2);
if(options2 == 1)
{
printf("1. Fast cash\n2. Withdraw\n3. Deposit\n4. Check balence\n5. Get card back");
scanf("%d", &options);
}
else
{
options = 5;
pinTries = 4;
printf("Thank you for useing this ATM, GoodBye\n");
break;
}
}
}
else if(pin != pins)
{
printf("Invalid pin, try again:");
scanf("%d", &pin);
pinTries++;
}
if(pinTries == 3)
{
printf("Sorry, you cant continue, please contact your bank");
}
}
return 0;
}
int fastCash(int amount)
{
int choice;
printf("1. $20.00\n2. 40.00\n3. 80.00\n4. 100.00\n5. Exit");
scanf("%d", &choice);
switch(choice)
{
case 1:
amount = 20;
case 2:
amount = 40;
case 3:
amount = 80;
case 4:
amount = 100;
case 5:
break;
}
return amount;
}
int withdraw()
{
int withdrawAmount;
printf("Enter the amount you would like to withdraw: ");
scanf("%d", &withdrawAmount);
balence -= withdrawAmount;
return balence;
}
int deposit()
{
int depositAmount;
printf("Enter an amount you would like to deposit: ");
scanf("%d", &depositAmount);
balence += depositAmount;
return balence;
}
void checkBalence(int balence)
{
printf("Your current balence is: %d\n", balence);
return;
}
现在,它应按预期运行,这里产生的最终余额为176美元:
Enter your pin:9999
Would you like a reciept:1
Choose from the following:
1. Fast cash
2. Withdraw
3. Deposit
4. Check balence
5. Get card back2
Enter the amount you would like to withdraw: 124
Would you like anohter transaction: 1
1. Fast cash
2. Withdraw
3. Deposit
4. Check balence
5. Get card back4
Your current balence is: 176
答案 2 :(得分:1)
嗯,解决问题的一个简单方法就是将int balence
声明为全局。您可以在main()
函数上方声明它,而不是在main()
函数内声明它。
这将解决您当前的问题。
答案 3 :(得分:1)
deposit
和withdraw
都返回更新的balance
,但您不使用返回值。尝试将呼叫更改为:
case 2:
balence = withdraw(balence);
break;
case 3:
balence = deposit(balence);
break;
fastCash
会返回要提取的现金金额,因此您需要更新main
中的余额:
case 1:
balence = balence - fastCash(fastCashChoice);
break;
这避免了两个指针(你需要额外的错误处理,即检查NULL
)和全局变量(这会使你的程序更复杂*)。
您的代码中还有一些问题。发送给fastCash
的参数根本没有被使用,因为您返回要撤回的金额。