2014-11-24 3 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 : 구문 오류 : 누락 ';' 'FUNC': 선언되지 않은 식별자 오류 C2109 : '형식' 오류 C2065 전 첨자는, 당신은 두 번째와 주요 방법을 종료하는 내가 코드에서 아래 게시 한 줄 이상 배열 또는 포인터 타입

+2

발로 코드를 작성하면 이러한 실수가 일어나기 때문에 코드를 들여 쓰기 바랍니다. – bitcell

+1

들썩 들썩 한 :-) Btw, @DonCarter, 당신을 도운 사람들을 위로 해주시겠습니까? 보상을주는 것뿐만 아니라 동일한 문제가있는 다른 사람들에게 실제로 그것을 해결 한 것을 알려주는 것이 표준 관행입니다. 당신은 하나의 대답을 upvote 또한 수락, – Mawg

+0

일부 UniCell 달리, 나는 코드를 작성하는 능력으로 태어난되지 않았습니다. 내가 게시 한 코드는 Word에서 복사하여 붙여 넣기 한 것으로 작성된대로 항상 바뀌지는 않습니다. 초보자들에게 인내심을 가져야합니다. 나는 단지 3 주 동안이 일을했다! –

답변

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#

  • return(0);while 루프 내에 배치되는 C 코드가 아닌

    • 을 그것은 하나 이상의 실행에 대해 사용자에게 묻지 않습니다. 루프를 while 루프 끝까지 이동합니다.
    • 산술 연산자의 다양한 함수 포인터를 유지 어레이의 선언은 while 루프 내에 배치되어서는 안된다 - 즉 while
    • 나은 압입가 만들 것 이상으로 void(*func[4])(double, double) = { &addition, &subtraction, &division, &multiplication }; 이동 -이 각 반복 사이에서 변화하지 않는다 코드 가독성
    • int number1; int number2; int answer;의 최상위 선언은 중복되므로 제거해야합니다 (특히 4 가지 산술 함수에서 다른 유형의 로컬 변수 이름으로 사용됨).

    나는 조각에 위의 변경을했습니다 (IdeOne는 MS 컴파일러를 사용하지 않는 scanf_s 간단한 scanf로 대체).

  • -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

    이것은 아름답게 작동했습니다. 여러분과 다른 모든 사람들이 의견을 보내 주셔서 감사합니다. 나는이 성질의 미래 오류를 피할 수 있도록 당신의 답장을 읽었으며 계속 읽을 것입니다. 다시 한 번 감사드립니다! –

    관련 문제