在 C 的开关盒中检测 Enter?
Detecting Enter within a switch case in C?
在这方面遇到的麻烦比我应该遇到的要多。我正在构建一个非常简单的银行菜单界面,用户可以在其中查看余额、存款、取款或退出菜单。 case 1
、case2
和 case3
似乎工作正常,但是 case 4
无法正常运行,无论我尝试使用 Google.
在当前版本中,出现 case 4
中的 printf()
语句,然后似乎没有其他任何事情发生,直到我输入其他选项之一 (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 [=10=].00.\n");
}
else if (a - b < 10)
{
printf("Withdrawal amount invalid. Remaining balance must be .00 or more.\n");
}
return a;
}
假设我想粉刷我家的外墙。我购买了油漆,然后 return 到我家(距离最近的商店有 5 小时的车程)。当我回到家时,我意识到我也忘了买画笔。我不想花接下来的 10 个小时开车去买油漆刷,但我应该如何涂漆?
我可以用扫帚、拖把、抹布、我的手、我儿子的手等等,其中none比刷子更好用。但后来我意识到……我有一匹马!我剪下马的主干并制作我自己的画笔!我今天画画,下周需要另一把刷子时再修剪我的马。这最终比 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 [=10=].00.\n");
}
else if (a - b < 10)
{
printf("Withdrawal amount invalid. Remaining balance must be .00 or more.\n");
}
return a;
}
在这方面遇到的麻烦比我应该遇到的要多。我正在构建一个非常简单的银行菜单界面,用户可以在其中查看余额、存款、取款或退出菜单。 case 1
、case2
和 case3
似乎工作正常,但是 case 4
无法正常运行,无论我尝试使用 Google.
在当前版本中,出现 case 4
中的 printf()
语句,然后似乎没有其他任何事情发生,直到我输入其他选项之一 (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 [=10=].00.\n");
}
else if (a - b < 10)
{
printf("Withdrawal amount invalid. Remaining balance must be .00 or more.\n");
}
return a;
}
假设我想粉刷我家的外墙。我购买了油漆,然后 return 到我家(距离最近的商店有 5 小时的车程)。当我回到家时,我意识到我也忘了买画笔。我不想花接下来的 10 个小时开车去买油漆刷,但我应该如何涂漆?
我可以用扫帚、拖把、抹布、我的手、我儿子的手等等,其中none比刷子更好用。但后来我意识到……我有一匹马!我剪下马的主干并制作我自己的画笔!我今天画画,下周需要另一把刷子时再修剪我的马。这最终比 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 [=10=].00.\n");
}
else if (a - b < 10)
{
printf("Withdrawal amount invalid. Remaining balance must be .00 or more.\n");
}
return a;
}