#include <iostream>
static float balance = 30000.0;
void deposit(float amount) {
if (amount < 0.0 && amount > 100000.0 ) {
printf("Error invalid amount entered ");
}
else {
balance += amount;
}
}
float withdrawal(float withdrawal) {
if (balance == 0.0)
printf("NO MONEY BROKIE");
if (withdrawal < 17000.0) {
printf("Error invalid amount entered ");
}
if (balance <= withdrawal) {
withdrawal = balance;
balance = 0;
}
balance -= withdrawal;
return withdrawal;
}
void Check() {
printf("Your Account Balance is : %f ", &balance);
}
int main()
{
float amount;
int task;
printf(" 1. Check Balance \n 2. Deposit \n 3. Withdrawal");
printf(" \n\nPlease note :\nThe deposit amount must not be greater than $100,000 ");
printf("\nThe withdrawal amount must not be greater than $17,000 ");
printf("\n\nWhat service would you like to perform : ");
scanf_s("%d", &task);
if (task == 1) {
Check();
}
else if (task == 2) {
printf("Enter the amount you would like to deposit : ");
scanf_s("%f", &amount);
deposit(amount);
printf("Thanks for doing business you have deposited %f ", &amount );
}
else if (task == 3) {
printf("Enter the amount you would like to withdraw : ");
scanf_s("%f", &amount);
float Withdrawal = withdrawal(amount);
printf("Thanks for doing business you have withdrawn : %f ", &Withdrawal);
}
else {
printf("Invalid task number !");
}
}我还以为它能起作用呢。为什么我运行的时候平衡0.0000和任何其他错误,我可能会错过我是新的,所以更多的细节,为什么它要求更详细的.....................................................................................
发布于 2022-10-26 07:57:48
您犯了一些语法错误,下面是完整的工作代码:
#include <iostream>
static float balance = 30000.0;
void deposit(float amount) {
if (amount < 0.0 && amount > 100000.0 ) {
printf("Error invalid amount entered ");
}
else {
balance += amount;
}
}
float withdrawal(float withdrawal) {
if (balance == 0.0)
printf("NO MONEY BROKIE");
if (withdrawal < 17000.0) {
printf("Error invalid amount entered ");
}
if (balance <= withdrawal) {
withdrawal = balance;
balance = 0;
}
balance -= withdrawal;
return withdrawal;
}
void Check() {
printf("Your Account Balance is : %f ", balance);
}
int main()
{
float amount;
int task;
printf(" 1. Check Balance \n 2. Deposit \n 3. Withdrawal");
printf(" \n\nPlease note :\nThe deposit amount must not be greater than $100,000 ");
printf("\nThe withdrawal amount must not be less than $17,000 ");
printf("\n\nWhat service would you like to perform : ");
scanf("%d", &task);
if (task == 1) {
Check();
}
else if (task == 2) {
printf("Enter the amount you would like to deposit : ");
scanf("%f", &amount);
deposit(amount);
printf("Thanks for doing business you have deposited %f ", amount );
}
else if (task == 3) {
printf("Enter the amount you would like to withdraw : ");
scanf("%f", &amount);
float Withdrawal = withdrawal(amount);
printf("Thanks for doing business you have withdrawn : %f ", Withdrawal);
}
else {
printf("Invalid task number !");
}
}发布于 2022-10-26 07:50:31
除了字符串外,scanf期望通过指针传递填充的参数。printf期望参数按值传递。
这一行:
printf("Your Account Balance is : %f ", &balance);应:
printf("Your Account Balance is : %f ", balance);类似地,还有其他print语句需要通过值而不是指针传递主参数。如这一行:
printf("Thanks for doing business you have withdrawn : %f ", &Withdrawal);应:
printf("Thanks for doing business you have withdrawn : %f ", Withdrawal);另一个可能的窃听器。在withdrawl函数中,如果数量小于17000,则代码会打印错误,但不会立即返回。相反,它将继续处理事务。不确定这是否重要。
https://stackoverflow.com/questions/74204311
复制相似问题