Below is the code I am starting with. What is desired is to have the user input the number of players and the number of scores and then populate the players and scores. To check the program out I usually will use random numbers for the scores. My problem is how to set up and access the array of Players[] linked to the array of rawScores[] and populate each. I am thinking of Players.rawScores[j] inside a couple of for loops would suffice. I just need help in the setup and addressing the elements.
Any assistance would be appreciated.

#include <iostream>
#include <conio.h>
using namespace std;

class Person
{
  protected:
    char *Name;
  public:
    void SetName(char LastN)
    {
       strcpy(Name, LastN);
    }
};
class Contestant : public Person
{
    private:
	   float *rawScores;
	public:
	   Contestant( )
	   {
	       SetName("");
	       Score = 0;
	       NumberAttempts = 0;
	   };
};
int main()
{
	int numP, numS;
	Contestant *Players;
	cout<<"How many Players? ";
	cin >>numP;
	Players = new Contestant[numP];
	cout<<"How many Attempts? ";
	cin >>numS;
	Players.rawScores = float[numS];
}

Recommended Answers

All 2 Replies

One option is to use parallel arrays.

It's important to remember that if you delete or move a subscript in either array, its corresponding subscript in the other array must be deleted or moved.

Hope this helps or is what you were looking for

First, SetName() is attempting to copy the name to an uninitialized pointer -- guarenteed to crash your program bigtime. Instead of using a char* for Name why not use the c++ std::string class so that you don't have to worry about memory allocation and all the problems that go along with it?

class Person
{
  protected:
    std::string Name;
  public:
    void SetName(std::stromg LastN)
    {
          Name = LastN;       
   }
};

You can also realize big improvements by using <vector> instead of array pointers, because vectors will auto expand to fit the desired array size, and you can index them just like you do normal arrays.

...
    vector<float> rawscores;
...
int main()
{
    vector<Contentent>  Players;
    ...
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.