Microsoft Developer Network > Página principal de foros > Visual C++ Language > Receiving C2447 error - unable to correct error
Formular una preguntaFormular una pregunta
 

RespondidaReceiving C2447 error - unable to correct error

  • domingo, 02 de diciembre de 2007 1:38New to C__ Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     

    I am a new to C++ and I am completing an exercise in C++.  I have read my text and online materials to see if I can correct the error.  I have tried everything that I know to correct the problem, but no luck.

    I don't understand what is meant by missing function header?  This is the error details: error C2447: '{' : missing function header (old-style formal list?) which occurs a line 60 (the red bracket below). 

     

    Any guidance is helpful.

    Chris  

     

    #include "stdafx.h"

    #include <iostream>

    using namespace std;

    int binomial ( int n, int k); // function prototype

    int _tmain(int argc, _TCHAR* argv[])

    {

    int n, k ; // parameters for the binomial number

    int result ;

    cout << endl ;

    // read in n & k

    cout << "Enter n (positive integer) : " ;

    cin >> n ;

    cout << "Enter k (positive integer) : " ;

    cin >> k ;

    result = binomial(n, k) ;

    cout << "Binomial number " << n << "C" << k

    << " = " << result << endl ;

    return (0) ;

    }

    // ********************************************************

    int binomial ( int n, int k ) ;

    /* Computes the binomial coefficient nCk */

    /* */

    /* Inputs: */

    /* n, k (integers) */

    /* */

    /* Output: */

    /* binomial coefficient nCk (integer) */

    {

    int numerator, denominator ;

    int i ; // needed to compute numerator & denominator <![endif]>

    if (n < k)

    {

    return(0) ;

    }

    else

    {

    denominator = 1 ; // initial value <![endif]>

    for (i = 1 ; i <= k ; i = i+1)

    denominator = denominator * 1 ; <![endif]>

    for (i = n-k+1 ; i <= n ; i = i+1)

    numerator = numerator * i ; <![endif]>

    return (numerator/denominator) ;

    } // else

    }

Respuestas

  • domingo, 02 de diciembre de 2007 1:48Andreas Masur Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    Simply remove the ';' sign at the end of your definition for 'binomial'...

    // ********************************************************

    int binomial ( int n, int k ) ;                              <-- Remove the ';' right here

    /* Computes the binomial coefficient nCk */

    /* */

    /* Inputs: */

    /* n, k (integers) */

    /* */

    /* Output: */

    /* binomial coefficient nCk (integer) */

    {

    int numerator, denominator ;

    int i ; // needed to compute numerator & denominator <![endif]>

    if (n < k)

    {

    return(0) ;

    }

    else

    {

    denominator = 1 ; // initial value <![endif]>

    for (i = 1 ; i <= k ; i = i+1)

    denominator = denominator * 1 ; <![endif]>

    for (i = n-k+1 ; i <= n ; i = i+1)

    numerator = numerator * i ; <![endif]>

    return (numerator/denominator) ;

    } // else

    }


  • domingo, 02 de diciembre de 2007 17:47Andreas Masur Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    Well...you have some '<![endif]>' clauses in your code that the compiler certainly cannot understand. Please find below a cleaned-up version of your function:

    Code Block

    int binomial( int n, int k )

    {

      int numerator, denominator;

      int i;


      int(n < k)

      {

        return (0);

      }

      else

      {

        denominator = 1;

        for( i = 1; i <= k; i = i + 1)

          denominator = denominator * i;


        for( i = n-k+1; i <= n; i = i + 1)

          numerator = numerator * i;


        return (numerator/denominator);

      }

    }


  • lunes, 03 de diciembre de 2007 9:55Pintu Shukla Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    Hello Andreas ,
    This code contain few error .have a look on the bold Line

     Andreas Masur wrote:
    Well...you have some '<![endif]>' clauses in your code that the compiler certainly cannot understand. Please find below a cleaned-up version of your function:

    Code Block

    int binomial( int n, int k )

    {

      int numerator, denominator;

      int i;


      int(n < k) // i think it is if(n<k) instead of int

      {

        return (0);

      }

      else

      {

        denominator = 1;

        for( i = 1; i <= k; i = i + 1)

          denominator = denominator * i;


        for( i = n-k+1; i <= n; i = i + 1)

          numerator = numerator * i; // Unexpected result .Because numerator is not initialized


        return (numerator/denominator);

      }

    }


Todas las respuestas

  • domingo, 02 de diciembre de 2007 1:48Andreas Masur Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    Simply remove the ';' sign at the end of your definition for 'binomial'...

    // ********************************************************

    int binomial ( int n, int k ) ;                              <-- Remove the ';' right here

    /* Computes the binomial coefficient nCk */

    /* */

    /* Inputs: */

    /* n, k (integers) */

    /* */

    /* Output: */

    /* binomial coefficient nCk (integer) */

    {

    int numerator, denominator ;

    int i ; // needed to compute numerator & denominator <![endif]>

    if (n < k)

    {

    return(0) ;

    }

    else

    {

    denominator = 1 ; // initial value <![endif]>

    for (i = 1 ; i <= k ; i = i+1)

    denominator = denominator * 1 ; <![endif]>

    for (i = n-k+1 ; i <= n ; i = i+1)

    numerator = numerator * i ; <![endif]>

    return (numerator/denominator) ;

    } // else

    }


  • domingo, 02 de diciembre de 2007 1:58New to C__ Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     

    Thanks for the reply, but now I have additional errors regarding missing ';" before <

    Simply remove the ';' sign at the end of your definition for 'binomial'...

    // ********************************************************

    int binomial ( int n, int k ) ;                              <-- Remove the ';' right here

    /* Computes the binomial coefficient nCk */

    /* */

    /* Inputs: */

    /* n, k (integers) */

    /* */

    /* Output: */

    /* binomial coefficient nCk (integer) */

    {

    int numerator, denominator ;

    int i ; // needed to compute numerator & denominator <![endif]>

    if (n < k)

    {

    return(0) ;

    }

    else

    {

    denominator = 1 ; // initial value <![endif]>

    for (i = 1 ; i <= k ; i = i+1)

    denominator = denominator * 1 ; <![endif]>

    for (i = n-k+1 ; i <= n ; i = i+1)

    numerator = numerator * i ; <![endif]>

    return (numerator/denominator) ;

    } // else

    }

     

     

  • domingo, 02 de diciembre de 2007 2:06Andreas Masur Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
     C++ Student wrote:

    Thanks for the reply, but now I have additional errors. 


    Well...and the errors are? The removal only fixes the error you posted about before. Certainly any other error in your code following this ';' sign is still there and raises not error since the first one is fixed... 
  • domingo, 02 de diciembre de 2007 17:07New to C__ Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     

    I reviewed the error codes with the online help.  Tried to correct the errors, but with no success.

    #include "stdafx.h"

    #include <iostream>

    using namespace std;

    int binomial ( int n, int k) ; // function prototype

    int _tmain(int argc, _TCHAR* argv[])

    {

    int n, k ; // parameters for the binomial number

    int result ;

    cout << endl ;

    // read in n & k

    cout << "Enter n (positive integer) : " ;

    cin >> n ;

    cout << "Enter k (positive integer) : " ;

    cin >> k ;

    result = binomial(n, k) ;

    cout << "Binomial number " << n << "C" << k

    << " = " << result << endl ;

    return (0) ;

    }

    // ********************************************************//

    int binomial ( int n, int k )

    /* Computes the binomial coefficient nCk */

    /* */

    /* Inputs: */

    /* n, k (integers) */

    /* */

    /* Output: */

    /* binomial coefficient nCk (integer) */

    {

    int numerator, denominator ;

    int i ; // needed to compute numerator & denominator <![endif]>

    if (n < k)

    {

    return(0) ;

    }

    else

    {

    denominator = 1 ; // initial value <![endif]>

    for ( i = 1 ; i <= k ; i = i + 1)

    denominator = denominator * i;<![endif]>

    for (i = n-k+1 ; i <= n ; i = i + 1) //line 76.  Four errors:  warning C4552: '<=' : operator has no effect; expected operator with side-effect;

    error C2143: syntax error : missing ';' before ')';  error C2143: syntax error : missing ';' before ')';  error C2143: syntax error : missing ';' before '<'

    numerator = numerator * i;<![endif]>

    return (numerator/denominator);

    } // else

    }

     

    Thanks,

    Chris

  • domingo, 02 de diciembre de 2007 17:47Andreas Masur Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    Well...you have some '<![endif]>' clauses in your code that the compiler certainly cannot understand. Please find below a cleaned-up version of your function:

    Code Block

    int binomial( int n, int k )

    {

      int numerator, denominator;

      int i;


      int(n < k)

      {

        return (0);

      }

      else

      {

        denominator = 1;

        for( i = 1; i <= k; i = i + 1)

          denominator = denominator * i;


        for( i = n-k+1; i <= n; i = i + 1)

          numerator = numerator * i;


        return (numerator/denominator);

      }

    }


  • domingo, 02 de diciembre de 2007 21:44New to C__ Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     

    Yes, the <![endif]> clauses were the problem.  The code ran when the <![endif]> clauses were removed.

     

    A good lesson learned.

     

    Thanks very much!

     

    Chris

  • lunes, 03 de diciembre de 2007 9:55Pintu Shukla Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    Hello Andreas ,
    This code contain few error .have a look on the bold Line

     Andreas Masur wrote:
    Well...you have some '<![endif]>' clauses in your code that the compiler certainly cannot understand. Please find below a cleaned-up version of your function:

    Code Block

    int binomial( int n, int k )

    {

      int numerator, denominator;

      int i;


      int(n < k) // i think it is if(n<k) instead of int

      {

        return (0);

      }

      else

      {

        denominator = 1;

        for( i = 1; i <= k; i = i + 1)

          denominator = denominator * i;


        for( i = n-k+1; i <= n; i = i + 1)

          numerator = numerator * i; // Unexpected result .Because numerator is not initialized


        return (numerator/denominator);

      }

    }


  • sábado, 08 de diciembre de 2007 4:22Voltronx Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     

     

    I would like to add an error message similar to the one below that would stop the program and recall the function if the problem would result in an overflow error. 

     

    if (n < k)

    {

    cout <<"ERROR!! n must be larger than k."<<endl ;

    return (0) ;

    }

     

    Your input would be greatly appreciated.
  • sábado, 08 de diciembre de 2007 12:18Pintu Shukla Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
     Voltronx wrote:

     

    I would like to add an error message similar to the one below that would stop the program and recall the function if the problem would result in an overflow error. 

     

    if (n < k)

    {

    cout <<"ERROR!! n must be larger than k."<<endl ;

    return (0) ;

    }

     

    Your input would be greatly appreciated.


    Hello Voltronx this will defenately show you the message but but will not recall the function .if you want that your program will work according to your logic .then you should use a do..while loop inside your program like shown in the following example

    Code Block

    do{

    //write your code here

    }while(n<k);



    Thanx