create sized dynamic array of class private members

For my project I'm working on, I have to create a dynamic character array to model my categories like name and title that are private in their class. The arrays also have to be the exact length of the strings plus 1 for the null character. I'm not exactly sure how to implement this though. I would appreciate any help/pointers if you have them.

///myUtil.cpp///////////////////


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "myUtil.h"

int getInt()
{
        int     temp;

        cin >> temp;
        while(!cin)
        {
                cin.clear();
                cin.ignore(MAX_CHAR, '\n');

                cout << "You entered an illegal integer. Please try again: ";
                cin >> temp;
        }
        cin.ignore(MAX_CHAR, '\n');
        return temp;
}

float getFloat()
{
        float   temp;

        cin >> temp;
        while(!cin)
        {
                cin.clear();
                cin.ignore(MAX_CHAR, '\n');

                cout << "You entered an illegal floating point value. Please try again: ";
                cin >> temp;
        }
        cin.ignore(MAX_CHAR, '\n');
        return temp;
}

char getChar()
{
        char    temp;

        cin >> temp;
        while(!cin)
        {
                cin.clear();
                cin.ignore(MAX_CHAR, '\n');

                cout << "You need to enter a character. Please try again: ";
                cin >> temp;
        }
        cin.ignore(MAX_CHAR, '\n');
        return temp;
}

void getString(char str[], int maxChar)
{
        cin.get(str, maxChar, '\n');
        while(!cin)
        {
                cin.clear();
                cin.ignore(maxChar, '\n');

                cout << "You did not enter anything. Please try again: ";
                cin.get(str, maxChar, '\n');
        }
        cin.ignore(MAX_CHAR, '\n');
}




//myUtil.h/////////////////////////////


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef MYUTIL_H
#define MYUTIL_H

#include <iostream>
using namespace std;
#include <cstring>

const int MAX_CHAR = 101;

int getInt();
float getFloat();
char getChar();
void getString(char str[], int maxChar);

#endif 




Last edited on
////songlist.cpp////////////////////////////////////////////////////


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "songlist.h"
#include<cstring>
#include <fstream>

SongList::SongList()
{
        //Song* list;
        //list=new Song[CAP];
        size = 0;
}

SongList::~SongList()
{
        //delete[] list;
}

void SongList::addSong(const Song& aSong)
{
        list[size] = aSong;
        size++;
}

void SongList::listAll() const
{
        for(int i=0; i<size; i++)
        {
                list[i].print();
        }
}

void SongList::loadSongs(const char fileName[])
{
        ifstream        in;
        char            title[MAX_CHAR];
        char            album[MAX_CHAR];
        char            artist[MAX_CHAR];
        char            buffer[MAX_CHAR];
        char            *pch;
        float           min;
        float           sec;

        Song currSong;
        in.open(fileName);
        if(!in)
        {
                cerr << "Failed to open " << fileName << " for reading!" << endl;
                exit(1);
        }

          while( in.getline(buffer, MAX_CHAR) )
        {

                char temp[5][MAX_CHAR];
                int counter;

                counter=0;
                pch=strtok (buffer,";");

                while(pch!=nullptr)
                {
                        strcpy(temp[counter], pch);
                        pch = strtok (nullptr, ";");
            counter++;
                }

                //cout<<"string length is: "<<strlen(temp[0])<<endl;
                strcpy(title, temp[0]);
                strcpy(artist, temp[1]);
                strcpy(album, temp[2]);
                min=atoi(temp[3]);
                sec=atoi(temp[4]);
                cout<<"Title: "<<title<<endl;
                cout<<"Artist: "<<artist<<endl;
                cout<<"Album: "<<album<<endl;
                cout<<"Min: "<<min<<endl;
                cout<<"Sec: "<<sec<<endl<<endl;


                currSong.setTitle(title);
                currSong.setArtist(artist);
                currSong.setAlbum(album);
                currSong.setMin(min);
                currSong.setSec(sec);
                addSong(currSong);
        }
        in.close();
}

void SongList::removeIndex()
{
        char    title[MAX_CHAR];
        char    artist[MAX_CHAR];
        char    album[MAX_CHAR];
        float   min;
        float   sec;
        int     i;
        int     index;
        int     a;

        cout<<"Please enter the index number of the song you wish to remove: ";
        cin>>index;
        while(!cin)
        {
                cin.clear();
                cin.ignore(MAX_CHAR, '\n');
                cout << "You entered an illegal integer. Please try again: ";
                cin >> index;
        }
        for(i=index;i<size;i++)
        {
                a=i+1;
                list[a].getTitle(title);
                list[a].getArtist(artist);
                list[a].getAlbum(album);
                min=list[a].getMin();
                sec=list[a].getSec();

                list[i].setTitle(title);
                list[i].setArtist(artist);
                list[i].setAlbum(album);
                list[i].setMin(min);
                list[i].setSec(sec);
        }
        size--;
}

