Visual C++ Developer Center > Visual C++ Forums > Visual C++ Language > Receiving C2447 error - unable to correct error
Ask a questionAsk a question
 

AnswerReceiving C2447 error - unable to correct error

  • Sunday, December 02, 2007 1:38 AMNew to C__ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

    }

Answers

  • Sunday, December 02, 2007 1:48 AMAndreas Masur Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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

    }


  • Sunday, December 02, 2007 5:47 PMAndreas Masur Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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);

      }

    }


  • Monday, December 03, 2007 9:55 AMPintu Shukla Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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);

      }

    }


All Replies

  • Sunday, December 02, 2007 1:48 AMAndreas Masur Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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

    }


  • Sunday, December 02, 2007 1:58 AMNew to C__ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

    }

     

     

  • Sunday, December 02, 2007 2:06 AMAndreas Masur Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     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... 
  • Sunday, December 02, 2007 5:07 PMNew to C__ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Sunday, December 02, 2007 5:47 PMAndreas Masur Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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);

      }

    }


  • Sunday, December 02, 2007 9:44 PMNew to C__ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

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

     

    A good lesson learned.

     

    Thanks very much!

     

    Chris

  • Monday, December 03, 2007 9:55 AMPintu Shukla Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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);

      }

    }


  • Saturday, December 08, 2007 4:22 AMVoltronx Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    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.
  • Saturday, December 08, 2007 12:18 PMPintu Shukla Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     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