7 Replies - 1625 Views - Last Post: 11 November 2010 - 11:07 AM Rate Topic: -----

#1 havoc_27   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 16-September 10

help with dice program

Posted 09 November 2010 - 11:38 PM

I have made a program to play a game of dice but i can not figure out how to show just the final score. It will ask the user how many times they want to roll the dice and then asks if they want to see an individual score or see a final score. i can have the individual score show but i don't know how to get he final score to show. can someone help in where i went wrong.

#include<iostream>
#include<conio.h>
#include<ctime>

using namespace std;

int Rolldie();
//char Validateinput(char answer, char opt1, char opt2);

int main()
{
	int score=0;
	int dice_number[13]={0};
	int number=0;
	int i=0;
	int d=0;
	char answer;
	char bar;

	
	cout<<"How many times do you want to roll the dice? ";
	cin>>number;
	cout<<endl;
	cout<< "Please enter I for Individual and F for final to see the scores."<<endl;
	cout<<"Do you want to see the Final score or the Individual scores? ";
	cin>>answer;

	srand(clock());
	
	for(i=0;i<=number;i++)
	{
		int die1=Rolldie();
		int die2=Rolldie();

		score = die1 + die2;
	
		//first loop
		if(answer=='i' || answer=='I')
		{
			cout<<die1<<"+"<<die2<<"=";
			cout<<score<<endl;
		}
		if(answer=='f' || answer=='F')
		{
			cout<<"The final score is: "<<   <<endl;


		}

	
}
	


	getch();
	return 0;
}

int Rolldie()
{

	return rand()%6+1;

}


This post has been edited by no2pencil: 09 November 2010 - 11:39 PM
Reason for edit:: Corrected code tags


Is This A Good Question/Topic? 0
  • +

Replies To: help with dice program

#2 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: help with dice program

Posted 09 November 2010 - 11:59 PM

You need to print the final score outside the loop, so that it prints only once after all the dice rolling happens. Now how to calculate that, for this get on more variable say final_score and initialize to 0 at start of the program. Inside your for loop whenever you calculate score for the dice roll add that score in final_score and store it in final_score. This way the addition of all dice rolls will be stored in final_score once you come out of for loop. Then you can just print this variable and show the final score.

I hope this will help you. :)
Was This Post Helpful? 0
  • +
  • -

#3 havoc_27   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 16-September 10

Re: help with dice program

Posted 10 November 2010 - 12:11 AM

ok so i would be doing

final_score=0; at the beginning of code

would i be placing this inside the for loop

final_score=score+final_score;

do i have that coding right?
Was This Post Helpful? 0
  • +
  • -

#4 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: help with dice program

Posted 10 November 2010 - 11:32 PM

Yes, it's looks OK to me. Try it and see if it's the thing you expected to come as output. Post your modified code.
Was This Post Helpful? 0
  • +
  • -

#5 havoc_27   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 16-September 10

Re: help with dice program

Posted 11 November 2010 - 01:16 AM

Sorry for the confusion earlier. I did a double post with the modified code i did. sorry everyone,

I do have another modified code, the problem i have now is the bar graph. i am not sure my coding is right can you guys please look at it

#include<iostream>
#include<conio.h>
#include<ctime>


using namespace std;

int Rolldie();

int main()
{
	char ans;
	do
	{
	int score=0;	// total of the die1 and die2 thrown and added together
	int dice_number[13]={0};	// array storage spot
	int number=0;				// inputted by the user
	int i=0;					// loop counter
	int d=0;					// counter to show the bar graph results
	char answer;				/* will show the individual result or the final
								score as requested by the user
								*/
	char bar;

	int final_score=0;			/*this will show just the score for all die1 and die2 added
								together without seeing individual results
								*/


	
	cout<<"How many times do you want to roll the dice? ";
	cin>>number;
	cout<<endl;
	/*This loop will check to see if the character entered is not a i or f.*/
	 do
		{
			cout<<"Do you want to see the Final score or the Individual scores[I/F]? ";
			cin >> answer;       
			answer = tolower(answer); 
		} while ( ! (answer=='i' || answer=='f') );


	/*will generate a random number*/
	srand(clock());
	
	for(i=0;i<number;i++)
	{
		int die1=Rolldie();
		int die2=Rolldie();
		

		score = die1 + die2;

		/*This keeps track of the scores between 2-12*/
		dice_number[score]++;

		
		
		/*This is if the answer is for individual scores to print*/
		if(answer=='i' || answer=='I')
		{
			cout<<die1<<"+"<<die2<<"= ";
			cout<<score<<endl;

		}

		/*This is for showing just the final score as entered by the user*/
		if(answer=='f' || answer=='F')
			{
			
				final_score=final_score+score;
				cout<<final_score<<endl;
			}
		
	}
	/*This will keep track of how many times the sum of 2 -12 
	come up and display it*/
	for(i=2;i<=12;i++)
	{
		cout<<"The score of "<<i<<" comes up "<<dice_number[i]<< " times"<<endl;
	}	
	
	/*this is the bar graph that will show how many times the 
	sums of die1 and die2 add up to 2-12
	*/
	cout<<"The Bar Graph"<<endl;
	for(d=2;d<=12;d++)
	cout<<d<<":"<<endl;
	

	for(i=2;i<=dice_number[d];i++)
		cout<<"*";

		

	cout<<"Try again? Yes or no. ";
	cin>>ans;
	}while(ans=='y' || ans=='Y');

	getch();
	return 0;
}

