比我可能要麻烦的多。我正在构建一个非常简单的银行菜单界面,用户可以在其中查看其余额,存入,提取或退出菜单。 case 1
,case2
和case3
似乎可以正常工作,但是无论我尝试使用Google哪种方式,case 4
都无法正常工作。
在当前版本中,printf()
中的case 4
语句出现了,然后直到我两次输入其他选项之一(1-3)似乎什么都没有发生,此时循环将循环并自动使用不管第二输入是什么。我尝试了getchar()
,但似乎没有用(或者我没有正确实现)。
为什么会出现这种现象,我该如何解决?
这里是代码:
#include <stdio.h>
#include <cs50.h>
double deposit(double a, double b);
double withdraw(double a, double b);
int main(void)
{
int resume = 1;
int user_input = 0;
double user_balance = 10.00;
printf("Welcome to UBank!\n");
while (resume)
{
printf("\n====================\n");
printf("Select an operation:\n\n1. Show Balance\n2. Make a Deposit\n3. Make a Withdrawal\n4. Quit\n"
"====================\n\n");
scanf("%d", &user_input);
int quit_character = 0x00;
double deposit_amount = 0.00;
double withdraw_amount = 0.00;
switch (user_input)
{
case 1:
printf("Balance: $%.2lf\n", user_balance);
break;
case 2:
printf("How much would you like to deposit?\n");
scanf("%lf", &deposit_amount);
user_balance = deposit(user_balance, deposit_amount);
break;
case 3:
printf("How much would you like to withdraw?\n");
scanf("%lf", &withdraw_amount);
user_balance = withdraw(user_balance, withdraw_amount);
break;
case 4:
printf("Press Enter to finish banking or any other key to continue.\n");
scanf("%d\n", &quit_character);
if (quit_character == 0x0A)
{
resume = 0;
}
break;
}
}
}
double deposit(double a, double b)
{
if (b > 0 && b < 10000)
{
return a + b;
}
else
{
printf("Please enter a valid amount. (0.01 - 9999.99)\n");
return a;
}
}
double withdraw(double a, double b)
{
if (b > 0 && a - b >= 10)
{
return a - b;
}
else if (b <= 0)
{
printf("Withdrawal amount must be greater than $0.00.\n");
}
else if (a - b < 10)
{
printf("Withdrawal amount invalid. Remaining balance must be $10.00 or more.\n");
}
return a;
}
假设我想粉刷我的房屋的外部。我购买了油漆,然后回到家(距离最近的商店有5小时的车程)。当我回到家时,我意识到我忘了还买画笔。我不想再花10个小时开车去拿油漆刷,但是我应该如何油漆呢?
我可以使用扫帚,拖把,抹布,我的手,我儿子的手等,这些都不比刷子更可取。但是后来我意识到...我有一匹马!我夹住马的主体并制作自己的画笔!我为这一天画画,下周当我需要另一支刷子时再次修剪马。最终比10小时车程便宜很多。
好...这是一个虚假的,虚构的故事。关键是使用scanf()进行用户输入就像用猪鼻绘画一样。这只是工作的错误工具。最好编写自己的用户输入函数以预期的方式工作。
请考虑以下GetInput()函数。我必须自己制作,但是使用scanf()进行用户输入肯定能胜过:
#include <stdio.h>
#include <stdlib.h>
double deposit(double a, double b);
double withdraw(double a, double b);
/*-----------------------------------------------------------------------------------
** Get input from user, and return it as specific types.
**
** Caller may specify NULL for any (or all) input types that are not needed.
*/
void GetInput(int *intOut, char *charOut, double *doubleOut)
{
// Pointer to a temporary (allocated memory) buffer to hold user input.
char *line_A = NULL;
// Number of bytes allocated to the temporary (allocated) buffer.
size_t lineSize = 0;
// Holds the number of bytes entered by the user, or (-1) for error.
ssize_t lineLength;
lineLength=getline(&line_A, &lineSize, stdin);
if(-1 == lineLength)
{
fprintf(stderr, "getline() failed.\n"
goto CLEANUP;
}
// If the caller did not specify NULL for intOut, ...
if(intOut)
// Convert the string to an int and pass it back to the caller.
*intOut = atoi(line_A);
if(charOut)
*charOut = *line_A;
if(doubleOut)
*doubleOut = atof(line_A);
CLEANUP:
if(line_A)
free(line_A);
return;
}
int main(void)
{
int resume = 1;
int user_input = 0;
double user_balance = 10.00;
printf("Welcome to UBank!\n");
while (resume)
{
printf("\n====================\n");
printf("Select an operation:\n\n1. Show Balance\n2. Make a Deposit\n3. Make a Withdrawal\n4. Quit\n"
"====================\n\n");
GetInput(&user_input, NULL, NULL); // Get an integer from the user.
char quit_character = 0x00;
double deposit_amount = 0.00;
double withdraw_amount = 0.00;
switch (user_input)
{
case 1:
printf("Balance: $%.2lf\n", user_balance);
break;
case 2:
printf("How much would you like to deposit?\n");
GetInput(NULL, NULL, &deposit_amount); // Get a double from the user.
user_balance = deposit(user_balance, deposit_amount);
break;
case 3:
printf("How much would you like to withdraw?\n");
GetInput(NULL, NULL, &withdraw_amount); // Get a double from the user.
user_balance = withdraw(user_balance, withdraw_amount);
break;
case 4:
printf("Press Enter to finish banking or any other key to continue.\n");
GetInput(NULL, &quit_character, NULL); //Get a character from the user.
if (quit_character == 0x0A)
{
resume = 0;
}
break;
}
}
}
double deposit(double a, double b)
{
if (b > 0 && b < 10000)
{
return a + b;
}
else
{
printf("Please enter a valid amount. (0.01 - 9999.99)\n");
return a;
}
}
double withdraw(double a, double b)
{
if (b > 0 && a - b >= 10)
{
return a - b;
}
else if (b <= 0)
{
printf("Withdrawal amount must be greater than $0.00.\n");
}
else if (a - b < 10)
{
printf("Withdrawal amount invalid. Remaining balance must be $10.00 or more.\n");
}
return a;
}