Fortune Tester App

I'm new to c++ programming. So I need help with My code it is just a simple concept which gives you 10 $ if you rolled a dice (greater than 4).. so when I compile it, If I have six as my number it says Oops Better Luck Next time How to fix it ??

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
#include <iostream>
#include <cstdlib>
#include <ctime>
int money = 10;


void basics (int num)
{
	std::cout << "Hi There, This is a Fortune tester Game !!" << std::endl;
	if (num == 1) {
		std::cout << "Press \"ENTER\" To Roll The die!!." << std::endl;
	} 
	std::cin.ignore();
}

int dieroll () {
	int ran;
	srand(time(NULL));
	ran = rand()%6+1;
	std::cout << "You Rolled a : " << ran << "\n";
	return ran;
}

int check (int total) {
	if (total > 4) {
		std::cout << "Oh you Just Won 10 Dollars!!\n" << std::endl;
		money += 10;
		std::cout << "Oh your Balance is " << money << std::endl;
		return 0;
	} else if (total < 3){
		std::cout << "Oops !! Better Try Again!!" << std::endl;
		 
	}
	return 1;
}

int main ()
{
	int firstdie, total, result;
	do {
		basics(1);
		firstdie = dieroll();
		result = check(total);
	} while (check(total) != 0);
	return 0;
}

You should only call srand once at the start of your program and not each time you run dieroll

In your main, you call dieroll and store its returned value in firstdie, then you call the check function passing total as its argument but total is a uninitialised variable declared in main.

So in a nutshell, check is being passed some random garbage value - You should be passing the result returned from dieroll into check


So your main would look something like this. Note I changed the while statement because in your code on line 43 you run check, then you run check again in the while loop so technically you would end up with double the money.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

int main()
{
	int dice, checkFlag;

	// set seed here and only once.
	srand(time(NULL));

	do
	{
		basics(1);
		dice = dieroll();
		checkFlag = check(dice);  // pass dieroll result to check

	} while (checkFlag !=0);

	return 0;
}
Topic archived. No new replies allowed.