0

我只是在这个问题上摸不着头脑,因为我似乎无法弄清楚可能导致错误的原因

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

当播放器的输入到达 for 循环中的最后一个索引时会发生错误,并立即中断。我认为此时我没有释放任何内存,所以我不确定问题是什么

如果有帮助,它也会在主函数中到达析构函数之前中断

4

0 回答 0