String Vectors only working for one iteration

I am making a bird call quiz game and I am using a string vector to store all of the PlaySound commands for each different file. When types in the first answer, and gets it right, then the music should stop and it does stop, but then the next bird song must play, but it isn't. It is something to do with the vector because if you uncomment the line I put in the if statement to see if they got it right, then that song plays, and it's not in a vector.

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
#include "header.h"


int main()
{
	srand(time(0));
	vector<string> birds(26);

	//DISC 1
	birds.at(0) = PlaySound(TEXT("disc1\\Track2\\1Northern Mocking Bird.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(1) = PlaySound(TEXT("disc1\\Track2\\2Brown Thrasher.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(2) = PlaySound(TEXT("disc1\\Track2\\3Cat Bird.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(3) = PlaySound(TEXT("disc1\\Track3\\1Downy Woodpecker.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(4) = PlaySound(TEXT("disc1\\Track3\\2Hairy Woodpecker.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(5) = PlaySound(TEXT("disc1\\Track3\\3Red Headed Woodpecker.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(6) = PlaySound(TEXT("disc1\\Track3\\4Red Bellied Woodpecker.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(7) = PlaySound(TEXT("disc1\\Track3\\5Northern Flicker.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(8) = PlaySound(TEXT("disc1\\Track3\\6Pileated Woodpecker.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(9) = PlaySound(TEXT("disc1\\Track4\\1American Robin.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(10) = PlaySound(TEXT("disc1\\Track4\\2Scarlet Tanager.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(11) = PlaySound(TEXT("disc1\\Track4\\3Summer Tanager.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(12) = PlaySound(TEXT("disc1\\Track4\\4Rose-breasted Grosbeak.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(13) = PlaySound(TEXT("disc1\\Track4\\5Red-eyed Virio.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(14) = PlaySound(TEXT("disc1\\Track4\\6Yellow-throated Virio.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(15) = PlaySound(TEXT("disc1\\Track5\\1Broad-winged Hawk.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(16) = PlaySound(TEXT("disc1\\Track5\\2Red-tailed Hawk.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(17) = PlaySound(TEXT("disc1\\Track5\\3Red-shouldered Hawk.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(18) = PlaySound(TEXT("disc1\\Track6\\1Swamp Sparrow.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(19) = PlaySound(TEXT("disc1\\Track6\\2Chipping Sparrow.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(20) = PlaySound(TEXT("disc1\\Track6\\Dark-eyed Junko.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(21) = PlaySound(TEXT("disc1\\Track6\\4Pine-warbler.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(22) = PlaySound(TEXT("disc1\\Track7\\1Cedar Waxwing.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(23) = PlaySound(TEXT("disc1\\Track7\\2Brown-headed Cowbird.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(24) = PlaySound(TEXT("disc1\\Track7\\3Eastern Kingbird.wav"),NULL,SND_FILENAME|SND_ASYNC);
	birds.at(25) = PlaySound(TEXT("disc1\\Track7\\4European Starling.wav"),NULL,SND_FILENAME|SND_ASYNC);


	
	
	vector<string> names(26);
	names.at(0) = "Northern Mocking Bird";
	names.at(1) = "Brown Thrasher";
	names.at(2) = "Cat Bird";
	names.at(3) = "Downy Woodpecker";
	names.at(4) = "Hairy Woodpecker";
	names.at(5) = "Red Headed Woodpecker";
	names.at(6) = "Red Bellied Woodpecker";
	names.at(7) = "Northern Flicker";
	names.at(8) = "Pileated Woodpecker";
	names.at(9) = "American Robin";
	names.at(10) = "Scarlet Tanager";
	names.at(11) = "Summer Tanager";
	names.at(12) = "Rose-breasted Grosbeak";
	names.at(13) = "Red-eyed Virio";
	names.at(14) = "Yellow-throated Virio";
	names.at(15) = "Broad-winged Hawk";
	names.at(16) = "Red-tailed Hawk";
	names.at(17) = "Red-shouldered Hawk";
	names.at(18) = "Swamp Sparrow";
	names.at(19) = "Chipping Sparrow";
	names.at(20) = "Dark-eyed Junko";
	names.at(21) = "Pine-warbler";
	names.at(22) = "Cedar Waxwing";
	names.at(23) = "Brown-headed Cowbird";
	names.at(24) = "Eastern Kingbird";
	names.at(25) = "European Starling";

	int num = rand()%25;
	bool go = true;

	while(true)


	{
		//PlaySound(NULL,NULL,SND_FILENAME);

		if(go)
			 num = rand()%25;

		birds.at(num);
		cout << num;

		string enter;
		getline(cin, enter);	

		if(enter == "0")
			break;

		if(enter.compare(names.at(num)) == 0) //.compare returns a 0 if they are equal 
		{
			cout << "Correct";
			go = true;
			PlaySound(NULL, 0, 0); // Stop previous sound

                        //DEBUGGING
                        //If this next line is uncommented, then that file will                             
                       //play, but the vector files won't play  
			/*PlaySound(TEXT("disc1\\Track2\\1Northern Mocking Bird.wav"),NULL,SND_FILENAME|SND_ASYNC);*/
                        //DEBUGGING
		}
		else
		{
			cout << "Wrong";
			go = false;
		}
			
		
		//cin.get(); // wait
		system("pause");
		//PlaySound(NULL, 0, 0);


		/*int x;
		cin >> x;

		if(x == 0)
			break;*/
		
		


	}
return 0;
}
I am using a string vector to store all of the PlaySound commands for each different file.
I don't know what PlaySound() does*, but it returns a string.
If it really returns the command you need to execute it, access it will do nothing
birds.at(num); //warning: statement has no effect

*Probably it's playing the file, that's the first thing that you heard. (¿can you tell us which library are you using?)
You may do it like
1
2
3
4
5
6
7
8
9
	vector<string> birds(26);
	//DISC 1
	birds.at(0) = "disc1\\Track2\\1Northern Mocking Bird.wav";
//...
		if(go)
			 num = rand()%25;

		birds.at(num);
		PlaySound(TEXT( birds.at(num) ),NULL,SND_FILENAME|SND_ASYNC);
TEXT may want a const char * so use TEXT( birds.at(num).c_str() ) if that's the case


I am making a bird call quiz game
Some friends did a bird call recognizer classifier.
Last edited on
First, your vector contains strings, playsound returns bool. So debug and look at the contents of bird. I'm short what you really want is every thing between "" in your bird container. Then when you play the sound use the index of the song filename and call the function.

Tip: I would use a map, where the key its the song name you have stored in name vector, and value the value is everything in "" in the bird vector. Then use find on the map.
@ne555
I tried doing that before but I didn't put the .c_str(), but when I tried it now, it still didn't work, and the libraries that I am using are
1
2
3
4
5
#include <iostream>
#include <Windows.h>// for PlaySound()
#include <mmsystem.h>
#include <vector>
#include <time.h> 

1
2
3
4
 birds.at(0) = "disc1\\Track2\\1Northern Mocking Bird.wav";
	birds.at(1) = "disc1\\Track2\\2Brown Thrasher.wav";
       //.....
PlaySound(TEXT(birds.at(num).c_str()), NULL, SND_FILENAME|SND_ASYNC); 


@clanmjc, could you explain what you're saying?
Last edited on
Topic archived. No new replies allowed.