Why does this program displays not responding at end of input?

I made this program as a project but it does not respond at the end of input loop.Can anyone help me fix this and explain whats wrong?
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    #ifndef PLAYER_H_INCLUDED
    #define PLAYER_H_INCLUDED
    #include<string.h>-
    using namespace std;


    int const NGAME = 10;
    const double DEFAULT_POINT = 0.0;

    class PLAYER
    {
    private:
        {
        string PlayerName;
        int Description;
        double *Points;

        void Points_Array()
        {Points= new double[10];
        for (int i = 0; i < 10; i++)
        Points[i] = DEFAULT_POINT; }
        }
    public:


    }
    PLAYER(){};


    PLAYER(string name, int Desc,int point[])
    { PlayerName = name;
    Description=Desc; }

    // Copy constructor
    PLAYER(const PLAYER &obj)
    { PlayerName = obj.PlayerName;
    Description = obj. Description;
    Points = new double[NGAME];
    for (int i = 0; i < NGAME; i++)
    Points[i] = obj.Points[i]; }

    // Destructor
    ~PLAYER()
    { delete [] Points; }

    // The setTestScore function sets a specific
    // test score's value.
    void setPoints(double score[])
    { Points = score; }

    // Set the student's name.
    void setPlayerName(string name)
    { PlayerName = name; }

    void setDescription(int desc)
    {
    Description=desc;
    }

    // Get the student's name.
    string getPlayerName() const
    { return PlayerName; }

    // Get the number of test scores.
    int getDescription()
    { return Description; }

    // Get a specific test score.
    double getPoints(int index) const
    { return Points[index]; }

    // Overloaded = operator
    const PLAYER operator=(const PLAYER &right)
    { delete [] Points;
    PlayerName = right.PlayerName;
    Description = right.Description;
    Points = new double[10];
    for (int i = 0; i < 10; i++)
    Points[i] = right.Points[i];
    return *this;  }

    };



    #endif // PLAYER_H_INCLUDED
    };



main.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <string.h>
#include "player.h"
#include <iomanip>
using namespace std;

void Inputfunction();
void displaydata();

PLAYER player1 = PLAYER();


const int N_P_T = 5;

PLAYER team[N_P_T];

int main()

    {

    //creation of original player
    Inputfunction();
    displaydata();

    PLAYER playerExtra = player1; //creates extra player copied from Player1
    double extra[] = {1,2,3,4,5,6,7,8,9,0};   //temporary holder for extra's scores
    playerExtra.setPoints(extra);

    return 0;
    }


void displaydata()
{
//  ofstream outfile;

//outfile.open("Basketball_report.txt");

//Header for text file

cout << setw(14) <<"Player Name"<< setw(8) << "Position" << setw(8)
<< setw(16) << "points";
cout << "-------------------------------------\n";

for (int i = 0; i < 5; i++)
{
cout << setw(14) << team[i].getPlayerName();
cout << setw(8) << team[i].getDescription();
//cout << setw(7) << team[i].getPoints() << endl;
}
//    outfile.close();// closes file
}



void Inputfunction()
{
string namem;
int descm;
double scorem[10];

cout<<"Score Board"<<endl;

for (int pnc=0;pnc<7;pnc++)
{

cout<<endl<<endl<<"Player"<<pnc+1<<endl<<endl;
cout<<"What is the player"<<pnc+1<<"'s Name?";

cin.ignore();
getline(cin,namem);


cout<<"What is his/her Position?";
cin>>descm;

/* 
for(int pgc=0;pgc<10;pgc++)
    
{cout<<"what did he score on the game"<<pgc+1<<"?";
cin>>scorem[pgc];}
*/

    if(pnc==0)
    {
    player1.setPlayerName(namem);
    player1.setDescription(descm);
    }

    {
    team[pnc+1].setPlayerName(namem);
    team[pnc+1].setDescription(descm);
    }

    }


    }

Last edited on
closed account (48T7M4Gy)
Use
#include <string> not #include<string.h>
it still terminates after input loop.
There are a lot of inconsistencies.
There are constants
1
2
const int N_P_T = 5;
int const NGAME = 10;

which are occasionally used in the code, but other times there are things like:
1
2
3
4
for (int i = 0; i < 5; i++)
for (int i = 0; i < 10; i++)
for (int pnc=0;pnc<7;pnc++)
double scorem[10]

Don't use magic numbers - make use of the named constants instead.

More of a problem is the matching of new[] with delete[]
That is almost certainly an issue.
Here, the pointer is not initialised at all:
PLAYER(){};
Here, it is allocated with new,
Points= new double[10];
Here it is assigned the value of some other array:
Points = score;

However, in the destructor,
 
~PLAYER()   { delete [] Points; }
the pointer is always deleted, regardless of whether or not it is valid.

The easiest solution might be to not use new/delete at all, Just define a fixed-size array.


Edit: Also, look at this:
1
2
3
const int N_P_T = 5;

PLAYER team[N_P_T];


1
2
3
4
5
for (int pnc=0;pnc<7;pnc++)
{
    team[pnc+1].setPlayerName(namem);
    team[pnc+1].setDescription(descm);
}

valid subscripts for array team[] are 0, 1, 2, 2, 4.
The above loop modifies elements 1, 2, 3, 4, 5, 6, 7

Element 0 is ignored, which is harmless. However, attempting to modify non-existent elements 5, 6 and 7 is surely a problem.
Last edited on
Topic archived. No new replies allowed.