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.
#include <iostream>
usingnamespace 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;
}