Thread: dealing with dynamic arrays

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    27

    dealing with dynamic arrays

    Hi there! I have a problem in dealing with dynamic arrays. I have initialized the objects and now i want to print them.

    Code:
    // main
    char* namesList[] = {"Brad Shaw","Aimen Adams","Sal Dimitry","Cristi Anreaz","Poala James"};
    
    
        int idList[]={232,444,135,52,134};
        Team t1( namesList,idList,5,"waqas");
            t1.Print_team();
    My class Team looks something like this:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include"Team.h"
    #include "Player.h"
    using namespace std;
    Team::Team()
    {
        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]);
            players[i].Set_Id(id_List[i]);
            
        }
    
    
    }
    
    
    void Team::Print_team()
    {
        
        cout << " the team " << Name << " has " << No_Of_Players << " players namely " << endl;
    
    
        for (int i=0; i<No_Of_Players; i++)
        {
            cout << " - " <<players[i].Get_Id() << " " << players[i].Get_Name() << endl;
        }
    
    
    
    
    }
    The print function does not work. I have to implement the print function without taking any parameters.. How should i do that ? Thanks!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you need to store No_Of_Players at the class level. that value does not exist after the constructor returns unless you do. from what I can see, Team::Print_team() has no definition for No_Of_Players, making it impossible to even compile this program.

    and please don't make your font so big. it's not helpful in any way, and it will often encourage people to ignore your post.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Consider using std::vector instead of new.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    27
    How to store No_Of_Players at a class level ??
    Last edited by waqas94; 07-15-2013 at 09:28 AM.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by waqas94 View Post
    How to store No_Of_Players at a class level ??
    Use the "this" pointer in the Team constructor and Print_team member function to access members of the Team class.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    27
    Quote Originally Posted by rcgldr View Post
    Use the "this" pointer in the Team constructor and Print_team member function to access members of the Team class.
    Not working!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by waqas94
    I have a problem in dealing with dynamic arrays.
    You really should use std::vector instead of trying to do manual memory management and std::string instead of pointers to char. If you must do manual memory management because it is a key point of the exercise, then use std::string. If you cannot use std::string, then write your own string class first. At the moment, I cringe at what you are doing because your code looks like a very fragile construct that can break so easily.

    Quote Originally Posted by waqas94
    My class Team looks something like this:
    You should post the class definition too, not just the implementation of member functions.

    Quote Originally Posted by waqas94
    Not working!
    I'm sorry to hear that, but that's not helpful feedback. What did you try? How does it not work? What error messages do you get? If you only say "not working!", then we can only reply "too bad!"
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not helpful!
    You need to show what you did and what errors you get.
    Even better is if you actually try to use std::vector! It's safer, it's faster and easier to get right and it's exception safe.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    By "at class level," do you mean that you want to store the number of players as a constant? If you decide to do that, you need to declare the variable type with the const modifier, and you need to write the constructor with an initializer list:

    Code:
    Team::Team( char* sNames_List[], int id_List[], int NoOfPlayers, char* Name ): No_Of_Players(NoOfPlayers) // initializer list
    {
      players = new Players[No_of_Players];
    
      for ( int i = 0; i < No_of_Players; i++) {
        players[i].setId(id_List[i]);
        players[i].setName(sNames_List[i]);
      }
    }
    The purpose of the above code is to demo initializer lists only, I don't know if this can be copied exactly.

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by rcgldr View Post
    Use the "this" pointer in the Team constructor and Print_team member function to access members of the Team class.
    Quote Originally Posted by waqas94 View Post
    Not working!
    Actually, you shouldn't need to use the this pointer except for special cases. One issue is that you constructor uses names that duplicate member names. Whiteflag's example code in the previous post should fix that issue.
    Last edited by rcgldr; 07-15-2013 at 12:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 12-03-2011, 02:26 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Dealing With Multi Dim Char Arrays
    By mike_g in forum C Programming
    Replies: 8
    Last Post: 06-15-2007, 01:52 PM
  4. Arrays, and Functions dealing with them
    By Argentum in forum C++ Programming
    Replies: 6
    Last Post: 12-05-2005, 07:25 PM
  5. Dynamic Arrays
    By swayp in forum C++ Programming
    Replies: 5
    Last Post: 01-27-2005, 06:18 AM