【问题标题】:I have a problem with variable's assignment and While function我对变量的赋值和 While 函数有疑问
【发布时间】:2019-09-08 18:48:43
【问题描述】:

这是我的第一篇文章,很高兴成为这个社区的一员。我是这方面的初学者,从我发现的不同教程中学习,这是我的第一个“程序”。我来找你们,因为我的代码有两个问题我的程序员朋友无法回答我(事实上,她告诉我在这里问):

我确信这是一个简单的答案,但我只是不知道,我在互联网上找不到任何正确的答案(或者我看起来不正确)。

谢谢大家!

#include <stdio.h>
#include <string.h>

int main()
{
// insert code here...
int num1;
int num3;
char ans[3];

printf("Enter first number: ");
scanf("%i", &num1);
printf("Enter second number: ");
scanf("%i", &num3);
while(!(strcmp(ans,"Sum") | (!(strcmp(ans,"Sub"))) | (!(strcmp(ans,"Mul"))) | (! 
(strcmp(ans,"Div"))) == 0));
printf("What kind of operation do you want to make? Type Sum, Sub, Mul or Div: 
");
scanf("%s", ans);
int Sum = num1 + num3;
int Sub = num1 - num3;
int Mul = num1 * num3;
//    int Div = num1 / num2;

if((strcmp(ans,"Sum") == 0))
{
    printf("The sum of both numbers give %d\n", Sum);
}
//    {printf("%d y %d\n", num1, num2);
//}
if((strcmp(ans,"Sub") == 0))
{
    printf("The substraction of both numbers give %d\n", Sub);
}
if((strcmp(ans,"Mul") == 0))
{
    printf("The multiplication of both numbers give %d\n", Mul);
}
if((strcmp(ans,"Div") == 0))
{
    printf("The division of both numbers give %d\n", num1 / num3);
}
{    if (!(strcmp(ans,"Sum") | (!(strcmp(ans,"Sub"))) | (!(strcmp(ans,"Mul"))) | 
(!(strcmp(ans,"Div"))) == 0))
{
    printf("This command does not apply, please type the correct option\n");
}
    //((strcmp(ans, "Sum")) | (strcmp(ans, "Sub")) | (strcmp(ans, "Mul")) | 
(strcmp(ans, "Div")) != 0) {



}
}
  • 当我运行程序时,它不会为“num2”分配除 0 以外的任何值。

  • 我真正希望“while”函数做的是告诉您输入了错误的字符并返回到主要问题,直到您输入正确为止。

P.S:现在我已经发送了我写的所有代码。

【问题讨论】:

  • 您的代码格式已损坏。为什么到处都是反引号?
  • 你应该先从基本的开始。你的缩进不好,连代码都不完整。
  • strcmp(ans,"Sum") 您的数组ans 未初始化,它太短而无法容纳您要与之比较的字符串。 Sum 需要 4 个字符来存储字母和终止 nul 字符。
  • 您的while 循环仅包含一条空指令。即使您为ans 提供值,它也不会做任何事情
  • 一般建议:您应该始终检查scanf函数调用的结果。返回值是有含义的(您可以在手册页中找到它,这是任何教程的 C 标准)并且不应被忽略。您还应该始终在编译器中启用警告并仔细阅读。

标签: c variables while-loop


【解决方案1】:

您没有检查是否为这两个数字输入了有效数字,因为您没有检查 scanf 是否返回 1

while 中的测试有 '(' ')' 错误放置,对于 if

while 你比较 ans 之前阅读它,所以它的使用是一个未定义的行为,while 可以无限循环

scanf 读取操作不限制读取字符数,可以写出ans 甚至一个有效的操作名输入你都没地方记住最后的空字符

缺少执行操作的程序结尾


提案:

#include <stdio.h>
#include <string.h>

int main()
{
  int num1;
  int num2;
  char ans[4];

  printf("Enter first number: ");

  if (scanf("%i", &num1) != 1) {
    puts("invalid number");
    return -1;
  }

  printf("Enter second number: ");

  if (scanf("%i", &num2) != 1) {
    puts("invalid number");
    return -1;
  }

  for (;;) {
    printf("What kind of operation do you want to make? Type Sum, Sub, Mul or Div: ");
    if (scanf("%3s", ans) != 1)
      /* EOF */
      return -1;

    if (!strcmp(ans,"Sum")) {
      printf("%d+%d=%d\n", num1, num2, num1+num2);
      return 0;
    }
    if (!strcmp(ans,"Sub")) {
      printf("%d-%d=%d\n", num1, num2, num1-num2);
      return 0;
    }
    if (!strcmp(ans,"Mul")) {
      printf("%d*%d=%d\n", num1, num2, num1*num2);
      return 0;
    }
    if (!strcmp(ans,"Div")) {
      if (num2 == 0)
        puts("cannot divide by 0");
      else
        printf("%d/%d=%d\n", num1, num2, num1/num2);
      return 0;
    }
    puts("This command does not apply, please type the correct option");
  }
}

如您所见,我没有单独的 while 来仅检查输入操作是否有效,因为这样做之后您必须再次测试什么是输入操作来管理它。

编译和执行:

/tmp % gcc -pedantic -Wall -Wextra o.c
/tmp % ./a.out
Enter first number: 12
Enter second number: 34
What kind of operation do you want to make? Type Sum, Sub, Mul or Div: aze
This command does not apply, please type the correct option
What kind of operation do you want to make? Type Sum, Sub, Mul or Div: Sum
12+34=46
/tmp % ./a.out
Enter first number: aze
invalid number
/tmp %

【讨论】:

    【解决方案2】:
    while(!(
              strcmp(ans,"Sum") | (!(
              strcmp(ans,"Sub"))) | (!(
              strcmp(ans,"Mul"))) | (!(
              strcmp(ans,"Div"))) == 0));
    

    这毫无意义。首先,逻辑或写成|| 而不是|。其次,末尾的;,在这种情况下意味着永远循环,因为输入永远不会在循环内改变。您不应该使用 ; 而是使用 { ... } 并将其放在 printf 和 scanf 周围以形成围绕它们的循环。

    将其改写为:

    do
    {
      printf("What kind of operation do you want to make? Type Sum, Sub, 'Mul' or Div: ");
      scanf("%s", ans);
    }while( strcmp(ans,"Sum") && 
            strcmp(ans,"Sub") &&
            strcmp(ans,"Mul") &&
            strcmp(ans,"Div") );
    

    strcmp 如果字符串不匹配,则返回非零值,这意味着这个 C 可以用英文读为:
    “当输入不是 Sum 并且输入不是 Sub 并且输入不是 Mul 并且输入不是 Div 时,请务必要求输入”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 2015-03-21
      • 1970-01-01
      • 2018-03-12
      相关资源
      最近更新 更多