Help with a 3 part program dealing with functions and factorials

The purpose of this program is to build a function to check inputs a function to calculate factorials and a function to calculate combinations. I thought I had it figured out but when I compile it I get these errors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 fact.cpp:31: parse error before `-' token
fact.cpp: In function `long unsigned int fact(...)':
fact.cpp:34: `int fact' redeclared as different kind of symbol
fact.cpp:31: previous declaration of `long unsigned int fact(...)'
fact.cpp:36: parse error before `do'
fact.cpp:38: syntax error before `>=' token
fact.cpp:38: syntax error before `--' token
fact.cpp:47: redefinition of `int fact'
fact.cpp:34: `int fact' previously defined here
fact.cpp:49: parse error before `do'
fact.cpp:51: syntax error before `>=' token
fact.cpp:51: syntax error before `--' token
fact.cpp:59: ISO C++ forbids declaration of `n' with no type
fact.cpp:59: `a' undeclared (first use this function)
fact.cpp:59: (Each undeclared identifier is reported only once for each 
   function it appears in.)
fact.cpp:60: ISO C++ forbids declaration of `r' with no type
fact.cpp:60: redefinition of `int r'
fact.cpp:46: `int r' previously declared here
fact.cpp:60: `b' undeclared (first use this function)
fact.cpp:61: parse error before `-' token
fact.cpp: In function `long unsigned int comb(int, int, int)':
fact.cpp:70: parse error before `!' token
fact.cpp: In function `int main()':
fact.cpp:88: ISO C++ forbids comparison between pointer and integer
fact.cpp:89: parse error before `,' token
fact.cpp:93: `c' undeclared (first use this function)
fact.cpp:95: warning: the address of `long unsigned int comb(int, int, int)', 
   will always be `true'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
 // --------------------------------------------------------------------
// File name:   simpleMath.cpp
// Assign ID:   LAB10
// Due Date:    11/07/12 at 7:30 pm 
//
// Purpose:     Performs a simple addiion between 2 items.
//
// Author:      bfields Byron Fields
// --------------------------------------------------------------------


#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

// -----------------------------------------------------------------
// Function Name:  input_check
// Purpose:	   Check to make sure n > r.
//                 
// -----------------------------------------------------------------

bool input_check(int n, int r)
{
if( n > r)
 return 1;
else 
   return 0;
}

// -----------------------------------------------------------------
// Function Name:  fact
// Purpose:	   calculate Factorial of inputs. return values used to call //function comb
//                 
// -----------------------------------------------------------------
unsigned long int fact(int n, int r, int (n-r))
{
int n;
int fact = 1;

do 
{
for (n;n>=1;n--)
{

fact = fact *n;

}
}while(n != 0);

int r;
int fact = 1;

do 
{
for (r;r>=1;r--)
{

fact = fact *r;

}
}while(r != 0);

n = a;
r = b;
(n-r) = c;


}
 

// -----------------------------------------------------------------
// Function Name:  comb
// Purpose:	   used to calculate Combination of values
//                 
// -----------------------------------------------------------------

unsigned long int comb(int a, int b, int c)
{
int comb = a!/(r!*(n-r)!);

return comb;
}

int main()
{

cout << "(c) 2012, bfields Byron Fields" << endl;

cout << "Enter the n value" << endl;
cin >> n;

cout << "Enter the r value" << endl;
cin >> r;

bool input_check(int n, int r);

if(input_check == 1)
  fact(int n, int r, int (n-r));
else
  cout << "Wrong Input" << endl;

comb(a,b,c);

cout << comb << endl;



cout << "(c) 2012, bfields Byron Fields" << endl;


    
return 0;
}


If needed these are the instructions my TA gave:

1
2
3
4
5
The main function should only prompt the user for two numbers, storing them in two variables named n and r respectively. These variables should be integers.

The first of the three functions should be called input_check. This function must check to make sure n>r. If n>r then your funciton should return a 1 to the main function. If n < r this means inputs are bad and function should return 0 to main. C(n,r) only works when n > r.

If first function returns a 1 to main then main function should call function fact. Fact should calculate the factorials with three values n, r, and (n-r). The return values should be stored in 3 different variables and these variables should be used to call the function comb. 
can anybody give me some input?
Line 36:
 
unsigned long int fact(int n, int r, int (n-r))

int (n-r) is not a valid argument.

Line 38: You can't declare int n here. n is already declared as an argument.

Line 51: same with int r here.

Line 52: int fact has already been declared at line 39. You can't declare the same variable multiple times in the same scope.

Lines 64-66: a,b,c are undefined.

Line 80:
 
int comb = a!/(r!*(n-r)!);
the compiler doesn't recognize !. That's not a valid operator.

