Random global variable

Im trying to write a text based survival game. I want to have all the resources start at a random number. Where do i define the variables? right now it is defined in main.
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
#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <sstream>
#include <fstream>
#include <cmath>
#include <ctime>

using namespace std;

class adventure
{
		int milk, coke, Koolaid, Budweiser;
public:
	void Situation1();
	int main();

};

void Intro ( void )
{
	int stat;
	cout << "==============================Adventure===============================\n\n";
}
void Situation1 ( void )
{
	int answer;
	cout << "\nYou are at your neighbors house. He asks you if you want somethng to drink";
	cout <<"\n What do you say?: ";
	cout << "\n\n1.) Water \n2.) Kool-aid \n3.) Budweiser \n4.) Milk\n :";
	cin >> answer;
	if (milk <= 5);


}


int main()
{
	system("TITLE Adventure 1.0");
	int milk;
	srand(time(0));
	milk = rand() % 7;
	Intro();
	//cout << milk;
	Situation1();
	system("PAUSE");
	return 0;
}
you can create them in the constructor of the class
How do i set the value to random? I understand i should put int milk; under public?
Public member variables are not the same as a global variable.

Situation1 is a member function of adventure so you have to define the function as
void adventure::Situation1() on line 25.

The main function should not be member of any class. You can create an adventure object in main and call its functions from there.
Last edited on
I get an error saying situation1 cannot be re declared
Topic archived. No new replies allowed.