-5

The sum of the following array does not show correctly, instead it shows large junk of digits. What I did wrong.

void putdata(){
        int mark_sum;
        cout << name << " " << age << " ";
        for(int i=0;i<6;i++){
            mark_sum += marks[i];
        }
        cout << mark_sum << " ";
        cout <<cur_id - no_of<< endl;
        no_of--;
    }

inside this function marks[] array contains some integer element, after taking sum it gets wrong sum.

Here is the full,

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

//classes
//classes
class Person{
public:
    virtual void getdata() = 0;
    virtual void putdata() = 0;
protected:
    string name;
    int age;
};

class Professor : public Person{
public:
    Professor() {
    }
    void getdata(){
        cin >> name;
        cin >> age >> publications;
        cur_id++;
        no_of++;
    }
    void putdata(){
        cout << name << " ";
        cout << age << " ";
        cout << publications << " ";
        cout <<cur_id - no_of<< endl;
        no_of--;
    }
private:
    int publications;
    static int cur_id;
    static int no_of;
};

class Student : public Person{
public:
    Student(){
    }
    void getdata(){
        cin >> name >> age;
        for(int i=0;i<6;i++){
            cin >> marks[i];
        }
        cur_id++;
        no_of++;
    }
    void putdata(){
        int mark_sum;
        cout << name << " " << age << " ";
        for(int i=0;i<6;i++){
            mark_sum += marks[i];
        }
        cout << mark_sum << " ";
        cout <<cur_id - no_of<< endl;
        no_of--;
    }
private:
    int marks[6];
    static int cur_id;
    static int no_of;
};

//static variables
int Student::cur_id = 0;
int Professor::cur_id = 0;
int Student::no_of = -1;
int Professor::no_of = -1;

//main
int main(){

    int n, val;
    cin>>n; //The number of objects that is going to be created.
    Person *per[n];

    for(int i = 0;i < n;i++){

        cin>>val;
        if(val == 1){
            // If val is 1 current object is of type Professor
            per[i] = new Professor;

        }
        else per[i] = new Student; // Else the current object is of type Student

        per[i]->getdata(); // Get the data from the user.

    }

    for(int i=0;i<n;i++)
        per[i]->putdata(); // Print the required output for each object.

    return 0;

}

I inputed this,

4 1 Walter 56 99 2 Jesse 18 50 48 97 76 34 98 2 Pinkman 22 10 12 0 18 45 50 1 White 58 87

And my expected output will be this,

Walter 56 99 1 Jesse 18 403 1 Pinkman 22 135 2 White 58 87 2

  • Fyi, `mark_sum += marks[i]` ... what was the initial value of `mark_sum` before the loop ? You never initialized it to zero. Your compiler should flag that was a "use of uninitialized variable" warning, and if it didn't you need to turn up your warnings. You also might try actually *checking* that your IO actually worked rather than assuming it does. Just saying. – WhozCraig Apr 15 '17 at 13:04
  • Looks like what you *really* want is [std::accumulate](http://en.cppreference.com/w/cpp/algorithm/accumulate). – Jesper Juhl Apr 15 '17 at 13:06

1 Answers1

2

You should initialize 'mark_sum' to zero when you define it.

In the first line of putdata function, instead of int mark_sum;, simply write int mark_sum = 0; and it will be alright.

The problem is that if you don't initialize an integer variable, you may not use it, cause you don't know the value.

more details here: Define integer (int); What's the default value?

Community
  • 1
  • 1
ganjim
  • 1,234
  • 1
  • 16
  • 30