Also this is my code and if anyone can see any way to fix it or make it better let me know
Thanks
# include <time.h>
# include <iostream>
using namespace std;
class Dice
{
private:
int human;
int computer;
char choice;
static double humantotal, computertotal;
public:
void getroll();
void display();
void results();
void playagain();
void past21();
};
double Dice::humantotal = 0;
double Dice::computertotal = 0;
void Dice::getroll()
{
srand( time(NULL) );
human = rand() % 6 + 1;
computer = rand() % 6 + 1;
}
void Dice::display()
{
humantotal += human;
computertotal += computer;
cout<<"Your total is: "<<humantotal<<endl;
cout<<"The computer's total is: "<<computertotal<<endl;
}
void Dice::results()
{
if(humantotal > computertotal)
{
cout<<"Congrats you have won!"<<endl;
}
else
{
cout<<"Sorry but the computer has won.."<<endl;
}
}
void Dice::playagain()
{
cout<<"Do you want to roll again? Enter 'y' or 'n':"<<endl;
cin>>choice;
while(choice == 'y' || choice == 'Y')
{
getroll();
display();
past21();
cout<<"Do you want to roll again? Enter 'y' or 'n':"<<endl;
cin>>choice;
}
}
void Dice::past21()
{
if (humantotal > 21)
{
cout<<"Sorry you lost! You have went past 21!"<<endl;
}
}
int main()
{
char choice;
Dice adice;
cout<<"Do you want to play a game of 21? Enter 'y' or 'n':"<<endl;
cin>>choice;
if (choice == 'y' || choice == 'Y')
{
adice.getroll();
adice.display();
adice.playagain();
adice.results();
}
else
{
cout<<"That makes me sad but :(.. but okay!"<<endl;
}
return 0;
}
This is code where I wanted to put the end of program at:
void Dice::playagain()
{
cout<<"Do you want to roll again? Enter 'y' or 'n':"<<endl;
cin>>choice;
while(choice == 'y' || choice == 'Y')
{
getroll();
display();
past21();
//AN ENDPROGRAM() function//
cout<<"Do you want to roll again? Enter 'y' or 'n':"<<endl;
cin>>choice;
}
}

New Topic/Question
Reply



MultiQuote





|