虽然我可以确认我正在输入正确类型的输入到我的C程序中,但我无法正确解释它的条件。当我运行以下代码时,程序成功调用lengthFormula()
,然后打印"Choose from the following..."
输入提示。C程序条件无法识别可操作的输入
int main(void)
{
char newCalculation;
do
{
char newCalculation;
lengthFormula();
printf("\nChoose from the following:\n a.) Calculate the length of a multi-bar scene in this project\n b.) Calculate bar length for a NEW project\n c.) Exit\n\n");
scanf(" %c", &newCalculation);
/* printf("%c", newCalculation); */
}while(tolower(newCalculation) == 'b');
if(tolower(newCalculation) == 'a')
{
float barLength;
multiBar(barLength);
}
if(tolower(newCalculation) == 'c')
{
exitProgram();
}
return 0;
}
当我进入a
,而不是再次调用lengthFormula()
,main()
回报0
和程序终止。当我取消注释第二个printf()调用并再次编译/运行时,输入a
返回a
,但它仍然返回0
/程序终止,而不是调用lengthFormula()
。
在@simplicisveritatis的建议,我也试着将我的条件语句,像这样的主要do-while循环中,具有相同的结果:
int main(void)
{
char newCalculation;
do
{
char newCalculation;
lengthFormula();
printf("\nChoose from the following:\n a.) Calculate the length of a multi-bar scene in this project\n b.) Calculate bar length for a NEW project\n c.) Exit\n\n");
scanf(" %c", &newCalculation);
/* printf("%c", newCalculation); */
if(tolower(newCalculation) == 'a')
{
float barLength;
multiBar(barLength);
}
if(tolower(newCalculation) == 'c')
{
exitProgram();
}
}while(tolower(newCalculation) == 'b');
return 0;
}
我如何改变我的条件,使他们识别我的输入?请不要告诉我使用getchar()
,因为每次我这样做,其他人都会告诉我使用scanf()
,其他人说这是危险的。我只是想了解为什么我的条件不起作用。
为什么他们告诉你使用'scanf()'?,请任何理由。你是否包含'stdio.h'和'ctype.h'?而这个“浮动杠” multiBar(barLength);'是未定义的行为,因为你永远不会初始化'barLength'。 –
我们知道,有关于如何从用户获得输入的章节。人们鄙视scanf,但其他人使用它也一样。一些人坚持cin >> v,其他人则警告说“这会给你一个字符串”......但是,您的scanf函数在上下文中是暂时的。 – JVene
'cin >> v'与这个问题无关,因为这是[tag:c]而不是[tag:C++],并且是'scanf()'是可怕的。特别是因为它没有正确地在书本中出现,他们通常忽略它的返回价值。 –