我有一些代码,但我会缩短它以仅显示相关部分。
// Displays the list of user’s options available
//Displays the user’s selections and sets the value of the choice
void mainMenu(int *choice);
//Displays the types of account they would like to access and sets the
//value of the chosen account type
void AccountMenu(char *typeAcct);
//Prompts the user for the amount of their deposit and updates the selected account
void DepositMoney(double *currBal);
//Asks the user if they want another transaction
void Repeat(char * doAgain);
int main(){
int choice = 0;
char repeat = 'y';
double checkBal = 575.0,
saveBal = 3750.0,
credBal = 450.0;
char typeAcct;
while(repeat!='n'){
mainMenu(&choice); //get action from user
AccountMenu(&typeAcct); //get account to perform action on from user
switch (choice){
case 1:
switch (typeAcct){
case 'c':
DepositMoney(&checkBal);
break;
case 's':
DepositMoney(&saveBal);
break;
case 'r':
DepositMoney(&credBal);
break;
} //case 1
break;
}
Repeat(&repeat);
repeat = tolower(repeat);
}
}
// Displays the list of user’s options available
//Displays the user’s selections and sets the value of the choice
void mainMenu(int *choice){
printf("Bank Program\n\n");
printf("Please enter your choice:\n");
printf("[1] Deposit\n");
printf("[2] Withdraw\n");
printf("[3] Balance Inquiry\n\n >> ");
scanf(" %d", choice);
}
//Displays the types of account they would like to access and sets the
//value of the chosen account type
void AccountMenu(char *typeAcct){
char choice;
printf("Please enter your choice:\n");
printf("[C] Checking\n");
printf("[S] Savings\n");
printf("[R] Credit\n\n >> ");
scanf(" %c", &choice);
*typeAcct = tolower(choice);
}
//Prompts the user for the amount of their deposit and updates the selected account
void DepositMoney(double *currBal){
printf("Depositing money.\nHow much would you like to deposit?\n\n >> ");
double amount = 0; //deposit amount
scanf(" &f", &amount);
*currBal = *currBal + amount;
}
void Repeat(char * doAgain){
printf("Would you like to perform another transaction? (y/n)\n\n >> ");
scanf(" &c", doAgain);
}
当我执行这段代码时,它基本上运行良好。例如,如果我为 mainMenu 输入“1”,为 AccountMenu 输入“c”,则 choice 和 typeAcct 确实分别设置为“1”和“c”。
问题似乎不在于循环。我已经尝试取出循环并只运行一次但它仍然不起作用。发生的事情是,在输入我想存入的金额后,变量的值没有更新,并且 mainMenu 和 AccountMenu 再次运行(即使 DepositMoney 是主函数中的最后一行代码并且没有环形)。为什么会这样?
最佳答案
1) scanf()
用法不正确
double amount = 0;
// scanf(" &f", &amount);
scanf(" %lf", &amount);
// scanf(" &c", doAgain);
scanf(" %c", doAgain);
2) 确保您的编译器警告已完全启用。这是最重要的一点,因为大多数问题都可以通过这种方式快速识别。
3) 始终检查scanf()
的返回值。顺便说一句,"%f"
中的前导空格是不需要的,因为 "%f"
无论如何都会扫描前导空白。不过很好,有 "%c"
。
if (scanf("%lf", &amount) != 1) Handle_Error();
4) tolower()
取一个int
。在用户输入 \x80
到 \xFF
范围内的 char
的罕见情况下,char
的转换> to int
是一个符号扩展,可能是一个负数。不幸的是,这对 tolower()
不起作用,它对 0 到 255 和 EOF 有很好的定义,但对其他负值不起作用。
// repeat = tolower(repeat);
repeat = tolower((unsigned char) repeat);
...
// *typeAcct = tolower(choice);
*typeAcct = tolower((unsigned char) choice);
关于c - 在 C 中使用指针的 Scanf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22339850/