How to use an arrray with vectors and structs?

I am making a song player simulator program. Right now i am just trying to get the playlist to show all the songs i input. i tried making a for loop and adding in array but it gives me errors because i don't know the proper way to use arrays with vectors. i am not very good with vectors either.

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
 #include <iostream>
#include <vector>
#include <string>
using namespace std;



struct song{

string title;
string artist;
double length;

};

vector<song>list;



char input(char choice, char &done, song &title, song &newSong, song &artist, song &length){

do{

cout<<"(1) Add a song""\n"<<endl;
cout<<"(2) Sort by title"<<endl;
cout<<"(3) Sort by artist"<<endl;
cout<<"(4) Sort by length"<<endl;
cout<<"(5) Print your playlist"<<endl;
cout<<"(6) Exit""\n"<<endl;

cout<<"Enter your option: ";
cin>>choice; cout<<"\n";
cout<<"\n";
cout<<"\n";

do{

if(choice=='1'){

cout<<"Title: ";
cin.ignore();
getline(cin, newSong.title);
list.push_back(title);

cout<<"Artist: ";

getline(cin, newSong.artist);
list.push_back(artist);

cout<<"Length: ";
cin>>newSong.length;
list.push_back(length);

cout<<"\n";
cout<<"\n";

cout<<"Done adding?";
cin>>done; cout<<"\n";

}


}while (done=='n');





if(choice=='2'){



}

else if(choice=='3'){


}

else if(choice=='4'){


}

else if(choice=='5'){


cout<<"-----Current Playlist----- "; cout<<"\n";
cout<<"Title: "<<newSong.title; cout<<"\n";
cout<<"Arist: "<<newSong.artist; cout<<"\n";
cout<<"Length: "<<newSong.length; cout<<"\n";
cout<<"\n";



cout<<"Other options""\n";
cout<<"\n";


}

else if(choice=='6'){

return 0;

}

}

while(done=='y');



}



int main(){

vector<song>list;


song title;
song artist;
song length;

song newSong;



list.push_back(artist);
list.push_back(length);


char choice;
char done;

choice = input(choice, done, title, newSong, artist, length);


}
You need to pass your vector to your input function, and I don't see why you whould need to pass 4 structs to that function.
Last edited on
1
2
3
4
5
6
song new_song;
new_song.artist = artist;
new_song.title = title;
new_song.length = 1.0;

list.push_back(new_song);


:)
Here is an example

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
#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct song
{
	string title;
	string artist;
	double length;
};

char input(vector<song> &list)
{
	char done = 'n';
	char choice = 0;
	int count = 0;
	string temp;
	song tempsong;
	
	do
	{/*enter do while loop*/

		//output options to the screen
		cout<<"(1) Add a song"<<endl;
		cout<<"(2) Sort by title"<<endl;
		cout<<"(3) Sort by artist"<<endl;
		cout<<"(4) Sort by length"<<endl;
		cout<<"(5) Print your playlist"<<endl;
		cout<<"(6) Exit""\n"<<endl;
		cout<<"Enter your option: ";
		cin>>choice; 
		cout<<"\n";
		cin.ignore(100, '\n');
		cout << endl << endl;

		if(choice=='6')
		{
			return 0;
		}
		else if(choice=='1')
		{
			do
			{/*enter do while loop*/
				count++;
				cout<<"Title: ";
				getline(cin, tempsong.title);

				cout<<"Artist: ";
				getline(cin, tempsong.artist);

				cout<<"Length: ";
				cin >> tempsong.length;
				cin.ignore();
				cout << endl << endl;
				list.push_back(tempsong);
				
				cout<<"Done adding?";
				cin >> done; cout<<"\n";
				cin.ignore(100, '\n');
				
			}while (done=='n');
		}/*end of if*/
		else if(choice=='2')
		{
		}

		else if(choice=='3')
		{
		}

		else if(choice=='4')
		{
		}
		else if(choice=='5')
		{
			cout<<"-----Current Playlist----- "<<"\n";
			for(int i = 0; i < count; i++)
			{
			cout<<"Title: "<<list[i].title; cout<<"\n";
			cout<<"Arist: "<<list[i].artist; cout<<"\n";
			cout<<"Length: "<<list[i].length; cout<<"\n";
			cout<<"\n";
			cout<<"\n";
			}/*end of for loop*/
			cout << "Press enter to continue";
			cin.ignore();
		}/*end of if*/
	}while(done=='y');
}

int main()
{

	vector<song>list;
	char choice;
	choice = input(list);
	
	cin.ignore();
	return 0;
}


You should break this up into better functions though
Last edited on
Topic archived. No new replies allowed.