0

i'm just scratching my head at this issue because i can't seem to figure out what could possibly be causing the error

#include <iostream>
#include "Player.h"
using namespace std;

Player :: Player(int games, string nam) {
    name = nam;
    array = new int[games];
}

Player :: ~Player() {
    delete[] array; 
}

int Player :: getNumber(Player g, int index) {
    return g.array[index]; 
}

void Player :: getInputs(Player g, int games) {
    for(int i = 0; i < games; i++){
        //int location = i + 1; 
        cout << "Please enter a number at index " << i + 1 << " for player " << g.name << ":";
        cin >> g.array[i]; 
        validInput(cin, g, i);
        cout << "what's going on" << endl;
    }
    cout << "Player " << g.name << " selected the following numbers: "; 
    for(int i = 0; i < games; i++){
        cout << g.getNumber(g, i) << " "; 
    }
}

void Player :: whoWinner(Player g, Player h, int games) {
    int player1Score = 0;
    int player2Score = 0; 
    for(int i = 0; i < games; i++) {
        if(g.array[i] > h.array[i]){
            player1Score++; 
        }else if(g.array[i] < h.array[i]) {
            player2Score++; 
        }else {
            continue; 
        }
    }
    if(player1Score > player2Score){
        cout << g.name << " Won" << endl; 
    }else if(player1Score < player2Score){
        cout << h.name << "Won" << endl;
    }else if (player1Score == player2Score) {
        cout << "It's a draw" << endl;
    }
}

void Player :: validInput(istream &stream, Player b, int index) {
    while(cin.fail()){
        cout << "Enter a number" << endl;
        cin.clear();
        cin.ignore();
        cin >> b.array[index];
    }
}

the error occurs when the input for the player reaches the last index in the for loop, where it breaks immediately. I don't think i'm freeing any memory at this point so i'm not sure what the issue is

also it breaks before the destructor is reached in the main function if that helps

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Cezlock
  • 19
  • 1
  • 3
    You don't seem to follow [the rules of three, five or zero](https://en.cppreference.com/w/cpp/language/rule_of_three). Simple solution (using the rule of zero): Remove the destructor and use `std::vector` for the data. – Some programmer dude Feb 08 '22 at 07:03
  • On another note, why do you pass `Player` objects to the member functions? That's not needed in C++ (and actually makes your issue worse). Do you perhaps come from a Python background? If you want to learn C++ invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Feb 08 '22 at 07:04
  • i can't use vectors and i need a destructor for this particular program but i'll try following those rules. also, i just sort of forgot that i didn't need to pass in objects. – Cezlock Feb 08 '22 at 07:16
  • 1
    *"i can't use vectors"* I wanna find the "instructors" that "teach" C++ this way and sock them with a stout rolling pin. Modern C++ should spend a couple weeks on manual memory management, then the rest of the cycle urging you to *never* do it, and showing you all the ways to achieve that. – WhozCraig Feb 08 '22 at 07:22
  • @WhozCraig Is soo right, can you show your teacher this : https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines ? And ask him/her to update their teaching material accordingly. And also tell you teacher that the use of new/delete and "C" style arrays are better left for an advanced C++ course for library developers. The real world is waiting for developers who can write code with less bugs and know when to (re)use tested code (e.g. the standard library). – Pepijn Kramer Feb 08 '22 at 08:02
  • Your code passes `Player` values to functions. That is similar to copying those values, as in `Player p1; Player p2 = p1;`. Since you are using the default copy constructor, those two players have the same pointer inside. When they are both destroyed, that causes a double delete. Read about the rule of five, as suggested, especially if you can't use the standard library containers and you are forced to manually handle pointers. – chi Feb 08 '22 at 09:32
  • thanks for everyone's help, once i implemented the necessary functions and stopped passing in the objects, everything works exactly as it should. now i'll also be able to curse at the system that forces me to manually manage memory. i had an assignment totally centered around it and i wanted to die the entire way through – Cezlock Feb 08 '22 at 17:28
  • Your question is incomplete, there is no real class definition of player i.e. the player.h content is missing. Alsways post an [mre] – Superlokkus Feb 18 '22 at 12:05

0 Answers0