使用布尔标志检测输入错误,C
我试图创建一个布尔标志,当输入无效的输入代码时,该标志将输出错误消息。这是一个课堂作业,我正在学习C,所以请原谅格式,如果可以改进,请通知我。我让代码成功地完成了所有工作,除了在输入或输出变量不正确时输出错误消息。我相信这是代码中布尔检查部分的错误。诊断就好了 该代码将:使用布尔标志检测输入错误,C,c,if-statement,char,boolean,flags,C,If Statement,Char,Boolean,Flags,我试图创建一个布尔标志,当输入无效的输入代码时,该标志将输出错误消息。这是一个课堂作业,我正在学习C,所以请原谅格式,如果可以改进,请通知我。我让代码成功地完成了所有工作,除了在输入或输出变量不正确时输出错误消息。我相信这是代码中布尔检查部分的错误。诊断就好了 该代码将: 请求和源货币、目标货币和源金额 如果输入变量不正确,则输出目标货币金额或错误消息 参数: 源和目标货币必须为加元($)、日元(Y)或欧元(E) 必须使用布尔标志 提前谢谢 # include <stdio.h&g
- 请求和源货币、目标货币和源金额
- 如果输入变量不正确,则输出目标货币金额或错误消息
- 源和目标货币必须为加元($)、日元(Y)或欧元(E)
- 必须使用布尔标志
# include <stdio.h>
int main() {
char $ = $;
char Y = Y;
char E = E;
char in_currency, out_currency;
char flag = 1;
char new_line;
float in_value;
float out_value;
//Calculation variables
printf("Enter the source currency: ");
scanf("%c%c", &in_currency, &new_line);
printf("Enter the destination currency: ");
scanf("%c%c", &out_currency, &new_line);
if ( ( in_currency || out_currency ) != ( '$' || 'Y' || 'E' ) )
flag = 0;
printf("Enter the value: ");
scanf("%f%c", &in_value, &new_line);
//Calculations
if (flag) {
printf("%c%.2f = %c%.2f \n", in_currency, in_value, out_currency, out_value);
}
else {
printf("There was an error with the input. \n");
}
return 0;
}
#包括
int main(){
char$=$;
chary=Y;
chare=E;
字符输入\货币,输出\货币;
字符标志=1;
新线;
浮点数;
浮出_值;
//计算变量
printf(“输入源货币:”);
scanf(“%c%c”、&货币和新行);
printf(“输入目标货币:”);
scanf(“%c%c”、&out\u货币和新行);
如果((货币内| | |货币外)!=(“$”| |“Y”| |“E”))
flag=0;
printf(“输入值:”);
scanf(“%f%c”、&in\u值、&new\u行);
//计算
国际单项体育联合会(旗){
printf(“%c%.2f=%c%.2f\n”,单位货币,单位价值,单位货币,单位价值);
}
否则{
printf(“输入有错误。\n”);
}
返回0;
}
感谢@JonathanLeffler的精彩评论
我相信这可能是由于你的if语句的逻辑顺序
if ( ( in_currency || out_currency ) != ( '$' || 'Y' || 'E' ) )
flag = 0;
你打算做的事情如下
if (
(in_currency == '$' || in_currency == 'Y' || in_currency == 'E')
||
( out_currency == '$' || out_currency == 'Y' || out_currency == 'E')
) flag = 0;
这是由于如何评估原始行。
(单位货币| |单位货币)
注意(第6.8.4.1节)中的说明
如果表达式的比较结果不等于0,则执行子语句
和打字一样
(单位货币!=0 | |单位货币=!0)
请注意,测试单个值将始终针对非零值进行测试,有时被视为“真”值
简而言之,if语句的第一部分将始终返回true,因为两个测试(非零字符值)都等于true
我们可以将其视为(in不是零或out不是零),因为两个值都不是零,两个测试都等于真,变成(真或真)-显然这反过来也是真的
if语句的后半部分没有什么不同
( '$' || 'Y' || 'E' )
这也将等同于存在(真或真或真),因此始终等同于真,例如:
( '$' != 0 || 'Y' != 0 || 'E' != 0)
最后,这将分解为您的代码阅读:
if ((in_currency != 0 || out_currency =! 0) != ( '$' != 0 || 'Y' != 0 || 'E' != 0))
if ((true || true) != ( true || true || true))
if ((true) != (true))
显然,这将始终等于false,因为true永远不等于true(除非某些通用常量发生变化;-)
结果是你的旗帜总是假的始终
我给你的这一行用来替换它,而是分别测试每个可能的案例场景,并比较结果
你可以这样理解
if
in_currency **is equal to** '$' *or*
in_currency **is equal to** 'Y' *or*
in_currency **is equal to** 'E'
or
out_currency **is equal to** '$' *or*
out_currency **is equal to** 'Y' *or*
out_currency **is equal to** 'E'
then flag = 0;
[编辑]
用户Turboc在下面回答说,您可能希望使用&&(and),而不是在这两个语句之间使用&(and)。(请原谅我将此信息放在这里,但我还不能对答案发表评论)
这取决于您对语句的求值方式-如果您希望标志等于false(0)(如果货币为上述任何一种货币,则输出或输入为空),则情况并非如此)
但是,如果您希望声明为真,只要货币是上述货币之一,您可以使用以下
if
in_currency **is equal to** '$' *or*
in_currency **is equal to** 'Y' *or*
in_currency **is equal to** 'E'
and
out_currency **is equal to** '$' *or*
out_currency **is equal to** 'Y' *or*
out_currency **is equal to** 'E'
then flag = 1;
然而,如果是这样的话,你可能希望决定什么样的答案才能更有效地解决你的问题。感谢@JonathanLeffler的精彩评论 我相信这可能是由于你的if语句的逻辑顺序
if ( ( in_currency || out_currency ) != ( '$' || 'Y' || 'E' ) )
flag = 0;
你打算做的事情如下
if (
(in_currency == '$' || in_currency == 'Y' || in_currency == 'E')
||
( out_currency == '$' || out_currency == 'Y' || out_currency == 'E')
) flag = 0;
这是由于如何评估原始行。
(单位货币| |单位货币)
注意(第6.8.4.1节)中的说明
如果表达式的比较结果不等于0,则执行子语句
和打字一样
(单位货币!=0 | |单位货币=!0)
请注意,测试单个值将始终针对非零值进行测试,有时被视为“真”值
简而言之,if语句的第一部分将始终返回true,因为两个测试(非零字符值)都等于true
我们可以将其视为(in不是零或out不是零),因为两个值都不是零,两个测试都等于真,变成(真或真)-显然这反过来也是真的
if语句的后半部分没有什么不同
( '$' || 'Y' || 'E' )
这也将等同于存在(真或真或真),因此始终等同于真,例如:
( '$' != 0 || 'Y' != 0 || 'E' != 0)
最后,这将分解为您的代码阅读:
if ((in_currency != 0 || out_currency =! 0) != ( '$' != 0 || 'Y' != 0 || 'E' != 0))
if ((true || true) != ( true || true || true))
if ((true) != (true))
显然,这将始终等于false,因为true永远不等于true(除非某些通用常量发生变化;-)
结果是你的旗帜总是假的始终
我给你的这一行用来替换它,而是分别测试每个可能的案例场景,并比较结果
你可以这样理解
if
in_currency **is equal to** '$' *or*
in_currency **is equal to** 'Y' *or*
in_currency **is equal to** 'E'
or
out_currency **is equal to** '$' *or*
out_currency **is equal to** 'Y' *or*
out_currency **is equal to** 'E'
then flag = 0;
[编辑]
用户Turboc在下面回答说,您可能希望使用&&(and),而不是在这两个语句之间使用&(and)。(请原谅我将此信息放在这里,但我还不能对答案发表评论)
这取决于您对语句的求值方式-如果您希望标志等于false(0)(如果货币为上述任何一种货币,则输出或输入为空),则情况并非如此)
但是,如果您希望该声明是真实的,只要货币是上述货币之一,您就可以使用foll