Constructor

I'm trying to make a text game like Zork and currently I'm trying to get a constructor to make a randomly generated number. Then with that number, print a different attack dialogue so it isn't always "The thing hits you". I'm not sure what's wrong as the compiler just runs and gives no errors. Any tips would be appreciated.

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
  struct Variants{
	std::string thing = "option 1";
	std::string thing2 = "option 2";
	std::string thing3 = "option 3";

	unsigned F;

	Variants(){
		srand(time(NULL));
		unsigned V = rand() % 1 + 3;

		V = F;
	}

	void print(){
		if (F == 1){
			std::cout << thing;
		}
		else if (F == 2){
			std::cout << thing2;
		}
		else if (F == 3){
			std::cout << thing3;
		}
	}
};

int main(){
	Variants * V1 = new Variants;

	V1->print();

	delete V1;

	return 0;
}
Do not call srand() multiple times (i.e. inside your constructor). srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/

Line 10,12: You set V to a random number, then you overlay V with the uninitialized value F.
I suspect you meant:
 
    F = V;

Ohh, ok that makes sense, thank you very much.
Topic archived. No new replies allowed.