trouble with returning a value inside a loop

I have tried various ways of returning a variable inside a loop in the function called tSumGen(generates triangular numbers) but it never works when I call it in main. The code is very simple but I cant find a way to make it work, I'd like to know if there is a way to return the value of the variable inside the last moment in the loop so I can then do some extra with that value in main.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <iostream>

int tNumGen(int p)
{
    int s=0;
    for(int i=p; i>0; i--)
    {
        s=s+i;
    }
    return s;
   
    
}

int main()
{
    tNumGen(7);
}


The value that should be getting returned to main is 28 but it doesnt show up.
You got to use cout to output the data.

If using namespace std;
cout << tNumGen(7);

If not using namespace std;
std::cout << tNumGen(7);

Look at the link below to learn more about input/output:
http://www.cplusplus.com/doc/tutorial/basic_io/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int tNumGen(int p)
{
    int s=0;
    for(int i=p; i>0; i--)
    {
        s=s+i;
    }
    return s;
   
    
}

int main()
{
    std::cout << tNumGen(7);
}
Last edited on
Topic archived. No new replies allowed.