Receiving C2447 error - unable to correct error
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 prototypeint
_tmain(int argc, _TCHAR* argv[]){
int n, k ; // parameters for the binomial number int result ;cout << endl ;
// read in n & kcout <<
"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}
答案
- 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
}
- 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);
}
}
- 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 Blockint 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);
}
}
全部回复
- 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
}
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
}
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...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 prototypeint
_tmain(int argc, _TCHAR* argv[]){
int n, k ; // parameters for the binomial number int result ;cout << endl ;
// read in n & kcout <<
"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
- 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);
}
}
Yes, the <![endif]> clauses were the problem. The code ran when the <![endif]> clauses were removed.
A good lesson learned.
Thanks very much!
Chris
- 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 Blockint 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);
}
}
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.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 Blockdo{
//write your code here
}while(n<k);
Thanx

