Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C++ help

Name: Anonymous 2010-09-30 6:02

Just started C++, Im required to make a program that finds the factorial of a entered number.

I keep getting this error
"`main' must return `int'"

Google wasn't much use

Heres my code:

#include <iostream>
using namespace std;

int factorial( int num);
void main()
{
    cout <<"Enter a number to find the factorial of"<<endl;
        int num, ans;
        cin >>num;
        ans = factorial(num);
        cout <<"The value of the factortial of " <<num <<" is " <<ans<<endl;
}


int factorial( int num)
{
    int fact = 1;
        for( int i = fact + 1; i <=(num + 1); i++)
        {
        fact = fact * i;
        }
    return fact;
}

Name: Anonymous 2010-09-30 6:21

Read SICP.

(define (factorial n)
  (fact-iter 1 1 n))
(define (fact-iter product counter max-count)
  (if (> counter max-count)
      product
      (fact-iter (* counter product)
                 (+ counter 1)

Name: Anonymous 2010-09-30 6:31

/code] tags, then read SICP.

Name: Anonymous 2010-09-30 6:56

im no C++ EXPERT by any means, but you need to add
return int; add the end of your main method

Name: Anonymous 2010-09-30 6:59


//other functions

int main()
{
    //Your crap here
    return 0;
}

Name: Anonymous 2010-09-30 7:02

>>2
U MENA

(define factorial
  (lambda (n)
    (define fact-iter
      (lambda (product count)
        (if (= count 0)
            product
            (fact-iter (* product count)
                       (- count 1)))))
    (fact-iter 1 n)))

Name: Anonymous 2010-09-30 7:03

>>5
you obviously didn't read the compiler error message. i fixed it for you

//other functions

int main()
{
    //Your crap here
    return `int';
}

Name: Anonymous 2010-09-30 7:04

>>1 should really read SICP.

Name: Anonymous 2010-09-30 7:54

Protip: WABT

Name: Anonymous 2010-09-30 8:05

Name: Anonymous 2010-09-30 9:16

>>10
YHBT

Name: Anonymous 2010-09-30 9:24


Don't change these.
Name: Email:
Entire Thread Thread List