5 Replies - 1707 Views - Last Post: 22 November 2009 - 11:47 PM Rate Topic: -----

#1 Claud745   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 29-October 09

Making a program end

Post icon  Posted 22 November 2009 - 10:56 PM

Hi I have a working code. My project was a game of 21 with dice. The thing is its working right but I just need some code to make it end. Like when you go over 21 it says you lost and ends the game.. but instead it just says you lost and then it asks you to roll again. So what I wanted to do was put a function that just ends the program and not sure what the code for that is.
Also this is my code and if anyone can see any way to fix it or make it better let me know :).
Thanks :D

# 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;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Making a program end

#2 Claud745   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 29-October 09

Re: Making a program end

Posted 22 November 2009 - 11:10 PM

Never mind I figured it out. All I needed to do was modify past21() and add exit(1):

void Dice::past21()
{
	if (humantotal > 21)
	{
		cout<<"Sorry you lost! You have went past 21!"<<endl;
		exit(1);
	}

	if (computertotal >21)
	{
		cout<<"Congrats you have won! The computer has went past 21!"<<endl;
		exit(1);
	}
}



Anyways, If anyone has any tips of improving my code I would appreciate it :).
Was This Post Helpful? 0
  • +
  • -

#3 ccubed   User is offline

  • It's That Guy
  • member icon

Reputation: 165
  • View blog
  • Posts: 1,416
  • Joined: 13-June 08

Re: Making a program end

Posted 22 November 2009 - 11:17 PM

You're code is working fine.

All past21 does is print out a message, which it does and then it just keeps on going to roll again.

What you really want is either a boolean or some other way to know when the game has ended.

or yeah, do that. Also, Exit(0). 1 actually means something went wrong.

This post has been edited by ccubed: 22 November 2009 - 11:18 PM

Was This Post Helpful? 1
  • +
  • -

#4 Mowgef   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: Making a program end

Posted 22 November 2009 - 11:24 PM

2 things. If the player gets over 21 you might make them start over, one can just keep rolling regardless of values.

Also, it doesn't check if the computer goes over 21.
Was This Post Helpful? 0
  • +
  • -

#5 Claud745   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 29-October 09

Re: Making a program end

Posted 22 November 2009 - 11:31 PM

View Postccubed, on 22 Nov, 2009 - 10:17 PM, said:

You're code is working fine.

All past21 does is print out a message, which it does and then it just keeps on going to roll again.

What you really want is either a boolean or some other way to know when the game has ended.

or yeah, do that. Also, Exit(0). 1 actually means something went wrong.


Thanks I didn't know that, ill change that now. :)


View PostMowgef, on 22 Nov, 2009 - 10:24 PM, said:

2 things. If the player gets over 21 you might make them start over, one can just keep rolling regardless of values.

Also, it doesn't check if the computer goes over 21.


I added another if statement for the computer also and it seemed to workout as long as they don't tie... but ill try to fix the tie problem tomorrow.
Was This Post Helpful? 0
  • +
  • -

#6 Claud745   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 29-October 09

Re: Making a program end

Posted 22 November 2009 - 11:47 PM

New version of the game (if anyone is interested in the code):
- Made it more interesting so you don't know what the computer has so that way it's more exciting to play and see if you've won or you've lost.
- Only problem is when it ties.. seems to give the win to the computer.

//Program is a game of 21 with dice//
//Each time you roll it adds up your total untill you choose to stop//
//If your total goes over 21 the computer wins and if the computer goes over 21 first you win and program exits//
# 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 so far is: "<<humantotal<<endl;
}


void Dice::results()
{
	if(humantotal > computertotal)
	{
		cout<<"Congrats you have won! The computer only had "<<computertotal<<"."<<endl;
	}
	else
	{
		cout<<"Sorry but the computer has won.. he had "<<computertotal<<"."<<endl;
	}
}

void Dice::playagain()
{
	cout<<"Do you want to roll again?"<<endl;
	cout<<"Enter 'y' or 'n' if you think you have a closer number than the computer!:"<<endl;
	cin>>choice;
	while(choice == 'y' || choice == 'Y')
	{
	getroll();
	display();
	past21();
	cout<<"Do you want to roll again?"<<endl;
	cout<<"Enter 'y' or 'n' if you think you have a closer number than the computer!:"<<endl;
	cin>>choice;
	}
}

		
void Dice::past21()
{
	if (humantotal > 21)
	{
		cout<<"Sorry you lost! You have went past 21!"<<endl;
		exit(1);
	}

	if (computertotal >21)
	{
		cout<<"Congrats you have won! The computer has went past 21!"<<endl;
		exit(1);
	}
}

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;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1