user3264250
user3264250

Reputation: 77

C++ While loop and Array

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

Upvotes: 0

Views: 1807

Answers (3)

stack smasher
stack smasher

Reputation: 463

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

Upvotes: 0

johnsyweb
johnsyweb

Reputation: 141948

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.

Upvotes: 0

Blaz Bratanic
Blaz Bratanic

Reputation: 2279

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;

Upvotes: 1

Related Questions