当用户输入无效数字时,如何给出打印语句错误';s或字母C
我有一个while循环菜单系统,可以收集用户的输入。但是,可以选择的选项数量显然有限(在本例中为4和6) 菜单一: 我的第一个菜单使用用户输入的字母来选择动作。我想让用户得到一个printf语句,如果他们使用了菜单上未使用的任何字母,则说明他们输入了无效的字母。代码如下:当用户输入无效数字时,如何给出打印语句错误';s或字母C,c,input,menu,C,Input,Menu,我有一个while循环菜单系统,可以收集用户的输入。但是,可以选择的选项数量显然有限(在本例中为4和6) 菜单一: 我的第一个菜单使用用户输入的字母来选择动作。我想让用户得到一个printf语句,如果他们使用了菜单上未使用的任何字母,则说明他们输入了无效的字母。代码如下: printf("\n\nOption | Action\n"); printf(" Q | Quit\n"); printf(" T | Loan Table\n"); printf("
printf("\n\nOption | Action\n");
printf(" Q | Quit\n");
printf(" T | Loan Table\n");
printf(" C | Calculate Loan\n");
printf(" E | Explanation\n");
printf("Select Option: ");
fseek(stdin, 0, SEEK_END);
scanf("%c", &menuChoice);
menuChoice = toupper(menuChoice);
我目前使用的错误句柄是:
if(menuChoice != 'Q' || menuChoice != 'T' || menuChoice != 'C' || menuChoice != 'E')
{
printf("Error! Choose a valid menu option.");
}
这显然不起作用,因为如果它不是E,例如,但仍然是C,它仍然会调出错误消息。我已经删除了这个
我的while循环目前使用它,但是它没有做任何事情,而且我对while循环在C中的工作方式仍然非常熟悉
我对此菜单的问题是,如何为此菜单调出打印语句错误
菜单二:
我的另一个菜单有点复杂,因为它使用数字系统。它基本上与上面的菜单相似,只是有数字。然而,还有更多的条件。菜单代码为:
printf("\nYou selected option C.\n");
printf("\nLoan Calculator\n");
printf("Option | Plan\n");
printf(" 1 | LF1\n");
printf(" 2 | LF2\n");
printf(" 3 | BBPL\n");
printf(" 4 | ILZERO\n");
printf(" 5 | ILFIVE\n");
printf(" 6 | LS5\n");
printf("Select Plan: ");
fseek(stdin, 0, SEEK_END);
scanf("%d", &planCode);
//CHECK IF IT IS A NUMBER BETWEEN 1 AND 6
printf("Cost of car: ");
fseek(stdin, 0, SEEK_END);
scanf("%d", &carCost);
//CHECK IF IT IS A NUMBER
printf("Deposit: ");
fseek(stdin, 0, SEEK_END);
scanf("%d", &deposit);
//CHECK IF IT IS A NUMBER
printf("Length of Loan (years): ");
fseek(stdin, 0, SEEK_END);
scanf("%d", &loanLength);
//CHECK IF IT IS A NUMBER AND IS BETWEEN 1 AND 10
如图所示,有相当多的条件。我尝试了很多方法来捕捉错误,但是程序总是会崩溃,或者即使不满足条件也会显示print语句
我试过!isdigit()
但是,即使是数字,它仍会播放该语句
如何捕捉这些错误
我是C语言的新手,他们对C语言的在线资源是有限的,希望你能提供帮助。提前谢谢
以下是我编写的全部代码,如果这还不够信息:
包括:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
主要内容:
intmain()
{
char menuChoice;
整数规划码;
成本、存款、贷款长度;
双重贷款初始;
双重贷款支付;
双铝酸盐;
双倍收费;
做
{
printf(“\n\n选项|操作\n”);
printf(“Q|Quit\n”);
printf(“贷款表”);
printf(“C|Calculate Loan\n”);
printf(“解释”);
printf(“选择选项:”);
fseek(stdin,0,SEEK_END);
scanf(“%c”和menuChoice);
menuChoice=toupper(menuChoice);
如果(menuChoice=='T')
{
printf(“\n您选择了选项T”。\n”);
printf(“\nEndor Loan Details List\n”);
printf(“+-----------------------------------------------------------------------------------------------------------------------------------+\n”);
printf(“贷款供应商|贷款产品代码|初始费用|月费|利率%|最高贷款金额|最低贷款金额|\n”);
printf(“+-----------------------------------------------------------------------------------------------------------------------------------+\n”);
printf(“| 1 |叶友| LF1 | 150美元| 0美元| 6.14%| 70000美元| 5000美元|\n”);
printf(“+-----------------------------------------------------------------------------------------------------------------------------------+\n”);
printf(“| 2 |叶友| LF2 |$155 |$0 | 6.24%|$70000 |$1000 |\n”);
printf(“+-----------------------------------------------------------------------------------------------------------------------------------+\n”);
printf(“3家大银行150美元0美元6.30%| 80000美元| 5000美元”);
printf(“+-----------------------------------------------------------------------------------------------------------------------------------+\n”);
printf(“4 | ITSALOAN | ILZERO | 149美元| 0美元| 7.10%*| 50000美元| 1000美元|\n”);
printf(“+-----------------------------------------------------------------------------------------------------------------------------------+\n”);
printf(“| 5 | ITSALOAN | ILFIVE | 140美元| 5美元| 7.10%*| 50000美元| 1000美元|\n”);
printf(“+-----------------------------------------------------------------------------------------------------------------------------------+\n”);
printf(“6只贷款绵羊5只| 349美元| 0只| 5.91%| 50000美元| 500美元|\n”);
printf(“+-----------------------------------------------------------------------------------------------------------------------------------+\n”);
printf(“注*:对于任何超过20000美元的贷款,ITSALOAN目前有一项特别促销活动,利率降至6.1%”;
}
如果(menuChoice=='C')
{
printf(“\n您选择了选项C”。\n”);
printf(“\n计算器\n”);
printf(“选项|计划”);
printf(“1 | LF1\n”);
printf(“2 | LF2\n”);
printf(“3 | BBPL\n”);
printf(“4 | ILZERO\n”);
printf(“5 | ILFIVE\n”);
printf(“6 | LS5\n”);
printf(“选择计划:”);
fseek(stdin,0,SEEK_END);
scanf(“%d”和平面代码);
//检查它是否是介于1和6之间的数字
printf(“汽车成本:”);
fseek(stdin,0,SEEK_END);
scanf(“%d”、&carCost);
//检查它是否是一个数字
printf(“存款:”);
fseek(stdin,0,SEEK_END);
scanf(“%d”和存款);
//检查它是否是一个数字
printf(“贷款期限(年):”;
fseek(stdin,0,SEEK_END);
scanf(“%d”和贷款长度);
//检查它是否是一个数字,并且介于1和10之间
如果(平面代码==1)
{
贷款初始=成本-存款;
如果
double calcPayment(double loanAnnualRate, double loan, int length)
{
double retv;
int payments;
double mul;
double rate;
rate = loanAnnualRate / 12;
payments = length * 12;
mul = pow(1 + rate, payments);
retv = (loan * mul * rate) / (mul - 1);
return retv;
}
int main()
{
char menuChoice;
int planCode;
int carCost, deposit, loanLength;
double loanInitial;
double loanPayments;
double IALSpecialRate;
double fee;
do
{
printf("\n\nOption | Action\n");
printf(" Q | Quit\n");
printf(" T | Loan Table\n");
printf(" C | Calculate Loan\n");
printf(" E | Explanation\n");
printf("Select Option: ");
fseek(stdin, 0, SEEK_END);
scanf("%c", &menuChoice);
menuChoice = toupper(menuChoice);
if (menuChoice == 'T')
{
printf("\nYou selected option T.\n");
printf("\nVendor Loan Details List\n");
printf("+-----------------------------------------------------------------------------------------------------------------------+\n");
printf("| | Loan Vendor | Loan Product Code | Initial Fee | Monthly Fee | Interest Rate % | Max. Loan Amount | Min. Loan Amount |\n");
printf("+-----------------------------------------------------------------------------------------------------------------------+\n");
printf("| 1 | Leaf-Friend | LF1 | $150 | $0 | 6.14%% | $70,000 | $5,000 |\n");
printf("+-----------------------------------------------------------------------------------------------------------------------+\n");
printf("| 2 | Leaf-Friend | LF2 | $155 | $0 | 6.24%% | $70,000 | $1,000 |\n");
printf("+-----------------------------------------------------------------------------------------------------------------------+\n");
printf("| 3 | Big Bank | BBPL | $150 | $0 | 6.30%% | $80,000 | $5,000 |\n");
printf("+-----------------------------------------------------------------------------------------------------------------------+\n");
printf("| 4 | ITSALOAN | ILZERO | $149 | $0 | 7.10%%* | $50,000 | $1,000 |\n");
printf("+-----------------------------------------------------------------------------------------------------------------------+\n");
printf("| 5 | ITSALOAN | ILFIVE | $140 | $5 | 7.10%%* | $50,000 | $1,000 |\n");
printf("+-----------------------------------------------------------------------------------------------------------------------+\n");
printf("| 6 | Loan Sheep | LS5 | $349 | $0 | 5.91%% | $50,000 | $500 |\n");
printf("+-----------------------------------------------------------------------------------------------------------------------+\n");
printf("NOTE*: ITSALOAN has a special promotion on at the moment for any loan above $20000 the interest rate drops to 6.1%");
}
if (menuChoice == 'C')
{
printf("\nYou selected option C.\n");
printf("\nLoan Calculator\n");
printf("Option | Plan\n");
printf(" 1 | LF1\n");
printf(" 2 | LF2\n");
printf(" 3 | BBPL\n");
printf(" 4 | ILZERO\n");
printf(" 5 | ILFIVE\n");
printf(" 6 | LS5\n");
printf("Select Plan: ");
fseek(stdin, 0, SEEK_END);
scanf("%d", &planCode);
//CHECK IF IT IS A NUMBER BETWEEN 1 AND 6
printf("Cost of car: ");
fseek(stdin, 0, SEEK_END);
scanf("%d", &carCost);
//CHECK IF IT IS A NUMBER
printf("Deposit: ");
fseek(stdin, 0, SEEK_END);
scanf("%d", &deposit);
//CHECK IF IT IS A NUMBER
printf("Length of Loan (years): ");
fseek(stdin, 0, SEEK_END);
scanf("%d", &loanLength);
//CHECK IF IT IS A NUMBER AND IS BETWEEN 1 AND 10
if (planCode == 1)
{
loanInitial = carCost - deposit;
if (loanInitial < 5000.00)
{
printf("\nSorry! Your loan is too low to be used with LF1.\nTry either LF2, ILZERO or ILFIVE.\n");
}
if (loanInitial > 70000.00)
{
printf("\nSorry! Youe loan is too high to be used with LF1.\nTry BBPL.\n");
}
if (loanInitial >= 5000.00 && loanInitial <= 70000.00)
{
loanPayments = calcPayment(0.0614, loanInitial, loanLength);
fee = 150;
printf("Loan Size : $ %8.2lf", loanInitial);
printf("\nTotal Repayment : $ %8.2lf", loanPayments * 12 * loanLength + fee);
printf("\nMonthly Repayments : $ %8.2lf", loanPayments);
printf("\nAnnual Repayment(s) : $ %8.2lf", loanPayments * 12);
printf("\nTotal Repayment Without Fees : $ %8.2lf", loanPayments * 12 * loanLength);
printf("\nTotal Fees : $ %8.2lf", fee);
printf("\nActual Loan Cost : $ %8.2lf", loanPayments * 12 * loanLength + fee - loanInitial);
}
}
if (planCode == 2)
{
loanInitial = carCost - deposit;
if (loanInitial < 5000.00)
{
printf("\nSorry! Your loan is too low to be used with LF2.\nTry LS5\n");
}
if (loanInitial > 70000.00)
{
printf("\nSorry! Youe loan is too high to be used with LF2.\nTry BBPL.\n");
}
if (loanInitial >= 1000.00 && loanInitial <= 70000.00)
{
loanPayments = calcPayment(0.0624, loanInitial, loanLength);
fee = 155;
printf("Loan Size : $ %8.2lf", loanInitial);
printf("\nTotal Repayment : $ %8.2lf", loanPayments * 12 * loanLength + fee);
printf("\nMonthly Repayments : $ %8.2lf", loanPayments);
printf("\nAnnual Repayment(s) : $ %8.2lf", loanPayments * 12);
printf("\nTotal Repayment Without Fees : $ %8.2lf", loanPayments * 12 * loanLength);
printf("\nTotal Fees : $ %8.2lf", fee);
printf("\nActual Loan Cost : $ %8.2lf", loanPayments * 12 * loanLength + fee - loanInitial);
}
}
if (planCode == 3)
{
loanInitial = carCost - deposit;
if (loanInitial < 5000.00)
{
printf("\nSorry! Your loan is too low to be used with BBPL.\nTry either LF2, ILZERO or ILFIVE.\n");
}
if (loanInitial > 80000.00)
{
printf("\nSorry! Youe loan is too high to be used with BBPL.\nTry a cheaper car below $80,000.\n");
}
if (loanInitial >= 5000.00 && loanInitial <= 80000.00)
{
loanPayments = calcPayment(0.063, loanInitial, loanLength);
fee = 150;
printf("Loan Size : $ %8.2lf", loanInitial);
printf("\nTotal Repayment : $ %8.2lf", loanPayments * 12 * loanLength + fee);
printf("\nMonthly Repayments : $ %8.2lf", loanPayments);
printf("\nAnnual Repayment(s) : $ %8.2lf", loanPayments * 12);
printf("\nTotal Repayment Without Fees : $ %8.2lf", loanPayments * 12 * loanLength);
printf("\nTotal Fees : $ %8.2lf", fee);
printf("\nActual Loan Cost : $ %8.2lf", loanPayments * 12 * loanLength + fee - loanInitial);
}
}
if (planCode == 4)
{
loanInitial = carCost - deposit;
if (loanInitial < 1000.00)
{
printf("\nSorry! Your loan is too low to be used with ILFIVE.\nTry LS5.\n");
}
if (loanInitial > 50000.00)
{
printf("\nSorry! Youe loan is too high to be used with ILFIVE.\nTry either LF1, LF2, BBPL or LS5.\n");
}
if (loanInitial >= 1000.00 && loanInitial <= 50000.00)
{
if (loanInitial >= 20000) { IALSpecialRate = 0.061; }
else IALSpecialRate = 0.071;
loanPayments = calcPayment(IALSpecialRate, loanInitial, loanLength);
fee = 149;
printf("Loan Size : $ %8.2lf", loanInitial);
printf("\nTotal Repayment : $ %8.2lf", loanPayments * 12 * loanLength + fee);
printf("\nMonthly Repayments : $ %8.2lf", loanPayments);
printf("\nAnnual Repayment(s) : $ %8.2lf", loanPayments * 12);
printf("\nTotal Repayment Without Fees : $ %8.2lf", loanPayments * 12 * loanLength);
printf("\nTotal Fees : $ %8.2lf", fee);
printf("\nActual Loan Cost : $ %8.2lf", loanPayments * 12 * loanLength + fee - loanInitial);
}
}
if (planCode == 5)
{
loanInitial = carCost - deposit;
if (loanInitial < 1000.00)
{
printf("\nSorry! Your loan is too low to be used with ILFIVE.\nTry LS5.\n");
}
if (loanInitial > 50000.00)
{
printf("\nSorry! Youe loan is too high to be used with ILFIVE.\nTry either LF1, LF2, BBPL or LS5.\n");
}
if (loanInitial >= 1000.00 && loanInitial <= 50000.00)
{
if (loanInitial >= 20000) { IALSpecialRate = 0.061; }
else IALSpecialRate = 0.071;
loanPayments = calcPayment(IALSpecialRate, loanInitial, loanLength);
loanPayments = calcPayment(0.071, loanInitial, loanLength);
fee = 40;
printf("Loan Size : $ %8.2lf", loanInitial);
printf("\nTotal Repayment : $ %8.2lf", loanPayments * 12 * loanLength + fee + (60 * loanLength));
printf("\nMonthly Repayments : $ %8.2lf", loanPayments + 5);
printf("\nAnnual Repayment(s) : $ %8.2lf", loanPayments * 12 + 60);
printf("\nTotal Repayment Without Fees : $ %8.2lf", loanPayments * 12 * loanLength);
printf("\nTotal Fees : $ %8.2lf", fee);
printf("\nActual Loan Cost : $ %8.2lf", loanPayments * 12 * loanLength + fee - loanInitial + (60 * loanLength));
}
}
if (planCode == 6)
{
loanInitial = carCost - deposit;
if (loanInitial < 500.00)
{
printf("\nSorry! Your loan is too low to be used with LS5.\n");
}
if (loanInitial > 55000.00)
{
printf("\nSorry! Youe loan is too high to be used with LS5.\nTry either LF1, Lf2 or BBPL.\n");
}
if (loanInitial >= 500.00 && loanInitial <= 55000.00)
{
loanPayments = calcPayment(0.0591, loanInitial, loanLength);
fee = 349;
printf("Loan Size : $ %8.2lf", loanInitial);
printf("\nTotal Repayment : $ %8.2lf", loanPayments * 12 * loanLength + fee);
printf("\nMonthly Repayments : $ %8.2lf", loanPayments);
printf("\nAnnual Repayment(s) : $ %8.2lf", loanPayments * 12);
printf("\nTotal Repayment Without Fees : $ %8.2lf", loanPayments * 12 * loanLength);
printf("\nTotal Fees : $ %8.2lf", fee);
printf("\nActual Loan Cost : $ %8.2lf", loanPayments * 12 * loanLength + fee - loanInitial);
}
}
}
if (menuChoice == 'E') { printf("\nYou selected option E.\n"); }
if (menuChoice == 'Q')
{
printf("\nExiting Program...\n");
exit(0);
}
} while (menuChoice != 'Q' || menuChoice != 'T' || menuChoice != 'C' || menuChoice != 'E');
printf("\nPlease Choose a valid option.\n");
_getch;
return 0;
switch (menuChoice)
{
case 'Q':
do_whatever_q_does();
break;
case 'E':
do_whatever_e_does();
break;
(and so on)
default :
throw_error_message();
}
bool readAndValidateInput(int* input, int min, int max) {
fseek(stdin, 0, SEEK_END);
if (scanf(" %d", input) != 1) {
fprintf(stderr, "Invalid input.\n");
return false;
}
if (min == 0 && max == 0)
return true;
if (*input < min || *input > max) {
fprintf(stderr, "Input not in range.\n");
return false;
}
return true; }
do {
printf("Select Plan: ");
//CHECK IF IT IS A NUMBER BETWEEN 1 AND 6
} while (!readAndValidateInput(&planCode, 1, 6))
do {
printf("Cost of car: ");
// supply 0 for min and max to omit range validation
} while(!readAndValidateInput(&carCost, 0, 0));