int Rolldie()
{

	return rand()%6+1;

}



Was This Post Helpful? 0
  • +
  • -

#6 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: help with dice program

Posted 11 November 2010 - 02:24 AM

And what are you expecting from it? are you facing any issues in it?
Was This Post Helpful? 0
  • +
  • -

#7 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: help with dice program

Posted 11 November 2010 - 03:17 AM

From whatever I got by looking at your code I have marked it. Check my comments inside the code.

#include<iostream>
#include<conio.h>
#include<ctime>


using namespace std;

int Rolldie();

int main()
{
	char ans;
	do
	{
	int score=0;	// total of the die1 and die2 thrown and added together
	int dice_number[13]={0};	// array storage spot
	int number=0;				// inputted by the user
	int i=0;					// loop counter
	int d=0;					// counter to show the bar graph results
	char answer;				/* will show the individual result or the final
								score as requested by the user
								*/
	char bar;

	int final_score=0;			/*this will show just the score for all die1 and die2 added
								together without seeing individual results
								*/


	
	cout<<"How many times do you want to roll the dice? ";
	cin>>number;
	cout<<endl;
	/*This loop will check to see if the character entered is not a i or f.*/
	 do
		{
			cout<<"Do you want to see the Final score or the Individual scores[I/F]? ";
			cin >> answer;       
			answer = tolower(answer); 
		} while ( ! (answer=='i' || answer=='f') );


	/*will generate a random number*/
	srand(clock());
	
	for(i=0;i<number;i++)
	{
		int die1=Rolldie();
		int die2=Rolldie();
		

		score = die1 + die2;

		/*This keeps track of the scores between 2-12*/
		dice_number[score]++;

		
		
		/*This is if the answer is for individual scores to print*/
		if(answer=='i' || answer=='I')
		{
			cout<<die1<<"+"<<die2<<"= ";
			cout<<score<<endl;

		}

		/*This is for showing just the final score as entered by the user*/
		if(answer=='f' || answer=='F')
			{
			
				final_score=final_score+score;
				cout<<final_score<<endl;
			}
		
	}
	/*This will keep track of how many times the sum of 2 -12 
	come up and display it*/
	for(i=2;i<=12;i++)
	{
		cout<<"The score of "<<i<<" comes up "<<dice_number[i]<< " times"<<endl;
	}	
	
	/*this is the bar graph that will show how many times the 
	sums of die1 and die2 add up to 2-12
	*/
	cout<<"The Bar Graph"<<endl;

/*AmitTheInfinity : this needs to be the nested for loop.  i.e. the for loop here should have a {} block and next 3 lines (printing d, a for loop statement and a cout statement inside that loop) should be inside this block.*/
	for(d=2;d<=12;d++)
	cout<<d<<":"<<endl;
	
/*AmitTheInfinity : The following loop goes from 2 to <=n which should actually start from 1*/
	for(i=2;i<=dice_number[d];i++) //AmitTheInfinity : you are using d as index so right now this will go into infinite loop.  you have to have this for loop inside the above for loop.
		cout<<"*";

		

	cout<<"Try again? Yes or no. ";
	cin>>ans;
	}while(ans=='y' || ans=='Y');

	getch();
	return 0;
}

int Rolldie()
{

	return rand()%6+1;

}







I hope this will help you.
Was This Post Helpful? 0
  • +
  • -

#8 havoc_27   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 16-September 10

Re: help with dice program

Posted 11 November 2010 - 11:07 AM

ok so my code would then be

for(d=2;d<=12;d++)
	{
	cout<<d<<":";
	for(i=2;i<=dice_number[d];i++)

		cout<<"*";
		
	}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1