Reputation: 45
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;
}
Upvotes: 0
Views: 115
Reputation: 798
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...
Upvotes: 0
Reputation: 2708
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;
}
Upvotes: 0
Reputation: 1206
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);
}
Upvotes: 0
Reputation: 2085
It should be
int answer = 0;
...
...
...
answer = answer + (i * i);
Upvotes: 0
Reputation: 212939
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.
Upvotes: 2
Reputation: 6051
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;
Upvotes: 1
Reputation: 258558
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;
}
Upvotes: 2