Simon
Simon

Reputation: 241

unsuccesfully trying to add integers of an array in function C++

I'm writing a program to add up a student's 5 scores.

I have successfully read 5 integers in an object function called input(), which assigns the values to the private int[] array named scores.

However, I cannot return the sum from the calculateTotalScore() function.

The output that is given by the Eclipse compiler when I try to run it, using 5 integers I chose for scores, is the following:

40 60 80 90 22

So it's outputting the numbers I'm giving it, but it's not doing any addition of integers.

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

Upvotes: 0

Views: 62

Answers (2)

solarflare
solarflare

Reputation: 441

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

Your function returns when you tell it to return.

cout << sum; is what is called unreachable code

Good idea to know what it is: https://en.wikipedia.org/wiki/Unreachable_code

Upvotes: 2

Remy Lebeau
Remy Lebeau

Reputation: 598001

The output you are seeing is from the cout statements in input(), that is why you see all of the numbers you typed in.

calculateTotalScore() does not output anything to cout, because it returns before it reaches that point in the code. The cout statement is unreachable. Your compiler should be warning you about that.

calculateTotalScore() should not be trying to output to cout at all. That is better done by main() instead, where calculateTotalScore() returns a value to it:

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();
    ...
}

On a side note, all of your sizeof() usages should be replaced with a single const variable instead:

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

In the real world, students are likely to have a variable number of scores, so consider using std::vector<int> instead of a fixed int[].

Upvotes: 0

Related Questions