【问题标题】:unsuccesfully trying to add integers of an array in function C++尝试在函数 C++ 中添加数组的整数失败
【发布时间】:2018-11-24 19:53:49
【问题描述】:

我正在编写一个程序来将学生的 5 分相加。

我已成功读取名为 input() 的对象函数中的 5 个整数,该函数将值分配给名为 scores 的私​​有 int[] 数组。

但是,我无法从 calculateTotalScore() 函数返回总和。

当我尝试运行 Eclipse 编译器时,它使用我为 scores 选择的 5 个整数给出的输出如下:

40 60 80 90 22

所以它输出的是我给它的数字,但它没有做任何整数加法。

#include <iostream>
using namespace std;

class Student {
private:
    int scores[5];
    int sum;
public:
    void input();
    int calculateTotalScore();
};

void Student::input() {
    for (int i = 0; i < sizeof(scores) / sizeof(int); i++) {
        int grade;
        cout << "Enter your score" << endl;
        cin >> grade;
        scores[i] = grade;
    }
    //checking that the array are being stored.
    for (int i=0; i < sizeof(scores) / sizeof(int);i++){
        cout <<scores[i] << " " << flush;
    }
    cout << endl;
}

// returns the sum of the students scores
int Student::calculateTotalScore(){

    for (int i=0; i < sizeof(scores) / sizeof(int);i++){
        sum += scores[i];
    }
    return sum;
    //Check that the numbers are adding up correctly
    cout << sum;
}

int main() {

    Student Kristen;
    Kristen.input();
    Kristen.calculateTotalScore();
    return 0;
}

【问题讨论】:

  • 您在打印总和之前从calculateTotalScore 返回。投票结束是一个错字。
  • 小测验:sum 的初始值是多少,在你尝试添加所有内容之前?是0吗?是42吗?它是生命、宇宙和一切的答案吗?
  • sum不应该是Student的成员,而是calculateTotalScore的局部变量。
  • 这是在调试器中单步执行代码时很容易发现的事情之一。
  • @SamVarshavchik 好的,我已经将“sum”初始化为 0,现在它给了我正确的输出。

标签: c++ arrays function oop object


【解决方案1】:
int Student::calculateTotalScore(){

for (int i=0; i < sizeof(scores) / sizeof(int);i++){
    sum += scores[i];
}
return sum;  <- Code returns to previous call

//Check that the numbers are adding up correctly
cout << sum; <- Code never reaches this point as it always returns

当你告诉它返回时,你的函数就会返回。

计算无法访问的代码

知道它是什么的好主意:https://en.wikipedia.org/wiki/Unreachable_code

【讨论】:

    【解决方案2】:

    您看到的输出来自input() 中的cout 语句,这就是您看到您输入的所有数字的原因。

    calculateTotalScore() 不会向cout 输出任何内容,因为它在到达代码中的那个点之前returns。 cout 语句无法访问。你的编译器应该会警告你。

    calculateTotalScore() 根本不应该尝试输出到cout。最好由main() 来完成,其中calculateTotalScore() 会向它返回一个值:

    int Student::calculateTotalScore(){
        int sum = 0;
        for (int i=0; i < sizeof(scores) / sizeof(int);i++){
            sum += scores[i];
        }
        return sum;
    }
    
    int main(){
        ...
        cout << Kristen.calculateTotalScore();
        ...
    }
    

    附带说明,您所有的 sizeof() 用法都应替换为单个 const 变量:

    #include <iostream>
    using namespace std;
    
    class Student {
    private:
        static const int NumberOfScores = 5;
        int scores[NumberOfScores];
    
    public:
        void input();
        int calculateTotalScore();
    };
    
    void Student::input() {
        for (int i = 0; i < NumberOfScores; i++) {
            int grade;
            cout << "Enter your score" << endl;
            cin >> grade;
            scores[i] = grade;
        }
    
        //checking that the array are being stored.
        for (int i = 0; i < NumberOfScores; i++){
            cout << scores[i] << " ";
        }
        cout << endl;
    }
    
    // returns the sum of the students scores
    int Student::calculateTotalScore(){
        int sum = 0;
        for (int i = 0; i < NumberOfScores; i++){
            sum += scores[i];
        }
        return sum;
    }
    
    int main() {
        Student Kristen;
        Kristen.input();
        cout << Kristen.calculateTotalScore();
        return 0;
    }
    

    在现实世界中,学生的分数可能不定,因此请考虑使用std::vector&lt;int&gt; 而不是固定的int[]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 2018-11-06
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多