Error

Been trying to figure out how to print out the right numbers from the class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
int Die::get_value()
{
	return value;
}
void Die::roll()
{
	int value;
	srand (time(NULL));
	value = rand() % 6 +1;
	return;
}



 cout << "Your initial rolls were: " << die1.get_value() << "and " << d.get_value() << "for a total of " /*<< total*/;
EDIT: As per MiiNiPaa's suggestion, it is likely that you did not intend the "value" variable to be local to the function. Try something like this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Die
{
	void roll() {value = rand() % 6 +1;}
	int get_value() { return value; }

	int value;
};

int main()
{
	srand (time(NULL));
	Die die1;
	Die d;

	int total = die1.get_value() + d.get_value();
	cout << "Your initial rolls were: " << die1.get_value() << "and " << d.get_value() << "for a total of " << total;
	return 0;
}
Last edited on
What is the problem? You don't show enough code to guess what you lack.

[Edit] Apart from that local variable issue ...
Last edited on
your value variable is local to roll() function and cannot be used outside it. Move it in private members part of your class
Topic archived. No new replies allowed.