Crown and Anchor Program

Hello there, I'm working on a C&A program and I'm having a couple of difficulties.
1) I need to save the total value of money as the program continues to loop.
2) The Dice roll results disappear after the 2nd or 3rd roll.
I'm unsure of what is wrong with my code however.
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <string>

using namespace std;

int getRandomNum(int lowRange, int highRange);


int main(int argc, char *argv[])
{
  //apply needed initializers here  
  int amountRoll=0;
  int diceValue=0; // this is the value the dice will actually roll on. (dice roll)
  int roll=0; // this will be the choice I will roll with
  int betMoney=0;  // this will be the money I will bet
  int totalMoney=0; //This will be the money I will input
  char yes= 'y';
  srand( static_cast<int>( time(NULL) ) );
  
  cout <<"Hello! Welcome to the Crown and Anchor Game. "<< endl;
  cout <<"Goodluck! "<< endl;
  cout <<"How much money will you play with? $" ;
  cin >> totalMoney;
  cout << endl << endl << endl;
 
 while (totalMoney!= 0)
 { 
  cout << "*******************************************" << endl << endl;
  
  cout << "Enter your bet $" ;
  cin >> betMoney;
  while( betMoney > totalMoney )
  {
      cout << "Please bet $" << totalMoney << " or lower" <<endl;
      cout << "Enter your bet $" ;
      cin >>betMoney; 
       
  }
  
   cout << "Choose a character. Enter: " << endl;
   cout <<"        1 for Heart"<<endl;
   cout <<"        2 for Crown"<<endl;
   cout <<"        3 for Diamond"<<endl;
   cout <<"        4 for Club"<<endl;
   cout <<"        5 for Anchor"<<endl;
   cout <<"        6 for Spade"<<endl;
   cout <<" Your choice: " ;
   cin>> roll;
   
   while(!(roll>0 && roll <7))
   {
                  cout<< " Please select a number from above "<<endl;
                  cout<< " Your choice :" ;
                  cin >> roll ;
   }
  
  
  cout <<"Dice rolled : "<<endl ;
  while (amountRoll <=2)
  {
  diceValue=getRandomNum(1,6);
  switch(diceValue)
  {
                   case 1:
     
                   cout<<"Heart ";
                   amountRoll++;
                   break;
     
                  case 2:
     
                   cout<<"Crown ";
                   amountRoll++;
                   break;
     
                  case 3:
     
                   cout<<"Diamond ";
                   amountRoll++;
                   break;
      
                  case 4: 
     
                   cout<<"Club ";
                   amountRoll++;
                   break;
      
                   case 5:
     
                   cout<<"Anchor ";
                   amountRoll++;
                   break;
     
                   default:
     
                   cout<<"Club ";
                   amountRoll++;
                   break;
     
   }
  cout <<endl;
  }

if(roll == diceValue )
 {
        cout << "You won $" << betMoney*2 <<endl;
        cout << "Your current balance is $" <<totalMoney+betMoney*2<<endl <<endl;
 }
else
  {
        cout << "You lost $" << betMoney <<endl;
        cout << "Your current balance is $" <<totalMoney-betMoney <<endl<<endl;
  }        
        
cout <<endl;
} 
      
      system("PAUSE");
    return 0;
}


int getRandomNum(int lowRange, int highRange)
{
	int randNum;
	
	randNum =  ( rand() % (highRange - lowRange + 1) ) + lowRange;
	
	return randNum;
}
Last edited on
Look at the comments.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <string>

using namespace std;

int getRandomNum(int lowRange, int highRange);


int main(int argc, char *argv[])
{
	//apply needed initializers here  
	int amountRoll = 0;
	int diceValue = 0; // this is the value the dice will actually roll on. (dice roll)
	int roll = 0; // this will be the choice I will roll with
	int betMoney = 0;  // this will be the money I will bet
	int totalMoney = 0; //This will be the money I will input
	char yes = 'y';
	srand(static_cast<int>(time(NULL)));

	cout << "Hello! Welcome to the Crown and Anchor Game. " << endl;
	cout << "Goodluck! " << endl;
	cout << "How much money will you play with? $";
	cin >> totalMoney;
	cout << endl << endl << endl;

	while (totalMoney != 0)
	{
		cout << "*******************************************" << endl << endl;

		cout << "Enter your bet $";
		cin >> betMoney;
		while (betMoney > totalMoney)
		{
			cout << "Please bet $" << totalMoney << " or lower" << endl;
			cout << "Enter your bet $";
			cin >> betMoney;

		}

		cout << "Choose a character. Enter: " << endl;
		cout << "        1 for Heart" << endl;
		cout << "        2 for Crown" << endl;
		cout << "        3 for Diamond" << endl;
		cout << "        4 for Club" << endl;
		cout << "        5 for Anchor" << endl;
		cout << "        6 for Spade" << endl;
		cout << " Your choice: ";
		cin >> roll;

		while (!(roll>0 && roll <7))
		{
			cout << " Please select a number from above " << endl;
			cout << " Your choice :";
			cin >> roll;
		}

		amountRoll = 0; // You need to set this to zero before every new roll.
		cout << "Dice rolled : " << endl;
		while (amountRoll <= 2)
		{
			diceValue = getRandomNum(1, 6);
			switch (diceValue)
			{
			case 1:

				cout << "Heart ";
				amountRoll++;
				break;

			case 2:

				cout << "Crown ";
				amountRoll++;
				break;

			case 3:

				cout << "Diamond ";
				amountRoll++;
				break;

			case 4:

				cout << "Club ";
				amountRoll++;
				break;

			case 5:

				cout << "Anchor ";
				amountRoll++;
				break;

			default:

				cout << "Club ";
				amountRoll++;
				break;

			}
			cout << endl;
		}

		if (roll == diceValue)
		{
			cout << "You won $" << betMoney * 2 << endl;
			cout << "Your current balance is $" << totalMoney + betMoney * 2 << endl << endl;
			totalMoney += betMoney * 2; //Update your totalMoney here.
		}
		else
		{
			cout << "You lost $" << betMoney << endl;
			cout << "Your current balance is $" << totalMoney - betMoney << endl << endl;
			totalMoney -= betMoney; //Need to update your money here as well.
		}
		
		cout << endl;
	}

	system("PAUSE");
	return 0;
}


int getRandomNum(int lowRange, int highRange)
{
	int randNum;

	randNum = (rand() % (highRange - lowRange + 1)) + lowRange;

	return randNum;
}
Last edited on
Oh thank-you! I hadn't noticed I forgot to update the values. :D
Topic archived. No new replies allowed.