【问题标题】:Errors during compilation of C codeC 代码编译期间的错误
【发布时间】:2015-01-21 18:54:48
【问题描述】:

我在使用此代码时遇到了一些问题。我已在代码末尾包含错误。

#include <stdio.h>

int main()

{
void addition(double number1, double number2); /* create the functions */
void subtraction(double number1, double number2);
void division(double number1, double number2);
void multiplication(double number1, double number2);
int inputfunc=1;
double inputnum1=0;
double inputnum2=0;
int number1;
int number2;
int answer;

while (inputfunc >= 1 && inputfunc <= 4) /* If function to be performed are those   below then continue performing loop */

{
printf("Press 1 to add two numbers.\n");
printf("Press 2 to subtract two numbers.\n");
printf("Press 3 to multiply two numbers.\n");
printf("Press 4 to divide two numbers.\n");
printf("Press 5 to exit.\n");
printf("Enter your choice\n");
scanf_s("%d", &inputfunc);

if( inputfunc == 5) /* Exit program if requested via 5 function */
return(0);

printf("Enter both numbers with a space in between.");
scanf_s("%lf %lf", &inputnum1, &inputnum2);

void(*func[4])(double, double)={&addition, &subtraction, &division, &multiplication};
  (*func[inputfunc-1])(inputnum1, inputnum2); 
  return(0);
  }

}

void addition(double number1, double number2) 
{
  double answer; 
  answer=number1+number2;
  printf("Addition of the two numbers = %lf + %lf = %lf\n", number1, number2, answer); 
  return;
} 

void subtraction(double number1, double number2)
{ 
  double answer; 
  answer=number1-number2;
  printf("By subtracting the two numbers results are %lf - %lf = %lf\n", number1,
    number2, answer); 
  return;
} 

void multiplication(double number1, double number2) 
{ 
  double answer; 
  answer=number1*number2;
  printf("By multiplying the two numbers results are %lf * %lf = %lf\n", number1, 
      number2, answer); 
  return;
} 

void division(double number1, double number2) 
{ 
  double answer; 
  answer=number1/number2;
  printf("By dividing the two numbers results are %lf / %lf = %lf\n", number1, 
        number2,  answer); 
  return ;
}

错误 C2143:语法错误:缺少 ';'在“类型”之前 错误 C2065:“func”:未声明的标识符 错误C2109:下标需要数组或指针类型

【问题讨论】:

  • 请缩进你的代码,因为如果你用脚写代码就会发生这样的错误。
  • 搞笑 :-) 顺便说一句,@DonCarter,你能投票给那些帮助过你的人吗?这是标准做法,不仅要奖励他们,还要向遇到相同问题的其他人指出实际解决问题的方法。您可以投票也可以接受一个答案,
  • 与一些 UniCell 不同,我不是天生就有写代码的能力。我发布的代码是从 Word 复制和粘贴的,在编写时并不总是转置。你应该对初学者有更多的耐心。我只在这里呆了三个星期!

标签: c arrays


【解决方案1】:

在我从您的代码下面发布的那一行中,您将用第二个大括号结束 main 方法。 这不是一个好主意,因为你在那个大括号下面有代码。

void(*func[4])(double, double)={&addition, &subtraction, &division, &multiplication};
(*func[inputfunc-1])(inputnum1, inputnum2); 
return(0);
} //end while

}  //end main method

