Vector shuffle print

Hello, I've a txt file (musica.txt) and I'm trying to shuffle the vector that contains it (vector<Musica> music) and print 10 random musics.

But , somehow, this isnt working.

Error:

error C2440: 'initializing' : cannot convert from 'std::_Vector_iterator<_Myvec>' to 'std::_Vector_iterator<_Myvec>'




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
int play_radio(string numero)

{
	std::srand ( unsigned ( std::time(0) ) );
	Musica person;
	vector<Musica> music = vector<Musica>();
	//vector<Musica> music;
	ifstream dataFile("musica.txt");
	string _iD, _title, _artist, _author, _album, _genre, _year = "";


	string line;

	getline(dataFile, line);


	const char comma = ',';
	while (getline(dataFile, line))
	{
		istringstream ss(line);


		if(!line.empty())
		{
			//cout << "passei por aqui" << endl;
			//userList = splitUserString(line);
			//parse(line,",",userList);
			getline(ss, _iD, comma);
			person.setID(_iD);
			getline(ss, _title, comma);
			person.setTitle(_title);
			getline(ss, _artist, comma);
			person.setArtist(_artist);
			getline(ss, _author, comma);
			person.setAuthor(_author);
			getline(ss, _album, comma);
			person.setAlbum(_album);
			getline(ss, _genre, comma);
			person.setGenre(_genre);
			getline(ss, _year, comma);
			person.setYear(_year);

			music.push_back(person);
		}

	
	}
	dataFile.close();
  

  // set some values:
  //for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9

  // using built-in random generator:
  random_shuffle( music.begin(), music.end() );

  std::cout << "myvector contains:";
  for (vector<int>::iterator it=music.begin(); it!=music.end(); it++)
    std::cout <<  ' ' << *it;

  std::cout << '\n';

  return 0;


  
}


Could you help me please?

Thanks in advance

Best regards
The code you showed has nothing common with the error message. The error message refers to _Myvec but there is no such identifier in the code.

Nevertheless your code is invalid. You are declaring an iterator of type vector<int>::iterator and initializing it with an iterator of type vector<Musica>::iterator

for (vector<int>::iterator it=music.begin(); it!=music.end(); it++)
std::cout << ' ' << *it;
Last edited on
Stupid mistake, Im sorry.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
cout << "Musicas que estao a tocar na radio:\n";
	cout << '\n';
	for(int i = 0; i<numero; i++)
	{
		cout << "ID:  " << music[i].getID() << endl;
		cout << "Title:  " << music[i].getTitle() << endl;
		cout << "Artist: " << music[i].getArtist() << endl;
		cout << "Author: " << music[i].getAuthor() << endl;
		cout << "Album:  " << music[i].getAlbum() << endl;
		cout << "Genre:  " << music[i].getGenre() << endl;
		cout << "Year:   " << music[i].getYear() << endl;
		cout << '\n';
		cout << '\n';}

	

	return 0;



}



Working as intended.

Thank you for help :D
Last edited on
By the way in your original code you are skipping the first line of the file and starting to process from the second line

1
2
3
4
5
	getline(dataFile, line);


	const char comma = ',';
	while (getline(dataFile, line))


I do not know whether it is an error because I do not know the structure of the file.
Topic archived. No new replies allowed.