In C++ variables have scope. A variable is generally visible inside the curly braces where it is declared; outside these brackets the variable does not exist.
That is why you cannot use s1
from change1
inside the loop: you need to return a value (best choice in your situation), or use a variable that is in scope in both change1
and main
.
printf ("This old man, he played ");
printf("He played knick-knack on my %s\n\n", change1(i));
...
string change1 (int i) {
string s1;
switch (i) {
...
}
return s1;
}
Note that you do not need a switch statement to implement change1
: when the code is so uniform, you may be better off with an array:
const char *strings[] = {"thumb", "shoe", ...};