【问题标题】:Error 'sscanf_result' undeclared (first use in this function)未声明错误“sscanf_result”(在此函数中首次使用)
【发布时间】:2013-10-13 15:23:19
【问题描述】:

这是我的代码,我得到了这个错误

在函数'main'中:

41 1 [Error] 'scanf_result' 未声明(在此函数中首次使用)

41 1 [注意]每个未声明的标识符对于它出现的每个函数只报告一次

     #include <stdio.h>
            #include <stdlib.h>


        int main ( int argc, char *argv[] )

      {
        int n;
          float fl,fw,wh,ww,dh,dw;
         float p,t;
        float tl,tw,a;
        float p1,p2,p3;
          printf("What's the width of the floor?");
          scanf ("%d", &fw);
          printf("What's the length of the floor?");
          scanf ("%d", &fl);
             printf("What's the height of the wall?");
              scanf ("%d", &wh);
               printf("What's the width of the wall?");
          scanf ("%d", &ww);
         printf("What's the width of the door?");
          scanf ("%d", &dw);
         printf("What's the height of the door?");
         scanf ("%d", &dh);
          a=fw*fl+(wh*ww)*3-(dh*dw);
      p1=a*22;
         p2=a*23.80;
          p3=a*14;

            char line[100];
          int answer; 
        answer = -1;
        while (answer != 0) 
       {
        printf ("\nWhat tiles do you want?:\n");
          printf (" [1] 20sm X 30sm.");
        printf (" [2] 30sm X 41,6sm");
         printf (" [3] 25sm X 33sm");
         printf ("\nWhat do you want to do? [0 for nothing] ");
          fgets (line, sizeof(line), stdin); 
              scanf_result = scanf (line, "%d", &answer);
            if ((scanf_result == 0) | (scanf_result == EOF))
           {
               printf ("\n *** 1 - 2 or 3! ***\n");
           answer = -1; 
          }
           switch (answer)
        {
                   case 0:
          break;
         case 1:
               printf(" Total price = %.2f lv \n",p1);
         break;
          case 2:
        printf(" Total price = %.2f lv \n",p2);
             break;
           case 3:
             printf(" Total price = %.2f lv \n",p3);
            break;
             default: 
            break;
              }

        }

              system("PAUSE");
           return 0;    
         }

【问题讨论】:

  • 修复代码格式和缩进。
  • 请不要破坏你自己的问题。

标签: c function scanf


【解决方案1】:

正如错误消息清楚地描述的那样,您的程序中没有scanf_result 变量的声明。

放一个:

int scanf_result;

在您的声明部分修复错误。

【讨论】:

    【解决方案2】:
          scanf_result = scanf (line, "%d", &answer);
    

    您的scanf() 用法错误。你需要sscanf()

    scanf() 函数应从标准输入流stdin 中读取。 sscanf() 函数应从字符串 line 中读取。

    [Error] 'scanf_result' 未声明(在此函数中首次使用)

    错误描述了您缺少的内容。

    声明scanf_result

    sscanf() 返回整数

    声明为整数变量。

    【讨论】:

      猜你喜欢
      • 2016-05-09
      • 2018-10-03
      • 2015-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多