0

My program is supposed to calculate the sum of all the squares of numbers up until the users input. For example if the user inputs 2, the function will perform : (1^2 + 2^2) However my program refuses to do anything when run. (Not sure if this is a function problem, or with the main body.)

#include <iostream>
#include <cmath>
using namespace std;

int sumofsquares (int num)

{
int i;
int answer;

for(int i=0; i <= num; i++){
answer = (num * num)+ num;
}
return (answer);

}


int main(){
int num;
cout<< "Enter a number" <<endl;
cin >> num;
while( num != -1){
sumofsquares(num);
}
cout<< "The sum of squares is "<< num <<endl;


return 0;
}

7 Answers7

2

You have to assign the return value of the function to something - in your case, since you're printing num, to num itself:

num = sumofsquares(num);

After you do this, your function will enter an infinite loop if num isn't -1 because you never modify it. You probably meant:

while( num != -1){
   cin << num;
}
sumofsquares(num);

After this, you're left with the bugs in the function:

int answer;
for(int i=0; i <= num; i++){
answer = (num * num)+ num;
}

should be

int answer = 0;
for(int i=0; i <= num; i++){
answer += i*i;
}

The real problem however is that you're missing basic C++/logic knowledge, to which the only solution is to learn from a good book.

Community
  • 1
  • 1
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
2

In sumofsquares change:

int answer;

to:

int answer = 0;

so that answer is properly initialised.

Also you need to change:

answer = (num * num)+ num;

to:

answer = (i * i) + answer;

otherwise you're squaring the wrong variable and adding it to the wrong accumulator.

See the other answers below for info on fixing the problems in main.

Also you should learn to format your code properly - that will make it much easier to read, debug and maintain, both for others and for yourself.

Paul R
  • 208,748
  • 37
  • 389
  • 560
1

You just called the function without getting the return value of it.

First option:

cout<< "The sum of squares is "<< sumofsquares(num) <<endl;

Second option:

num=sumofsquares(num);
cout<< "The sum of squares is "<< num <<endl;
Lior
  • 5,841
  • 9
  • 32
  • 46
0

It should be

int answer = 0;
...
...
...
answer = answer + (i * i);
Abhinav
  • 2,085
  • 1
  • 18
  • 31
0

Your program is stuck in the while loop, since the termination condition num != -1 will never change. This is because the variable num is passed to sumofsquares by value, rather than by reference. Thus changing the num variable in sumofsquares will have no effect on the num variable in main. Replace your while statement with:

while (num != -1) {
    num = sumofsquares(num);
}
nickolayratchev
  • 1,136
  • 1
  • 6
  • 15
0

Here you go!

#include <iostream>
#include <cmath>
using namespace std;

//Prototype
int sumofsquares(int num);

void main()
{
    int num;  
    int answer;

    cout >> "Enter a number.\n";
    //Get number input
    cin << num;
    //Call your workhorse function
    answer = sumofsquares(num);
    //Double check my >> directions :P  Format may be wrong here 
    cout >> "Sum of squares for " >> num >> "is " >> answer >> "\n";
}

int sumofsquares(num)
{
    int answer;
    int i = 0;

    for (i = 0; i < num; i++)
    {
        answer = answer + ( i * i );
    }

    return answer;
}
davepmiller
  • 2,620
  • 3
  • 33
  • 61
0

Also, you should note that you used cin to initialize num, so amongst the issues pointed out by others, keep in mind that you should use cin to initialize a string/char, then change that to an integer:

#include <iostream>
#include <cmath>
#include <string>
using namespace std;

int sumofsquares (int num){
  //should have initialized answer to zero
  int i, answer = 0;
  //this for loop now accomplishes what you want
  for(i=0; i <= num; i++){
    answer = (i^2) + answer;
  }
  return (answer);
}


int main(){
  int num, ans;
  string input;
  cout<< "Enter a number" <<endl;
  cin >> input;
  num = atoi(input.c_str());
  //this was an infinite loop, here's how to check what you wanted to check.
  if (num >= 0){
    ans = sumofsquares(num);
    cout << "The sum of squares is: " << ans << endl;
    return 0;
  }
  else { 
    cout<< "Error: The number you have called this function with is invalid." << endl;
    return 0;
  }
}

Something along these lines...

innospark
  • 778
  • 1
  • 5
  • 8