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

Dani AI

Generated

The simplest, safest design is to give each contestant their own container of scores instead of trying to link parallel arrays. That keeps the index logic local to each object and avoids the lifetime and synchronization bugs that come with raw pointers. ’s parallel-array warning is valid for small quick hacks; for production code prefer encapsulation. ’s nudge toward standard library types is also right — use std::string and std::vector and let RAII manage memory.

Example Contestant type (illustrates member layout and safe access):

#include <string>
#include <vector>

struct Contestant {
    std::string name;
    std::vector<float> scores;

    Contestant() = default;
    Contestant(const std::string& n, std::size_t attempts)
        : name(n), scores(attempts, 0.0f) {}

    // safe access with bounds checking
    float& at_score(std::size_t i) { return scores.at(i); }
};

How to create N players with M attempts and populate scores (use <random> for reproducible floats):

#include <iostream>
#include <random>
#include <vector>

// ... Contestant definition above ...

int main() {
    std::size_t N, M;
    std::cin >> N >> M;
    std::vector<Contestant> players;
    players.reserve(N);
    for (std::size_t i = 0; i < N; ++i)
        players.emplace_back("Player" + std::to_string(i+1), M);

    std::mt19937 rng{std::random_device{}()};
    std::uniform_real_distribution<float> dist(0.0f, 10.0f);
    for (auto& p : players)
        for (auto& s : p.scores)
            s = dist(rng);
}

Tips and gotchas: use .at() for bounds-checked access while debugging; resize/reserve to avoid reallocations; copying a Contestant will deep-copy its scores vector (or use move semantics). Prefer the encapsulated approach unless you have a strong reason to maintain parallel arrays. For reference: std::vector, std::string, <random> utilities.

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.