program problem

consider this prgram, this is game called 'beat that'.in this game,a player will roll four dice.the program must generate a random number between 1and6 for different dice:
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

void print(int dice);

//this is the seed function

void rand_seed(){
int seed = (int)(time(0));
srand(seed);
}

//this function generates a random number between 0 - 6 (1,2,3,4,5)
int dice_roll(){
return 1 + rand() % (6 - 1 + 1);
}

void swap(int &x, int &y) {
int temp;
if(x < y){
temp = x;
x = y;
y = temp;
}
}

//arranging random numbers from bigger to smaller

int arrange(int d1,int d2,int d3,int d4){

bool stop = false;
while(!stop){
if(d1>=d2 && d2>=d3 && d3>=d4){
stop = true;
}
swap(d1,d2);
swap(d2,d3);
swap(d3,d4);
}
cout << endl << endl;

// output

cout << "4 dices are rearranged....." << endl;
print(d1), print(d2), print(d3), print(d4);
return d1;
}

// switch function for printing in star style

void print(int dice){
switch (dice){
case 1:{
cout << "----" << endl;
cout << " |" << endl;
cout << " * |" << endl;
cout << " |" << endl;
cout << "----" << endl;
break;
}
case 2: {
cout << "----" << endl;
cout << "* |" << endl;
cout << " |" << endl;
cout << "* |" << endl;
cout << "----" << endl;
break;
}
case 3: {
cout << "----" << endl;
cout << "* |" << endl;
cout << "* |" << endl;
cout << "* |" << endl;
cout << "----" << endl;
break;
}
case 4: {
cout << "----" << endl;
cout << "* *|" << endl;
cout << " |" << endl;
cout << "* *|" << endl;
cout << "----" << endl;
break;
}
case 5: {
cout << "----" << endl;
cout << "* *|" << endl;
cout << " * |" << endl;
cout << "* *|" << endl;
cout << "----" << endl;
break;
}
case 6: {
cout << "----" << endl;
cout << "* *|" << endl;
cout << "* *|" << endl;
cout << "* *|" << endl;
cout << "----" << endl;
break;
}
default: cout << "" <<endl;
}
}

int main()
{

//declaration



rand_seed();

int d1 = dice_roll();
int d2 = dice_roll();
int d3 = dice_roll();
int d4 = dice_roll();

//output

cout << "4 dices are trown....." << endl;
print(d1),print(d2),print(d3),print(d4);

int largest = arrange(d1,d2,d3,d4);


cout<<"to play again press: 'p'& F9"<<endl;
cout<<endl<<endl;
cout<<"to stop, just press: 's'"<<endl;



return 0;
}

The problem is that i need to ask a user if he/she wishes to continue with playing this game or to stop ,so I dont know how to do so. can anyone please do it for me.
Use a while loop that asks the user whether they want to keep going; if they don't, exit the loop.
but how? can you show me an example please.
Mistake here:
1
2
3
4
//this function generates a random number between 0 - 6 (1,2,3,4,5)
int dice_roll(){
return 1 + rand() % (6 - 1 + 1);//<< This will give (1,2,3,4,5,6) not (1,2,3,4,5)
}
For the while loop as Zhuge said, you can do something along the lines of:

1
2
3
4
5
6
7
char playAgain = 'Y';

while (playAgain == 'Y')
{
    //roll the dice etc
    //ask if player wants to play again and accept inputs of Y or N
}
Topic archived. No new replies allowed.