Functions help

Where does nTarget get its a value? I don't know of or see anything that initializes nTarget.

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
//
//Can someone explain where the function factorial gets a value for nTarget?
//
//

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

//  Return the factorial of the argument
//  provided. return a 1 for invalid arguments
//  such as negative numbers

int factorial (int nTarget)
{
    //set up an accumulator that is initialized to one
    int nAccumulator = 1;

    for (int nValue = 1; nValue <= nTarget; nValue++)
    {
        nAccumulator*= nValue;
    }
    return nAccumulator;
}

int main(int nNumberofArgs, char* pszArgs[])
{
    cout << "This program calculates"
         << " factorials of user inputs.\n"
         << "Enter a negative number to exit" << endl;

         //stay in a loop input for the user until he enters a negative number.
         for (;;)
         {
             //enter a number to calculate the factorial of
             int nValue;

             cout << "Enter a number: ";
             cin  >> nValue;

             //exit if number is negative
             if (nValue< 0)
             {
                 break;
             }
             int nFactorial = factorial (nValue);
             cout << nValue << " factorial is "
                  << nFactorial << endl;
         }
         system ("PAUSE");
         return(0);
}
look at line 47. nTarget = nValue since it is being used as a parameter with the name of nTaret might I suggest you read up on functions.
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/

Ps what is up with your main function?
1) you are not using args.
2) most people if using args name it argc , and argv for count and vectors AFAIK
Last edited on
Thank you that helped I was thinking that had something to do with it, but the reference really helped thank you. The main function is made out of magic and rainbows to me, I don't question it.
Topic archived. No new replies allowed.