String Help

I am supposed to create the "dem bones" song/ The output should be the song, using strings. This is what i have so far, and i don'tknow why the program wont work. any help?

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

/********************************************************************
 * GENERATE SONG
 * This function will generate one long string that constitutes the
 * complete song "Dem Bones."  Note that the list of bones is provided
 * by the parameter list
 ********************************************************************/
string generateSong(string list[], int num)
{
   string output;
   string CTT = "connected to the ";
   // your code goes here
   for(int i = 0; i < 9; i++)

       output = list[i];

   return output;

}

/**********************************************************************
 * MAIN
 * In this program, MAIN is a driver program.  It will feed to generateSong()
 * the list of bones and get back the complete song as a string.  MAIN will
 * then display the song
 ***********************************************************************/
int main()
{
   string list[9] =
   {
      "toe",
      "foot",
      "leg",
      "knee",
      "hip",
      "back",
      "neck",
      "jaw",
      "head"
   };

   // generate the song
   string song = generateSong(list, 9);

   // display the results
   cout << song;

   return 0;
}

any help would be greatly appreciated! thanks
On line 18 you assign each word hence the result will be the last word. To append use += (+ " ")
so far i used
 
output = list[i] + " bone connected to the ";

i want to add the next part to my output soit will say
"toe bone connected to the foot bone"
so i tried to do this
 
output = list[i] + " bone connected to the " + list[i+1];

it compiles, then when i run it nothing happens. any ideas?
Hey man, here is what I have so far. I am not used to working with strings so I have no idea what to do to make this work. But hopefully you get the idea I'm trying to use. If it is too much writing for what you need you can go ahead and ignore this.

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
string generateSong(string list[], int num)
{
   string output;
   char *CTT[1] = "connected to the ";
   char * song[100] =  {"Dem bones, dem bones, dem...dancing bones\n",
	    "Dem bones, dem bones, dem...dancing bones\n",
	    "Dem bones, dem bones, dem...dancing bones\n",
		 "\t Doin' the skeleton dance\n",
	    "The 0 bone is "+CTT +"1 bone,\n",
	    " The 1 bone is "+ CTT + "2 bone,\n",
	    " The 2 bone is "+ CTT + "3 bone,\n",
	    "\t Doin' the skeleton dance\n",
	    " The 3 bone is "+ CTT + "4 bone,\n",
	    " The 4 bone is "+ CTT + "5 bone,\n",
	    " The 5 bone is "+ CTT + "6 bone,\n",
	     "\t Doin' the skeleton dance\n",
	    "\t Shake your hands to the left.\n",
	    "\t Shake your hands to the right.\n",
	    "\t Put your hands in the air.\n",
	    "\t Put your hands out of sight\n",
	    "\t Doin' the skeleton dance\n",
	    " The 7 bone is "+ CTT + "8 bone,\n",
	    "\t Doin' the skeleton dance!"};
	
    int u = 0;
   for(int i = 0; i < 40; i++)
      for (int b = 0;b < sizeof(song[i]) ;b++)
	if (song[i][b] == u)
	{
	  song[i][b] = list[u];
	  u++;
	  break;
	}
	
       output = song;

   return output;

}


So what I tried to do is to use an array to go through the song and where ever there is a number, replace it with the word from list. The above code does not function properly. But at least you get they idea.



E:
This one works! Buuutttt, main does not display the song

E2: Thanks to sebelius, it now works correctly

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

/********************************************************************
 * GENERATE SONG
 * This function will generate one long string that constitutes the
 * complete song "Dem Bones."  Note that the list of bones is provided
 * by the parameter list
 ********************************************************************/
string generateSong(string list[], int num)
{
   string output;
   string CTT = "connected to the ";
   // your code goes here
   for(int i = 0; i+1 < num; i++)
     output += "The "+list[i]+" bone is "+CTT + list[i+1] +" bone\n";
     
   return output;

}

/**********************************************************************
 * MAIN
 * In this program, MAIN is a driver program.  It will feed to generateSong()
 * the list of bones and get back the complete song as a string.  MAIN will
 * then display the song
 ***********************************************************************/
int main()
{
   string list[9] =
   {
      "toe",
      "foot",
      "leg",
      "knee",
      "hip",
      "back",
      "neck",
      "jaw",
      "head"
   };

   // generate the song
   string song = generateSong(list, 9);

   cout << song + "Doin' the skeleton dance!" << endl;//Thank sebelius

   return 0;
}



$ ./DemBones
The toe bone is connected to the foot bone
The foot bone is connected to the leg bone
The leg bone is connected to the knee bone
The knee bone is connected to the hip bone
The hip bone is connected to the back bone
The back bone is connected to the neck bone
The neck bone is connected to the jaw bone
The jaw bone is connected to the head bone
Doin' the skeleton dance!
Last edited on
closed account (9y8C5Di1)
string output="";
for(unsigned int x=0;x<getLenght(list);x++){
output+=list[x];
}output+="toe bone connected to whatever you wanted";
cout<<output<<endl;
@sebelius you are a genius!
Topic archived. No new replies allowed.