Thread: Dynamic Arrays

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    16

    Dynamic Arrays

    Ok back to the questions

    I've created a database of CD's in which the Database class is simply an array of pointers to CD's (another class). I've initialized it as such:

    Code:
    CD* databaseArray[50];
    Now my question is how I can replace that 50 with a number that can change. For instance, make an array the size of the CD's in the database. Because once I have 50 CD's in the Database it will simply just start writing over them.

    Anyone got any ideas?

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    One of the easiest ways to do this is to use the vector class to act as a container for your CD class. This is cprogramming's vector tutorial. Let me know if you have any further questions.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    An array size has to be a constant value. If you want a dynamic array then you need to use a container class such as std::vector, or simulate an array with new and delete. Both of these have been covered extensively on this forum, if you're willing to search.
    Kampai!

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Alright, I whipped something up for ya. I use the vector container to store a struct and then display it to the user. Hopefully this will point you in the direction you want to go:

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    struct cd {
    	std::string Album;
    	int numTracks;
    };
    
    int main() {
    
    	std::vector<cd> my_DB;
    	std::vector<cd>::iterator iDB;
    	cd my_CD;
    
    	//now I am using a for loop simply to populate my vector
    	//you would most likely load your DB from a file and have 
    	//a more complex user input system
    	for(int i = 0; i < 2; i++) {
    		std::cout<< "Enter the Album name: ";
    		std::getline(std::cin, my_CD.Album);
    		std::cout << "Enter the number of tracks: ";
    		std::cin >> my_CD.numTracks;
    		std::cin.get();
    		my_DB.push_back(my_CD); //add the structure to the vector container
    	}
    
    	//display the information from vector container
    	for(iDB = my_DB.begin(); iDB != my_DB.end(); iDB++) {
    		std::cout << "The Album name is: " << (*iDB).Album << std::endl;
    		std::cout << "The number of tracks is : " << (*iDB).numTracks << std::endl;
    	}
    
    	std::cin.get();
    	return 0;
    }
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Hey andyhunter, to create kind of a middle minute discussion, why do you always go:
    Code:
    std::vector
    std::cout
    std::endl
    Why not just add these at the top:
    Code:
    using std::vector;
    using std::cout;
    using std::endl;
    And call vector, cout, etc, without having to go std:: all of the time?

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Kleid-0
    Hey andyhunter, to create kind of a middle minute discussion, why do you always go:
    [...]
    Why not just add these at the top:
    [...]
    And call vector, cout, etc, without having to go std:: all of the time?
    There is a microscopic chance that he'll include a badly written header to his program in the future, in which 'vector' and/or 'cout' are defined in the global namespace as something else.

    To specify std:: is not, by any means, required.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 11:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. processing dynamic arrays
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-04-2006, 11:32 AM
  4. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM