Thread: Where am I going wrong here?

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    99

    Where am I going wrong here?

    Trying to use an inline function to do a factorial, yet won't compile:

    Code:
    #include<iostream>using namespace std;
    
    
    inline int factorial(int n);
    int computeFactorials(int, int);
    
    
    
    
    
    
    int main()
    {
    	factorial(1, 8);
    	return 0;
    }
    
    
    int factorial(int n)
    {
    return (n == 1) ? 1: ( factorial(n - 1) * n) ;}
    
    
    int computeFactorials(int num, int max)
    {
    	cout << "Factorial of " << num << ": ";
    	cout << factorial(num) << endl;
    	num ++;
    	if (num > max ) return 0;
    	else return factorial(num, max);
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,631
    It looks like you may need two functions named factorial(), one with one parameter, and one with two parameters.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    904
    The compiler's warnings should be a big clue:

    Code:
    181267.cc: In function ‘int main()’:
    181267.cc:16:16: error: too many arguments to function ‘int factorial(int)’
       16 |  factorial(1, 8);
          |                ^
    181267.cc:6:12: note: declared here
        6 | inline int factorial(int n);
          |            ^~~~~~~~~
    181267.cc: In function ‘int computeFactorials(int, int)’:
    181267.cc:33:32: error: too many arguments to function ‘int factorial(int)’
       33 |  else return factorial(num, max);
          |                                ^
    181267.cc:21:5: note: declared here
       21 | int factorial(int n)
          |     ^~~~~~~~~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why so wrong numbers? As I write so wrong?
    By Dmy in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2017, 02:10 PM
  2. Replies: 3
    Last Post: 11-14-2011, 06:35 PM
  3. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 01:38 PM
  4. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  5. Replies: 9
    Last Post: 07-15-2004, 03:30 PM

Tags for this Thread