[SOLVED] Triangle function

I am learning C++ on my own out of a book I recently purchased, but I cannot get this code contained in the book to work. The program is intended to total all numbers from 1 to the number input (triangle function). However, when the program outputs whatever number is input. Any help is greatly appreciated.

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
#include <iostream>

using namespace std;

int triangle(int num);

int main()

{

	int n;
	cout << "Enter a number and press ENTER: ";
	cin >> n;
	cout << "Function returned " << triangle(n);
	return 0;
}

	
int triangle(int n)
{
	int i;
	int sum = 0;
	for (i = 1; i <= n; i++)
		sum = sum + 1;
	return sum;
}
Last edited on
Replace 1 by i in line no. 24.
Thanks for the help.
Topic archived. No new replies allowed.