0

I have made a application where you enter in all the marks and it gives you the average and also makes it repeat itself but the problem is that

1) when ever the 'Finding average' line is executed, it gives me the wrong value and also I use array to do so.

2)When ever I try to iterate the application, the destructor is called and messes up my application

and here is my code

#include <iostream>
#include <string>
using namespace std;

class Grade{
private:
    int* ptr;
    int number;
public:
    Grade(const int hNumber){
        number = hNumber;
        ptr = new int[this->number];
    }
    Grade(const Grade& cpy){
        ptr = new int[cpy.number];
    }
    void get_marks(){
        for(int i = 0; i < number; ++i){
            cout << "Enter Mark " << i << ": ";
            cin >> ptr[i];
        }
    }
    const int& operator [](const int access) const{
        return ptr[access];
    }
    ~Grade(){
        cout << "Deleting memory" << endl;
        delete [] ptr;
    }
};

int main(){
//local variables
    int sum = 0;
    string name,subject;
    int repeat;
    char again = 'y';
    //user interface
    cout << "Welcome to Grade Marker" << endl;
    cout << "Enter your name: ";
    getline(cin,name);
    while(again == 'y'){
    cout << "Enter the subject name: ";
    getline(cin,subject);
    cout << "How many marks are being entered: ";
    cin >> repeat;
    //creating instance of grade
    Grade grd(repeat);
    grd.get_marks();;
    //display info
    cout << "The average mark is: ";
    for (int i = 0; i < repeat; i++){
        sum = ((sum + grd[i]) / repeat);
    }
    cout << sum << endl;
    //looping the application
    cout << "Would you like to enter another subject[y/n]: ";
    cin >> again;
    }
    //good bye message
    if (again == 'n' || again == 'no'){
        cout << "Goodbye" << endl;
    }
    system("pause");
    return 0;
}

and just to make it simple, the code section which I think gives me error are

cout << "Would you like to enter another subject[y/n]: ";
        cin >> again;
        }
        //good bye message
        if (again == 'n' || again == 'no'){
            cout << "Goodbye" << endl;
            }

and

//display info
        cout << "The average mark is: ";
        for (int i = 0; i < repeat; i++){
            sum = ((sum + grd[i]) / repeat);
        }
        cout << sum << endl;

and thank you for your time

user3264250
  • 77
  • 1
  • 6
  • 1
    `'no'` isn't a string literal. And `again` is a single `char`, so trying to compare it with two characters isn't a good idea. – chris Mar 05 '14 at 21:56
  • 2
    Shouldn't you be dividing by repeat only once (after adding all the marks) ? also watch out for integer division. – Borgleader Mar 05 '14 at 21:56
  • You are doing the average wrong. No wonder you are getting wrong value! – user3329166 Mar 05 '14 at 22:02
  • Your conditional checking against `'no'` will not work, and you also have an integer division problem. – Zac Howland Mar 05 '14 at 22:02
  • Yes @Borgleader that helped me for the for-loop problem but I cant seem to figure out while loop problem. It keeps on calling the destructor – user3264250 Mar 05 '14 at 22:02
  • Some more criticism: `ptr` isn’t a very good variable name: it doesn’t tell the reader anything. And you should not use a pointer here anyway, use a `std::vector`. – Konrad Rudolph Mar 05 '14 at 22:03
  • @user3264250, Why is that a problem? The object gets destroyed at the end of the scope. That's normal. – chris Mar 05 '14 at 22:03
  • @user3264250 Thats because your object is inside the scope of the while loop, objects with automatic duration are destroyed when they reach the end of the scope they were created in. – Borgleader Mar 05 '14 at 22:04

3 Answers3

1

You are doing integer division, and in addition you do not reinitialize sum to 0 for each iteration. You can move sum declaration inside of the loop and just write:

float sum = 0;
for (int i = 0; i < repeat; i++){
    sum += grd[i];
}
cout << "The average mark is: ";
cout << sum / repeat << endl;
Blaz Bratanic
  • 2,279
  • 12
  • 17
0

std::cin.ignore() will help you here.

Adding...

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

...to the very end of your while-loop will clear out the input buffer up to the newline... You'll find that this will end the seemingly infinite loop.

See How do I flush the cin buffer? for more info.

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
0

Your while loop is calling the destructor because of scope. You are declaring the Grade grd every iteration you run the while loop. That means that your grd from the previous iteration is being redeclared again and again, hence the destructor being called. But that's normal. Did you want to save the Grade instances? In that case you'll need to make an array of Grade objects

stack smasher
  • 443
  • 3
  • 10