简体   繁体   English

如果语句 c 未评估变量并跳过

[英]variable is not being evaluated and skips if statement c

I have a question.我有个问题。 When I try to ask a user to enter yes or no as a single character and set the char variable with brackets as I either get that Y or y is not valid in my if statement.当我尝试要求用户输入 yes 或 no 作为单个字符并使用括号设置 char 变量时,因为我得到 Y 或 y 在我的 if 语句中无效。 If I do it without brackets I only get the ascii value which will evaluate in the if statement.如果我不带括号这样做,我只会得到将在 if 语句中评估的 ascii 值。 The following code would fail to evaluate recAnswer value.以下代码将无法评估 recAnswer 值。 When I output the value in a printf it would show that recAnswer = "Y" or "y" depending on the input.当我 output 中的值 printf 时,它会根据输入显示 recAnswer = "Y" 或 "y"。 I've tried if(&recAnswer == "Y") and if(*recAnswer = "Y")我试过 if(&recAnswer == "Y") 和 if(*recAnswer = "Y")

int main(int argc, char** argv)
{
  int numEntered;
  char recAnswer[1];

  while(numEntered < 1 || numEntered > 15) // Continually ask user to enter a number until the number entered is between 1 and 15 . 
  {
     printf("\nPlease enter a number between 1 and 15:");
     scanf(" %d", &numEntered);
  } 
  printf("\nDo you want to get the factorial value recursively? Enter Y or N:"); //Ask user if they want to get the answer recursively 
  scanf(" %c",&recAnswer);
     
if(recAnswer == "y" || recAnswer == "Y")
{
  printf("The recursive value of %d is %d", numEntered, recursive(numEntered)); //Print out recursive value
}   
else
{
  printf("The non-recursive value of %d is %d", numEntered, nonRecursive(numEntered));  //Print out looped value
}
return 0;
}

Thank you for looking into this感谢您对此进行调查

For starters this loop对于初学者这个循环

  while(numEntered < 1 || numEntered > 15) // Continually ask user to enter a number until the number entered is between 1 and 15 . 
  {
     printf("\nPlease enter a number between 1 and 15:");
     scanf(" %d", &numEntered);
  } 

invokes undefined behavior because the variable numEntered was not initialized.调用未定义的行为,因为变量numEntered未初始化。

int numEntered;

substitute the while loop for a do-while loop like将 while 循环替换为 do-while 循环,例如

  do
  {
     numEntered = 0;
     printf("\nPlease enter a number between 1 and 15:");
     scanf(" %d", &numEntered);
  } while(numEntered < 1 || numEntered > 15); // Continually ask user to enter a number until the number entered is between 1 and 15 . 

Secondly if you are going to enter one character then there is no sense to declare an array with one element like其次,如果您要输入一个字符,那么声明一个包含一个元素的数组是没有意义的

char recAnswer[1];

(Note: Moreover you are using an incorrect argument for the conversion specifier %c (注意:此外,您使用的转换说明符 %c 的参数不正确

scanf(" %c",&recAnswer);
            ^^^^^^^^^^^ 
  • end note)尾注)

Declare an object of the type char and initialize it for example with the constant 'n'.声明一个char类型的 object 并使用常量“n”对其进行初始化。

char recAnswer = 'n';

And change the if statement the following way并通过以下方式更改 if 语句

if(recAnswer == 'y' || recAnswer == 'Y)

that is use character integer constants instead of string literals.即使用字符 integer 常量而不是字符串文字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM