Hi, i am making a dice game where you roll 2 dices, and add up the totals. If its even, player 1 gets a point, odd, player two. However, i am looking for some advise. I need it to keep track of how many wins each player has and display the winner at the end. How would i go about doing that? I tired somethings in the game function but it does not seem to be working. thanks
#include <iostream>
#include <cstdlib>
#include <time.h>
usingnamespace std;
int Dice (int dice1, int dice2, int sum)
{
int even;
int odd;
int eventotal=0;
dice1 = rand() % (6)+1;
dice2 = rand() % (6)+1;
cout << "First Dice = " << dice1 << endl;
cout << "Second Dice = " << dice2 << endl;
sum=dice1+dice2;
cout << "The Sum is: " << sum << endl<< endl;
return (dice1, dice2, sum);
}
int rules()
{
int main();
system("cls");
cout << " \nThe Rules Are Quite Simple. We Roll Two Dice.\n -If the Sum is Even, Player 1 Gets a Point\n -If the Sum is Odd, Player 2 Gets a Point\n";
cout <<endl;
system("pause");
system("cls");
main ();
}
int game()
{
int times;
int dice1;
int dice2;
int sum;
int even;
int odd;
int eventotal=0;
system("cls");
cout << "\n How Many Times Would You Like to Roll?\n";
cout << " Rolls: ";
cin >> times;
system("cls");
while (times>0)
{
cout << "Rolls Left :" << times << endl;
if (sum % 2 == 0)
{even++;
if (even==1)
{eve n=even+1;
eventotal=even;}
else
{even=even;}}
else
{odd++;}
cout << "Player 1 Points: " << eventotal <<endl;
cout << "Player 2 Points: " << odd << endl << endl;
Dice (dice1, dice2, sum);
times--;}
}
int main()
{
system ("color 97");
time_t seconds;
time(&seconds);
srand((unsignedint) seconds);
int key;
cout << "** Are You Feeling Lucky??? **\n\n";
cout << "Menu\n Press 1 for Rules\n Press Any Key to Play\n" ;
cout << "\nWhat is Your Choice: ";
cin >> key;
if (key==1)
{rules();}
else
{game();}
system("pause");
}
You shouldn't call main. Is that even allowed? Also you need to return an integer for the rules function.
To keep track of wins just add two global variables. Increment them when the player or players win. Then output the number of wins.
You have a space in line 60 that shouldn't be there. You could just use an increment there.
Line 61 There is no point in that. You don't assign a variable to itself without modify the variable.
From the dice function. You can't return 3 variables.
#include <iostream>
#include <cstdlib>
#include <time.h>
usingnamespace std;
int eventotal=0;
int Dice (int dice1, int dice2, int sum)
{
int even;
int odd;
int eventotal=0;
dice1 = rand() % (6)+1;
dice2 = rand() % (6)+1;
cout << "First Dice = " << dice1 << endl;
cout << "Second Dice = " << dice2 << endl;
sum=dice1+dice2;
cout << "The Sum is: " << sum << endl<< endl;
return (dice1, dice2, sum);
}
int rules()
{
int main();
system("cls");
cout << " The Rules Are Quite Simple. We Roll Two Dice.\n -If the Sum is Even, Player 1 Gets a Point\n -If the Sum is Odd, Player 2 Gets a Point\n";
cout <<endl;
system("pause");
system("cls");
main ();
}
int game()
{
int times;
int dice1;
int dice2;
int sum;
int even;
int odd;
system("cls");
cout << "How Many Times Would You Like to Roll?\n";
cin >> times;
while (times>0)
{
cout << "Rolls Left :" << times << endl;
Dice (dice1, dice2, sum);
times--;}
if (sum % 2 == 0)
{even++;
eventotal=eventotal+1;}
else
{odd++;}
cout << "Player 1 Points: " << eventotal <<endl;
cout << "Player 2 Points: " << odd << endl << endl;
}
int main()
{
time_t seconds;
time(&seconds);
srand((unsignedint) seconds);
int key;
cout << "Menu\n Press 1 for Rules\n Press Any Key to Play\n" ;
cout << "\nWhat is Your Choice: ";
cin >> key;
if (key==1)
{rules();}
else
{game();}
system("pause");
}
// dice_game.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int even;
int odd;
int eventotal = 0;
void Dice (int& dice1, int& dice2, int& sum)
{
dice1 = rand() % (6)+1;
dice2 = rand() % (6)+1;
cout << "First Dice = " << dice1 << endl;
cout << "Second Dice = " << dice2 << endl;
sum = dice1 + dice2;
cout << "The Sum is: " << sum << endl<< endl;
//return (dice1, dice2, sum);
}
void rules()
{
system("cls");
cout << " \nThe Rules Are Quite Simple. We Roll Two Dice.\n -If the Sum is Even, Player 1 Gets a Point\n -If the Sum is Odd, Player 2 Gets a Point\n";
cout << endl;
system("pause");
system("cls");
}
void game()
{
int times;
int dice1;
int dice2;
int sum;
int even;
int odd;
int eventotal=0;
system("cls");
cout << "\n How Many Times Would You Like to Roll?\n";
cout << " Rolls: ";
cin >> times;
system("cls");
while (times>0)
{
cout << "Rolls Left :" << times << endl;
if (sum % 2 == 0)
{
even++;
if (even==1)
{
even++;
eventotal=even;
}
}
else
{
odd++;
}
cout << "Player 1 Points: " << eventotal <<endl;
cout << "Player 2 Points: " << odd << endl << endl;
Dice (dice1, dice2, sum);
times--;
}
}
int main()
{
system ("color 97");
srand(unsigned(time(NULL)));
int key;
cout << "** Are You Feeling Lucky??? **\n\n";
cout << "Menu\n Press 1 for Rules\n Press Any Key to Play\n" ;
cout << "\nWhat is Your Choice: ";
cin >> key;
if (key==1)
{
rules();
}
else
{
game();
}
system("pause");
return 0;
}
Great, i fixed it and now it works. Thanks, however why do i not need to have return for line 24? and Why did u change the random lines on 86? Thanks, i really appericate your help