I need help getting a function to return a string

Is it possible for a function outside of int main() to return a string?

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
string RandomColorGenerator(int randnum, vector<int> &randcolorvect)
{
	string myrandcolor;
	srand(time(NULL));
	int randcolor;
	
	if(randnum==1)
		myrandcolor = red;
	if(randnum==2)
		myrandcolor = blue;
	if(randnum==3)
		myrandcolor = green;
	if(randnum==4)
		myrandcolor = yellow;
	if(randnum==5)
		myrandcolor = pink;
	if(randnum==6)
		myrandcolor = purple;
	if(randnum==7)
		myrandcolor = lightblue;
	if(randnum==8)
		myrandcolor = lime;
	if(randnum==9)
		myrandcolor = mahogany;
	if(randnum==10)
		myrandcolor = aqua;
	if(randnum==11)
		myrandcolor = violet;
	if(randnum==12)
		myrandcolor = torquoise;
	if(randnum==13)
		myrandcolor = plum;
	if(randnum==14)
		myrandcolor = black;
	if(randnum==15)
		myrandcolor = white;
	if(randnum==16)
		myrandcolor = brown;
	if(randnum==17)
		myrandcolor = grey;
	if(randnum==18)
		myrandcolor = teal;
	if(randnum==19)
		myrandcolor = lightgreen;
	if(randnum==20)
		myrandcolor = fire;
	if(randnum==21)
		myrandcolor = darkblue;
	if(randnum==22)
		myrandcolor = darkgreen;
	if(randnum==23)
		myrandcolor = orange;
	if(randnum==24)
		myrandcolor = peach;
	
	return myrandcolor;
}


I want this program to return a string. I know it is impossible to have string RandomColorGenerator(int randnum, vector<int> &randcolorvect), but I was wondering what I could use instead
Last edited on
Is it possible for a function outside of int main() to return a string?
You need to make the return value a string, and not an int. Change myrandcolor = red; and the other colors, to myrandcolor = "red";
Last edited on
I think this is probably a simpler way of doing what you want to do:

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
string RandomColorGenerator(int randnum)
{
    switch(randnum)
    {
    case 1:    return "red";
    case 2:    return "blue";
    case 3:    return "green";
    case 4:    return "yellow";
    case 5:    return "pink";
    case 6:    return "purple";
    case 7:    return "lightblue";
    case 8:    return "lime";
    case 9:    return "mahogany";
    case 10:   return "aqua";
    case 11:   return "violet";
    case 12:   return "torquoise";
    case 13:   return "plum";
    case 14:   return "black";
    case 15:   return "white";
    case 16:   return "brown";
    case 17:   return "grey";
    case 18:   return "teal";
    case 19:   return "lightgreen";
    case 20:   return "fire";
    case 21:   return "darkblue";
    case 22:   return "darkgreen";
    case 23:   return "orange";
    case 24:   return "peach";
    default:   return "";
    }
}
Last edited on
thanks guys. just forgot the quotation marks
I never really got switches, but if randnum = 5, "pink" would be returned??
Topic archived. No new replies allowed.