第一次使用函数,并不确定为什么我的程序没有做任何事情?

时间:2012-10-16 23:40:51

标签: c++

我的程序应该在用户输入之前计算所有数字平方的总和。例如,如果用户输入2,则该函数将执行:(1 ^ 2 + 2 ^ 2)但是我的程序在运行时拒绝执行任何操作。 (不确定这是一个功能问题,还是与主体有关。)

#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 个答案:

答案 0 :(得分:2)

您必须将函数的返回值指定为某些内容 - 在您的情况下,因为您正在打印numnum本身:

num = sumofsquares(num);

执行此操作后,如果num不是-1,您的函数将进入无限循环,因为您从不修改它。你可能意味着:

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

在此之后,你留下了函数中的错误:

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

应该是

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.

答案 1 :(得分:2)

sumofsquares更改:

int answer;

为:

int answer = 0;

以便answer正确初始化。

您还需要更改:

answer = (num * num)+ num;

为:

answer = (i * i) + answer;

否则你正在调整错误的变量并将其添加到错误的累加器中。

请参阅下面的其他答案,了解有关解决main中的问题的信息。

此外,您应该学会正确格式化代码 - 这将使其他人和您自己更容易阅读,调试和维护。

答案 2 :(得分:1)

你刚刚调用函数而没有得到它的返回值。

第一个选项:

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

第二个选项:

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

答案 3 :(得分:0)

应该是

int answer = 0;
...
...
...
answer = answer + (i * i);

答案 4 :(得分:0)

您的程序停留在while循环中,因为终止条件num != -1永远不会改变。这是因为变量num按值传递给sumofsquares,而不是通过引用传递给num。因此,更改sumofsquares中的num变量不会影响main中的while变量。将您的while (num != -1) { num = sumofsquares(num); } 语句替换为:

{{1}}

答案 5 :(得分:0)

你走了!

#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;
}

答案 6 :(得分:0)

另外,你应该注意到你使用cin初始化num,所以在其他人指出的问题中,请记住你应该使用cin初始化一个字符串/ char,然后将其更改为整数:

#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;
  }
}

这些方面的东西......