int SongList::searchArtist() const
{
        char    title[MAX_CHAR];
        char    artist[MAX_CHAR];
        char    Artist[MAX_CHAR];
        char    album[MAX_CHAR];
        float   min;
        float   sec;
        int     i;

        i=0;
        cout<<"Please enter the name of the song's artist: ";
        cin.get(Artist, MAX_CHAR, '\n');
        cin.ignore(MAX_CHAR, '\n');
        char *b1=Artist;
        strcpy(b1, Artist);
        int Compare;
        char *b2=artist;
        cout<<"Searching..."<<endl<<endl;
        do
        {
                do
                {
                        list[i].getArtist(artist);
                        Compare=strcmp(b1,b2);
                        b2=artist;
                        i++;
                        if(i>size)
                        {
                                cout<<"End of library reached."<<endl;
                                return 1;
                        }
                 }while(Compare!=0);

                i=i-1;
                list[i].getTitle(title);
                list[i].getArtist(artist);
                list[i].getAlbum(album);
                min=list[i].getMin();
                sec=list[i].getSec();
                cout<<"Title: "<<title<<endl;
                cout<<"Artist: "<<artist<<endl;
                cout<<"Album: "<<album<<endl;
                cout<<"Duration: "<<min<<"."<<sec<<endl<<endl;
                i++;
        }while(i<size);
        return 0;
}


int SongList::searchAlbum() const
{
        char    title[MAX_CHAR];
        char    artist[MAX_CHAR];
       char    album[MAX_CHAR];
        char    Album[MAX_CHAR];
        float   min;
        float   sec;
        int     i;

        i=0;
        cout<<"Please enter the name of the song's album: "<<endl;
        cin.get(Album, MAX_CHAR, '\n');
        cin.ignore(MAX_CHAR, '\n');
        char *b1=Album;
        strcpy(b1, Album);
        int Compare;
        char *b2=album;
        cout<<"Searching..."<<endl<<endl;
        do
        {
                do
                {
                        list[i].getAlbum(album);
                        Compare=strcmp(b1,b2);
                        b2=album;
                        i++;
                        if(i>size)
                        {
                                cout<<"End of library reached."<<endl;
                                return 1;
                        }
                 }while(Compare!=0);

                i=i-1;
                list[i].getTitle(title);
                list[i].getArtist(artist);
                list[i].getAlbum(album);
                min=list[i].getMin();
                sec=list[i].getSec();
                cout<<"Title: "<<title<<endl;
                cout<<"Artist: "<<artist<<endl;
                cout<<"Album: "<<album<<endl;
                cout<<"Duration: "<<min<<"."<<sec<<endl<<endl;
                i++;
        }while(i<size);
        return 0;
}
void SongList::saveSongs(const char fileName[]) const
{
        char            title[MAX_CHAR];
        char            artist[MAX_CHAR];
        char            album[MAX_CHAR];
        float           min;
        float           sec;
        ofstream        out;

        out.open(fileName);
        if(!out)
        {
                cerr << "Failed to open " << fileName << " for writing!" << endl;
                exit(1);
        }

        for(int i=0; i<size; i++)
        {
  list[i].getTitle(title);
                list[i].getArtist(artist);
                list[i].getAlbum(album);
                min = list[i].getMin();
                sec = list[i].getSec();
                out << title << ';' <<artist<<";"<<album<<";"<< min << ";"<<sec<<endl;
        }
        out.close();
}


/////songlist.h///////////////////////////////////////////////////

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "song.h"

class SongList
{
public:
        SongList();
        ~SongList();


        void addSong(const Song& aSong);
        void removeIndex();
        void listAll() const;
        void loadSongs(const char fileName[]);
        void saveSongs(const char fileName[]) const;
        int  searchAlbum() const;
        int  searchArtist() const;
private:
        const static int CAP = 10;
        Song    list[CAP];
        int     size;
};


Last edited on
//////song.cpp/////////////////////////////////////////////////////////


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "song.h"

Song::Song()
{
        init("no title","No artist","No Album", 0, 0);
}

Song::Song(const char name[])
{
        init(title,"No artist","No album", 0, 0);
}

Song::Song(const char title[], float min)
{
        init(title,"No artist", "No album", min, 0);
}

void Song::init(const char title[],const char artist[], const char album[], float min, float sec)
{
        strcpy(this->title, title);
        strcpy(this->artist, artist);
        strcpy(this->album, album);
        this->min = min;
        this->sec=sec;
}

Song::~Song(){}

void Song::getTitle(char title[]) const
{
        strcpy(title, this->title);
}

void Song::setTitle(const char title[])
{
        strcpy(this->title, title);
}
void Song::getArtist(char artist[]) const
{
        strcpy(artist, this->artist);
}

