Can you have a vector of chars?

This is a discussion on Can you have a vector of chars? within the C++ Programming forums, part of the General Programming Boards category; My program has a vector of albums(a class). Class Album can set and get the name of the album and ...

  1. #1
    Shadow12345
    Guest

    Can you have a vector of chars?

    My program has a vector of albums(a class). Class Album can set and get the name of the album and artist.
    I want to have two vectors of chars, one is AlbumNameVector(ANV) and the other is Artist Name Vector(ArtV).

    is the following legal?


    Code:
    while(EnteringData) {
    
    AV.push_back(new Album);//adds a new Album to the AlbumVector
    
    ANV.push_back(new char[50]);	//adds a new 50 byte character array to the AlbumNameVector
    
    ArtV.push_back(new char[50]);	//adds a new 50 byte character array to the ArtistVector
    }
    MY PROGRAM IS NOT WOKRING, FOR SOME REASON IT SKIPS OVER ALBUM NUMBER 0, that is my biggest problem.


    I have decided to include what i have for the full source in case you wanted to look
    [code]
    #include <iostream>
    #include <windows.h>
    #include <conio.h>
    #include <vector>
    #include <string.h>
    #include <fstream>

    using namespace std;

    class Album {
    public:

    void SetAlbumName(char *name) {strncpy(AlbumName, name, 50);}
    void SetArtistName(char *name) {strncpy(ArtistName, name, 50);}
    void SetSongName(char *name) {strncpy(SongName, name, 50);}
    char* GetAlbumName() {return AlbumName;}
    char* GetArtistName() {return ArtistName;}
    char* GetSongList() {return SongName;}
    protected:
    char AlbumName[50];
    char ArtistName[50];
    char SongName[50];
    };

    int main() {

    int Answer;
    int Index = 0;
    bool EnteringData;

    typedef vector<Album*> AlbumVector;
    typedef vector<char*> AlbumNameVector;
    typedef vector<char*> ArtistVector;
    typedef vector<char*> SongListVector;

    AlbumVector AV;
    AlbumNameVector ANV;
    ArtistVector ArtV;
    SongListVector SLV;

    cout << "Would you like the enter an album to the database? " << endl; //asks user if he/she would like to enter information
    cout << "1) Yes " << endl << "2) No " << endl;
    cin >> Answer;
    if(Answer == 1)
    EnteringData = true;
    else
    EnteringData = false;
    while(EnteringData) {

    AV.push_back(new Album); //adds a new Album to the AlbumVector
    ANV.push_back(new char[50]); //adds a new 50 byte character array to the AlbumNameVector
    ArtV.push_back(new char[50]); //adds a new 50 byte character array to the ArtistVector
    SLV.push_back(new char[50]); //adds a new 50 byte character array to the SongListVector
    cout << "Enter the name of album #" << Index<< endl; //asks user the name of the album
    cin.getline(ANV[Index],50); //Inputs the Album name
    AV[Index]->SetAlbumName(ANV[Index]); //Sets the name of the Album[Index] (uses class methods)
    Index++;
    }
    cout << "End of program" << endl;
    getch();
    system("CLS");
    return 0;
    }






    //1) store album name, artist name, and list of songs in a database
    //create class album, derive artist name from class album
    //derive song list from class album
    //album name, artist name, song list



    //2) be able to read the album name, artist name, and list of songs from database
    //3) be able to search the database based on user input
    Last edited by Shadow12345; 06-10-2002 at 01:01 PM.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    instead of passing "new char[50]" to std::vector:ush_back(), allocate memory for an arry, than pass that array.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    My question is why are you using so many vectors when a vector of Album objects will work just as well? You could use the C++ string class and a single vector of Album objects. Use a buffer to get input from the user and then add that input to the corresponding member of each new object. This would simplify your program greatly.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Therin lies the power of C++ - OOP

  5. #5
    Shadow12345
    Guest
    yeah

    vectors are so COOL though!
    Last edited by Shadow12345; 06-10-2002 at 09:50 PM.

  6. #6
    Shadow12345
    Guest
    blah
    Last edited by Shadow12345; 06-11-2002 at 02:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while condition question..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 04-04-2009, 05:49 PM
  2. Counting how many chars in a string
    By tigs in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 01:25 AM
  3. really got stuck with unsigned chars
    By Abdi in forum C Programming
    Replies: 7
    Last Post: 06-11-2002, 01:47 PM
  4. move chars position
    By SpuRky in forum C Programming
    Replies: 3
    Last Post: 06-09-2002, 03:59 AM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 05:29 PM

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