Hi there! I have a problem in dealing with dynamic arrays. I have initialized the objects and now i want to print them.
My main function looks like this:

char* namesList[] = {"Brad Shaw","Aimen Adams","Sal Dimitry","Cristi Anreaz","Poala James"};

    int idList[]={232,444,135,52,134};
    Team t;
    Team t1( namesList,idList,5,"waqas");
    //t1.Print_team();

My class Team looks like this:

#include <iostream>
#include <fstream>
#include <string>
#include"Team.h"
#include "Player.h"
using namespace std;
class Team
{
  private:
  Player* players;                      // class Player has two members i.e. char* name, int id
  int No_Of_Players;
  char* Name;                   
}
public:
Team::Team()
{
    Player* players=0;
    No_Of_Players=0;
    Name=0;

}

Team::Team( char* sNames_List[], int id_List[], int No_Of_Players, char* Name)
{

    Player* players= new Player[No_Of_Players];

    for (int i=0; i<No_Of_Players; i++)
    {
        players[i].Set_Id(id_List[i]);
        players[i].Set_Name(sNames_List[i]);
        cout << players[i].Get_Id() << " " << players[i].Get_Name() << endl;
    }


}
/*
void Team::Print_team()
{

    cout << " the team " << Name << " has " << No_Of_Players << " namely " << endl;

    for (int i=0; i<No_Of_Players; i++)
    {
        cout << " - " <<players[i].Get_Id() << " " << players[i].Get_Name() << endl;
    }

}
*/
Now the problem is that how do i print the values using the print function? 

Recommended Answers

All 6 Replies

What's wrong with the code you've already got? The only problems I can see is possibly formatting the output with a '\t' at the start of the players lines.

One suggestion I could make is, instead having the output direct from your class, have a ToString method instead and leave the printing to the calling method. This way if in the future you want to make a GUI for your class, or send the data to a file, you won't have to re-code the class. You can put formatting character codes('\n','\t', etc.) in the string to have it appear the way you want, then just output the string. You can even use the String Stream class(<sstream>) to mix numbers and strings, which works just like cout.

As for dynamic arrays, vectors would probably serve you better. Yopu access them the same as arrays, but you have built-in properties and methods like size, front, back, insert, swap, etc.

Hi there! the problem lies in the last function which is the print function. when i call it from the main it causes the program to crash. The values which have been set in the overloaded constructor are to be printed in this 'print' function. I had written cout statement in the overloaded constructor just to check..

Name at that moment is a NULL pointer.

At a quick first sight:

Replace

Player* players= new Player[No_Of_Players];

with

players= new Player[No_Of_Players];

Under that add:

No_Of_Players = No_Of_Players;

(Or: if you want to be more clear about the scope)

this->No_Of_Players = No_Of_Players;

Also make sure you set a string for "Name" in your constructors. Then your function should "work".
When using dynamic data you'll pretty much always want to define some overloads aswell, namely:

  • The copy constructor
  • The assignment operator
  • The destructor

It's probably worth it to look up. I'd probably use a vector (or something similar) here though instead of a dynamic array. You should also use the "string" object instead of using C-strings.

C++ also has something named a "member initialization list" you'd probably want to make a habit using. (look it up)

Yoo might like to see a C++ way of coding ...

Then, if really need dynamic C strings and a dynamic array, you will have a working proto-type to start.

#include <iostream>
#include <string>
#include <vector>


//using namespace std;

class Player
{
    std::string name;
    int id;
public:
    // default and value ctor...
    Player( std::string n = "", int i = 0 ) : name(n), id(i) {}
    void print() const
    {
        std::cout << "Name: " << name << ", ID: " << id;
    }
} ;


class Team
{
    std::vector< Player > players;
    std::string tName;
public:
    // default ctor ...
    Team() {}

    void set_tName( std::string tN ) { tName = tN; }

    void push_back( const Player& pl )
    {
        players.push_back( pl );
    }

    void print() const
    {
        for( size_t i = 0; i < players.size(); ++ i )
        {
            players[i].print();
            std:: cout << "\n";
        }
        std::cout << "Team \"" << tName << "\" has "
                  << players.size() << " players.\n";
    }
} ;

To see some next steps to ...

  1. replacing C++ string with dynamic C strings

  2. replacing C++ STL vector with dynamic array

  3. replacing C++ cout with C printf

you could check here ...

Using C code with C++

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.