【讨论】:

    【解决方案2】:

    代码编译并工作 - 见it in action here

    几点:

    • 这是 C 代码,不是 C#
    • return(0); 被放置在while 循环中 - 这将防止它向用户询问不止一次运行。将此移动到 while 循环的末尾下方。
    • 包含算术运算符的各种函数指针的数组的声明不应放置在 while 循环中 - 它不会在每次迭代之间更改 - 即将 void(*func[4])(double, double) = { &amp;addition, &amp;subtraction, &amp;division, &amp;multiplication }; 移动到 while 上方
    • 更好的缩进将使代码更具可读性
    • int number1; int number2; int answer; 的最顶层声明是多余的,应删除(尤其是在 4 个算术函数中用作具有不同类型的局部变量名称时)。

    我对 sn-p 进行了上述更改(scanf_s 被简单的 scanf 替换,因为 IdeOne 不使用 MS 编译器)。

    【讨论】:

      【解决方案3】:
      // the scanf_s function is the secure function for string input, using scanf
      
      // this version compiles without any warnings, etc under linux gcc
      
      // this version checks for input errors 
      // (except the actual value of the variable inputFunc)
      // I think the use of a switch() statement would be much more robust
      // rather than the use of the function ptr table, although not quite as flexable
      
      // Notice the function prototypes are outside of any function
      // so the compiler will create the proper code
      
      #include <stdio.h>
      #include <stdlib.h> // contains exit() and EXIT_FAILURE
      
      // function prototypes
      void addition(double number1, double number2);
      void subtraction(double number1, double number2);
      void division(double number1, double number2);
      void multiplication(double number1, double number2);
      
      
      void(*func[4])(double, double)={&addition, &subtraction, &division, &multiplication};
      
      int main()
      {
      
          int inputFunc=1;
          double inputnum1=0;
          double inputnum2=0;
      
          /* If function to be performed are those   
             below then continue performing loop */
      
          // note:
          // if the inputFunc is (for instance) 6 then this while loop is exited
          // however, there was no return statement
          // for that execution path
      
          while (inputFunc >= 1 && inputFunc <= 4) 
          {
              printf("Press 1 to add two numbers.\n");
              printf("Press 2 to subtract two numbers.\n");
              printf("Press 3 to multiply two numbers.\n");
              printf("Press 4 to divide two numbers.\n");
              printf("Press 5 to exit.\n");
              printf("Enter your choice\n");
      
              if( 1 != scanf(" %d", &inputFunc) )
              {
                  perror( "scanf_s" );
                  exit( EXIT_FAILURE ) ;
              }
      
              // implied else, scanf for which command was successful
      
              // note: there should be some checking here 
              //       to assure that the input was in the valid range '1...5'
      
              if( inputFunc == 5)
              { /* then, Exit while loop if requested via 5 function */
                // note: good program practice is to put the return
                //       at the bottom of the function
                   break;
              }
      
              printf("Enter both numbers with a space in between.");
              if( 2 != scanf(" %lf %lf", &inputnum1, &inputnum2) )
              {
                  perror( "scanf for 2 input numbers" );
                  exit(EXIT_FAILURE);
              }
      
              // implied else, scanf for two input numbers successful
      
              // exec the desired function
              (*func[inputFunc-1])(inputnum1, inputnum2);
          }
          return(0); // to avoid compiler warning
      }
      
      void addition(double number1, double number2)
      {
        double answer;
        answer=number1+number2;
        printf("Addition of the two numbers = %lf + %lf = %lf\n", number1, number2, answer);
        return;
      }
      
      void subtraction(double number1, double number2)
      {
          double answer;
          answer=number1-number2;
          printf("By subtracting the two numbers results are %lf - %lf = %lf\n",
              number1,
              number2,
              answer);
      }
      
      void multiplication(double number1, double number2)
      {
          double answer;
          answer=number1*number2;
          printf("By multiplying the two numbers results are %lf * %lf = %lf\n",
              number1,
              number2,
              answer);
      }
      
      void division(double number1, double number2)
      {
          // note:  this should check that number2 is NOT 0, to avoid divide by zero error
          double answer;
          answer=number1/number2;
          printf("By dividing the two numbers results are %lf / %lf = %lf\n",
              number1,
              number2,
              answer);
      }
      

      【讨论】:

      • 这很好,感谢您和其他所有人的投入。我已经并将继续阅读您的回复,这样我就可以避免将来出现这种性质的错误。再次感谢!
      猜你喜欢
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      相关资源
      最近更新 更多