简体   繁体   中英

Language-C, while-loop, error for wrong input

I really hope you can help me with this. So this is just a part of my Code, which doesn't. The part you see below doesn't work as intended. So the output of the remaining salary is also correct, but if the input is out of the given range(whether it is a number or letter) it doesn't output an error, as id doesn't repeat the question.

int main()
{
  int variable1 = 0;
  double variable2 = 0;
  double variable3 = 0;
  double variable4 = 0;
  char variable5 = 0;

  printf("Type in a number:\n");
  scanf("%d", &variable1);

  while((variable1<0) || (variable1>5000000))
  {
    printf("Error\n");

    if((variable1>0) && (variable1<=5000000))
    {
      printf("Question that requires the answer y/Y or n/N?:\n");
      scanf(" %c", &variable5);
    }
   return 0;
  }

  variable2 = Function1(variable1, variable2);
  variable3 = Function2(variable1, variable2, variable3);
  variable4 = Function3(variable3, variable4, variable5);

  while((variable5!='j') || (variable5!='J') || ((variable5!='n') || (variable5!='N' )))
  {
    printf("Error\n");
    break;
    if((variable5=='j') || (variable5=='J') || ((variable5=='n') || (variable5=='N' )))
    return 0;
      if((variable1>0) && (variable1<=5000000))
    {  
      printf("Output1:\t\t\t\t\t\t\t\t%12.2d\n", variable1);
      printf("Function1:\t\t\t%12.2lf\n", variable2);
      printf("Function2:\t%12.2lf\n" , variable3 + variable4);
      printf("Function3:\t\t\t\t\t\t\t%12.2lf\n" , variable1+ variable2+ variable3 + variable4);
      return 0;
    }
  }
}
while((variable1<0) || (variable1>5000000))
{
  printf("Error\n");

  if((variable1>0) && (variable1<=5000000))
  {
    printf("Question that requeires the answer y/Y or n/N?:\n");
    scanf(" %c", &variable5);
  }
 return 0;
}

Within the first while loop, remove if((variable1>0) && (variable1<=5000000)) . That condition is the converse of what seems to be intended, and it is unnecessary to tests, since the loop just tested whether the value is invalid. This explains why the question prompt doesn't repeat.

This code is equivalent to the following code.

int main()
{
  int variable1 = 0;
  double variable2 = 0;
  double variable3 = 0;
  double variable4 = 0;
  char variable5 = 0;

  printf("Type in a number:\n");
  scanf("%d", &variable1);

  while((variable1<0) || (variable1>5000000))
  {
    printf("Error\n");
    return 0;
  }

  variable2 = Function1(variable1, variable2);
  variable3 = Function2(variable1, variable2, variable3);
  variable4 = Function3(variable3, variable4, variable5);

  while((variable5!='j') || (variable5!='J') || ((variable5!='n') || (variable5!='N' )))
  {
    printf("Error\n");
    break;
  }
}

And the above code is equivalent to the following code.

int main()
{
  int variable1 = 0;

  printf("Type in a number:\n");
  scanf("%d", &variable1);

  if((variable1<0) || (variable1>5000000))
  {
    printf("Error\n");
    return 0;
  }

  printf("Error\n");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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