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.
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <string>
usingnamespace 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;
}
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <string>
usingnamespace 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;
}