Lines 91, 94: n and r are undefined.

Line 96-98:
1
2
3
bool input_check(int n, int r);

if(input_check == 1)

You declared input_check as a function returning a bool. You can't compare a bool to 1, only true or false. Also, input_check is a function, therefore you must call it in the if statement and pass it the required arguments.
 
if(input_check(n,r))

Note: Declaring input_check as a bool function does not agree with your instructions which state that input_check shout return 1 or 0.

Line 98:
 
fact(int n, int r, int (n-r));

You can't declare types in a function call.

The compiler diagnostics tell you all this if you read them carefully.





Line 96 does nothing since it's coded as a function prototype. And on line 98 it looks like you are trying to call it, but what you are actually doing is testing the address of input_check since, like arrays, the name of a function without the parentheses to indicate a function call is actually the address of function. That's why the warning for line 95 is there. Since the address of a function will never be 0, testing the address will always be true. But it will also never == 1. When you return a value from a function you either have to assign it to a variable or use it in place. What you could do is simply remove line 96 and write 98 like this: if(input_check(int n, int r) == 1). This will call the function passing n and r to it, return the bool value, then check to see if it was true or false. Also, you aren't actually returning or testing the bool value but an int value. The compiler understands that 0 = false and 1 = true however and makes the conversion for you. Not a big deal really; it's just that using TRUE and FALSE might be a bit clearer.

On line 103 you are calling comb with the arguments a, b, c yet I don't see anywhere that you are declaring them before the function call.

And you're fact function has a few errors. I don't believe you can actually evaluate an expression in a functions declaration so using int(n-r) there won't work. If you want that value simply do it within the function since you are already passing both n and r to it. Then on line 38 you are declaring n again. The function declaration declares any variables you name in it, so int n and int r are already declared by the function declaration. Additionally, in the for loop, the first part inside the parentheses is a partial declaration of n again. Since n has already been declared you can leave that blank, but you still have to show the semi colon. Same with r at lines 54 and 56. And on lines 64 to 66 you are using int a, b and c again without declaring them. Then you are assigning the values of a and b to the variables you just worked on, thereby destroying any work you just did on n and r. Assignments work from right to left. So you are saying here, take the value contained in variable a and assign it to n. Lastly, you can't have an expression on the left side of an assignment statement. line 66 should look like c = n-r. But like I said, you would need to declare a, b, and c first.

Hopefully that should get you started down the right path.
thanks you guys. im almost out the tunnel now.

only problem im having now is with the fact function and comb function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
 // --------------------------------------------------------------------
// File name:   simpleMath.cpp
// Assign ID:   LAB10
// Due Date:    11/07/12 at 7:30 pm 
//
// Purpose:     Performs a simple addiion between 2 items.
//
// Author:      bfields Byron Fields
// --------------------------------------------------------------------


#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

// -----------------------------------------------------------------
// Function Name:  input_check
// Purpose:	   Check to make sure n > r.
//                 
// -----------------------------------------------------------------

int input_check(int n, int r)
{
if( n > r)
 return 1;
else 
   return 0;
}

// -----------------------------------------------------------------
// Function Name:  fact
// Purpose:	   calculate Factorial of inputs. return values used to call //function comb
//                 
// -----------------------------------------------------------------

unsigned long int fact(int n, int r)
{

int factn = 1;

do 
{
for (;n>=1;n--)
{

factn = fact *n;

}
}while(n != 0);


int factr = 1;

do 
{
for (;r>=1;r--)
{

factr = fact *r;

}
}while(r != 0);

int a = factn;
int b = factr;
int c = n-r;


}
 

// -----------------------------------------------------------------
// Function Name:  comb
// Purpose:	   used to calculate Combination of values
//                 
// -----------------------------------------------------------------

unsigned long int comb(int a, int b, int c)
{
int comb = a/(b*c);

return comb;
}

int main()
{
int n,r,a,b,c;

cout << "(c) 2012, bfields Byron Fields" << endl;

cout << "Enter the n value" << endl;
cin >> n;

cout << "Enter the r value" << endl;
cin >> r;


if(input_check(n,r) == 1)
  fact(n,r);
else
  cout << "Wrong Input" << endl;

comb(a,b,c);

cout << comb << endl;



cout << "(c) 2012, bfields Byron Fields" << endl;


    
return 0;
}


these are the errors:
1
2
3
4
5
6
7
8
9
dact.cpp: In function `long unsigned int fact(int, int)':
dact.cpp:41: invalid operands of types `long unsigned int ()(int, int)' and `
   int' to binary `operator*'
dact.cpp:54: invalid operands of types `long unsigned int ()(int, int)' and `
   int' to binary `operator*'
