Sum of the squares

Hey I am trying to write a program that will take the sum of the squares and I can't figure out what is wrong with my code. Mathematically, it makes sense but the code makes the number way to large. Unless, I am just tried and can't see its driving me nuts. Here is my code.

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

using namespace std;
int main()
{
    int number,sum;


    cout << "Enter a number greater than 0 (less than 1 to quit:) ";
    cin >> number;
    if (number > 0)
    {
        for(int counter = 1 ; counter <= number; counter++)
        {
        sum =sum + pow(counter,2);

        }
    cout <<"The sum of the squares from 1 to "<< number<< " is "<< sum<<"."<<endl;
    }
    else
    {
        cout<<"Press enter to quit."<<endl;
    }
return 0;
}

On line 12 number is given a value from the user input.

Hint: On which line is sum given an initial value?
cout <<"The sum of the squares from 1 to "<< number<< " is "<< sum<<"."<<endl;


So if I enter 10, sum should equal 14 (assuming 1 is a square number). The code you have would return 1 + 4 + 9 + 16 + ... + 81, the sum of the squares from numbers less than what I entered
I see I never gave sum an initial value. Thanks it works now. I just gave sum =0 lol.
Topic archived. No new replies allowed.