2014-11-24 112 views
0

我对这段代码有几个问题。我在代码的最后列出了错误。编译C代码时出错

#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:语法错误:缺少 ';'之前的“type” 错误C2065:FUNC':未声明的标识符 错误C2109:下标要求数组或指针类型

+2

请缩进你的代码,因为如果你用脚写代码就会发生类似这样的错误。 – bitcell 2014-11-24 07:13:51

+1

搞笑:-)顺便说一句,@DonCarter,你可以请upvote那些谁帮助你?这是标准做法,不仅要奖励他们,而且要向其他人表明实际解决问题的同样的问题。你可以upvote,也可以接受一个答案, – Mawg 2014-11-24 09:15:39

+0

与一些UniCell不同,我没有天生的编写代码的能力。我发布的代码是Word中的复制和粘贴,它并不总是在写入时进行转置。初学者应该有更多的耐心。我只在这里待了三个星期! – 2014-11-25 01:29:08

回答

0

在我已经从您的代码贴在下面行,您正在结束与所述第二主要方法支撑。 这不是一个好主意,因为你在代码下面有代码。

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

} //end main method 
0

代码编译和工作 - 看it in action here

几点:

  • 这是C代码,不C#
  • return(0);置于while循环中 - 这将防止它要求用户进行多次运行。将此移动到while循环结束的下方。
  • 阵列保持的各种函数指针到算术运算符的声明不应该被定位在while循环内 - 它不每次迭代之间变化 - 即移动void(*func[4])(double, double) = { &addition, &subtraction, &division, &multiplication };上述while
  • 更好缩进将使代码更具可读性
  • int number1; int number2; int answer;的最上面的声明是多余的,应该删除(特别是因为它们在4个算术函数中用作不同类型的本地变量名称)。

我已到代码段中的上述变化(scanf_s用简单scanf替换为IdeOne不使用MS编译器)。

-1
// 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); 
} 
+0

这工作很好,谢谢你和其他人的投入。我已经并将继续阅读您的回复,以便避免将来出现此类错误。再次感谢! – 2014-11-25 01:22:38