dact.cpp: In function `int main()':
dact.cpp:95: warning: the address of `long unsigned int comb(int, int, int)', 
   will always be `true'
' 


im sure those are simple errors but unfortunately Im not as strong with definitions as I should be.
On line 104 you are again calling a function without assigning the return to a variable. While you did declare comb as a variable in the comb() function it only has scope INSIDE the function. Therefore, after you call it and it returns comb, comb no longer exists. So you have to declare another variable to hold the value returned by comb() to output it. Also, I'd use a different name for int comb since you already have a function named that. Just helps to differentiate between the 2.
okay I got it to compile now problem, but when I run it it does not ask for any of my inputs and just does the factorial of one number ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
 // --------------------------------------------------------------------
// File name:   simpleMath.cpp
// Assign ID:   LAB10
// Due Date:    11/07/12 at 7:30 pm 
//
// Purpose:     Performs a simple addiion between 2 items.
//
// Author:      bfields Byron Fields
// --------------------------------------------------------------------


#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;


int input_check(int n, int r)
{
if( n > r)
 return 1;
else 
   return 0;
}

unsigned long int fact(int n, int r)
{

int factn = 1;

do 
{
for (;n>=1;n--)
{

factn = fact *n;
int a = factn;

}
}while(n != 0);


int factr = 1;

do 
{
for (;r>=1;r--)
{

factr = fact *r;
int b = factr;

}
}while(r != 0);



int c = n-r;


}
 

unsigned long int comb(int a, int b, int c)
{
int comb = a/(b*c);

return comb;
}

int main()
{
int n,r,a,b,c,d;

cout << "(c) 2012, bfields Byron Fields" << endl;

cout << "Enter the n value" << endl;
cin >> n;

cout << "Enter the r value" << endl;
cin >> r;


if(input_check(n,r) == 1)
  fact(n,r);
else
  cout << "Wrong Input" << endl;

d = comb(a,b,c);

cout << d << endl;



cout << "(c) 2012, bfields Byron Fields" << endl;


    
return 0;
}
You still have compile errors in the code you posted:

Line 36:
 
factn = fact *n;

You're referring to the address of the function (fact). Not what you want.

Line 50. Same problem.
Okay I got the code to compile completely error free now but I'm still not getting correct results and I think I know why but I can't think of how to fix it.

My combination function requires information from my factorial . How do I pass the info from one function to the next and then store it in main ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
File name:   simpleMath.cpp
// Assign ID:   LAB10
// Due Date:    11/07/12 at 7:30 pm 
//
// Purpose:     Performs a simple addiion between 2 items.
//
// Author:      bfields Byron Fields
// --------------------------------------------------------------------


#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;


int input_check(int n, int r)
{
if( n > r)
 return 1;
else 
   return 0;
}

unsigned long int fact(int n, int r)
{

int factn = 1;

do 
{
for (;n>=1;n--)
{

factn *= n;
int a = factn;

}
}while(n != 0);


int factr = 1;

do 
{
for (;r>=1;r--)
{

factr *= r;
int b = factr;

}
}while(r != 0);



int c = n-r;


}
 

unsigned long int comb(int a, int b, int c)
{
int comb = a/(b*c);

return comb;
}

int main()
{
int n,r,a,b,c,d;

cout << "(c) 2012, bfields Byron Fields" << endl;

cout << "Enter the n value" << endl;
cin >> n;

cout << "Enter the r value" << endl;
cin >> r;


if(input_check(n,r) == 1)
  fact(n,r);
else
  cout << "Wrong Input" << endl;

d = comb(a,b,c);

cout << d << endl;



cout << "(c) 2012, bfields Byron Fields" << endl;


    
return 0;
}
One thing I see that you compiler should have flagged is that the definition for fact says it returns an unsigned long (line 63), but when you call it (line 88) and assign the result to d, d is declared as an int. Your compiler should give you a truncation warning. Even if long and int are the same length on your compiler, you should get a warning about assigning an unsigned value to a signed value.

Also, nowhere in fact() do you return a result.

Line 57, you calculate c, but don't do anything with it. The value is lost when fact exits.

Line 84: Was fact() supposed to return something? Otherwise, what was the point of calling it?

Line 88: a, b and c are uninitialized at this point. The calculation that comb() does is going to be bogus.



Yeah I'm using Unix and it didn't say anything about that. I appreciate all the help your giving though. The more input I get from you the clearer all this is getting.

And fact is supposed to return the factorial of the read in variables n & r. And then the function comb is supposed to take the values of factorial from function fact and also the value of n-r from fact and calculate the combination.
Topic archived. No new replies allowed.