void Song::setArtist(const char artist[])
{
        strcpy(this->artist, artist);
}

void Song::getAlbum(char album[]) const
{
        strcpy(album, this->album);
}

void Song::setAlbum(const char album[])
{
        strcpy(this->album, album);
}

void Song::setMin(float min)
{
        this->min = min;
}
void Song::setSec(float sec)
{
        this->sec=sec;
}

float Song::getSec() const
{
        return sec;
}

void Song::readIn()
{
        cout << "Please enter the song's Title: ";
        getString(title, MAX_CHAR);
        cout<< "Please enter the song's artist: ";
        getString(artist, MAX_CHAR);
        cout<<"Please enter the song's album: ";
        getString(album, MAX_CHAR);
        cout << "Please enter how many minutes are in the song: ";
        min = getFloat();
        cout<<"Please enter how many seconds remain in the song after the minutes you entered above are up: ";
        sec=getFloat();
}

void Song::print() const
{
        cout << title << ": " << artist<<":"<<album<< ":"<<min << ":"<<sec<<endl;
}


//////////song.h/////////////////////////////////////////////////////////////////////////////////


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef SONG_H
#define SONG_H

#include "myUtil.h"

class Song
{
public:
        Song();
        Song(const char title[]);
        Song(const char title[], float min);
        ~Song();

        void getTitle(char title[]) const;
        void setTitle(const char title[]);
        void getArtist(char artist[]) const;
        void setArtist(const char artist[]);
        void getAlbum(char album[]) const;
        void setAlbum(const char album[]);
        float getMin() const;
        void setMin(float min);
        float getSec() const;
        void setSec(float sec);
        void print() const;
        void readIn();
private:
        char    title[MAX_CHAR];
        char    artist[MAX_CHAR];
        char    album[MAX_CHAR];
        float   min;
        float   sec;
        void init(const char title[],const char artist[], const char album[], float min, float sec);
};

#endif 



////////ui.cpp///////////////////////////////////////////////////////////////////////////////


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "ui.h"
#include "myUtil.h"

void displayMenu(int &cmd);

void start()
{
        char            command;
        SongList        roster;
        char    fileName[] = "music.txt";


        roster.loadSongs(fileName);
        displayMenu();
        command=readInCommand();
        while(command!= 'q')
        {
                executeCommand(command, roster);
                displayMenu();
                command=readInCommand();
        }
        roster.saveSongs(fileName);
}

void displayMenu()
{
        cout << endl << "Welcome to your music library. Please enter one of the following options." << endl
                << "     a. Add new song " << endl
                << "     l. Display library " << endl
                << "     i. Remove song by index number " << endl
                << "     d. Search for a song by album " << endl
                << "     t. Search for a song by artist "<<endl
                << "     q. Quit " << endl << endl;
}

char readInCommand()
{
        char cmd=getChar();
        return tolower(cmd);
}

void executeCommand(char cmd, SongList& list)
{
        Song    aSong;

        switch(cmd)
        {
                case 'a':
                        aSong.readIn();
                        list.addSong(aSong);
                        break;
                case 'l':
                        list.listAll();
                        break;
                case 'i':
                        list.removeIndex();
                        break;
                case 'd':
                        list.searchAlbum();
                        break;
                case 't':
                        list.searchArtist();
                        break;
                case 'q':
                        cout<<"Goodbye"<<endl;
                        break;
                default:
                        cout << "You have enterd an invalid option, please try again." << endl;
                        break;
        }
}



////////ui.h/////////////////////////////////////////////////////////

1
2
3
4
5
6
7
8
9
10
11
#ifndef UI_H
#define UI_H

#include "songlist.h"

void start();
void displayMenu();
char readInCommand();
void executeCommand(char command, SongList& list);

#endif 



//////app.cpp/////////////////////////////////////////////////////////


1
2
3
4
5
6
7
#include "ui.h"

int main()
{
        start();
        return 0;
}



////music.txt///////////////

1
2
3
4
5
6
7
Break;Three Days Grace;Life Starts Now;3;13
Joker And The Thief;Wolfmother;Wolfmother;4;40
Come with Me Now;Kongos;Lunatic;3;31
I Am Machine;Three Days Grace;Human;3;20
Help Is on the Way;Rise Against;Endgame;3;57
Black Hole Sun;Soundgarden;Superunknown:5:18;3;57
Immigrant Song;Led Zeppelin;Led Zeppelin 3;2;26

Last edited on
C'mon dude, you must have seen nicer formatting on this forum by now and wondered how.
http://www.cplusplus.com/articles/jEywvCM9/
I have and my code didn't look like this in the submission section. All the tabbing disappeared when I submitted it. Not sure why the code block didn't work though.

Edit: I see from the link I can highlight and click source code so I'll try that instead
Last edited on
Topic archived. No new replies allowed.