Complete Random Function

I need help for the completion of the getColor function that would essentially return the randNum which would select random colors from the array, I am completely lost on how to do this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <string>

class color{
private:
	string colors[7];
public:
	color(void){ colors[0] = "Red"; colors[2] = "Orange"; colors[3] = "Green"; colors[4] = "Blue"; colors[5] = "Indigo"; colors[6] = "Violet"; }
	
	string getColor(){ int randomNum = 4;}
	void printArray(){ for (int i = 0; i < 7; i++){ cout << colors[i] << endl; } }
};
int _tmain(int argc, _TCHAR* argv[])
{
	color C;
	cout << C.getColor << endl << endl;
	C.printArray();

	system("pause");
	return 0;
}
So you want the getColor ( ) function to return a string containing the name of one of seven random colors?

1
2
3
4
5
#include <time.h>

srand ( time ( NULL ) ); // Seed random number generator. Only do this once.

int val = rand ( ) % 7; // Set val equal to a random number between 0 and 6. 


ps: colors[1] isn't initialized in your constructor.
Last edited on
ahh yes i forgot yellow, thank you very much, then i just

return colors[val];

Last edited on
How would I be able to make it so that the array of colors pops up in a random order every time?
If that is possible
Last edited on
Get a random number.
Print the color at that index.
Get another random number that hasn't been used yet.
Print the color at that index.
Repeat.
ok so now i have split my program into a header file, a cpp, and the main cpp, and i have included the header file into my two cpp's. Everything was going well before I debugged it and I had all these errors saying that by object wasn't declared.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Color.cpp

#include "stdafx.h"
#include "Color.h"
#include <iostream>
using namespace std;
#include <string>
#include "time.h"

color::color(){
	colors[0] = "Red"; colors[1] = "Orange"; colors[2] = "Yellow"; colors[3] = "Green"; 
	colors[4] = "Blue"; colors[5] = "Indigo"; colors[6] = "Violet";
}

string color::getColor(){
	srand(time(NULL));
	int randomNum = rand() % 7;
	return colors[randomNum];
}

void color::printArray(){
	for (int i = 0; i < 7; i++){ cout << colors[i] << endl; }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Color.h

#include "stdafx.h"
#include <iostream>
#include "stdafx.h"
using namespace std;




class color{
private:
	string colors[7];
public:
	color(void);

	string getColor();
	
	void printArray();
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// getColorAssignment3JasonRoss.cpp : Defines the entry point for the console application.
//
#include "Color.h"
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{	
	color bo;
	cout << bo.getColor() << endl << endl;
	bo.printArray();
	return 0;
}

Errors:
Error 4 error C2065: 'bo' : undeclared identifier 11
Error 5 error C2065: 'bo' : undeclared identifier 12
Error 7 error C2065: 'bo' : undeclared identifier
Error 2 error C2065: 'color' : undeclared identifier \getcolorassignment3jasonross.cpp 11
Error 3 error C2146: syntax error : missing ';' before identifier 'bo'
Error 6 error C2228: left of '.getColor' must have class/struct/union c:\users\jason\documents\visual studio 2013\projects\getcolorassignment3jasonross\getcolorassignment3jasonross\getcolorassignment3jasonross.cpp 12
Error 8 error C2228: left of '.printArray' must have class/struct/union c:\users
Warning 9 warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
Warning 1 warning C4627: '#include "Color.h"': skipped when looking for precompiled header use


Any ideas of what I did wrong?
Why would my #include "Color.h" be skipped?
Last edited on
You include of "Color.h" was skipped because it was placed before the precompiled header was after it. The #include "stdafx.h" line should ALWAYS be the first line in a cpp file (excluding comments).
Last edited on
Topic archived. No new replies allowed.