Like Tree1Likes
  • 1 Post By laserlight

dealing with dynamic arrays

This is a discussion on dealing with dynamic arrays within the C++ Programming forums, part of the General Programming Boards category; Hi there! I have a problem in dealing with dynamic arrays. I have initialized the objects and now i want ...

  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
    2,588
    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.
    Code:
    namespace life
    {
        const bool change = true;
    }

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    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 10:28 AM.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,340
    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
    22,274
    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!"
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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,340
    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 01: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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21