Who's the winner?

Pages: 12
Could you help me to get the winner after the seven dice rolls? the indevdual seven scores should be addes then the winner told at the end and Im not sure what to write for it.
Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

int getRand();

 void Rolldie( int num0,int num1, int num2, int num3, int num4, int num5,int num6,int num7);
 void results (int num0,int num1, int num2, int num3, int num4, int num5,int num6, int num7);
 void score( int num0,int num1, int num2, int num3, int num4, int num5,int num6, int num7); 
  int main()
{
 int num0=0,num1=0, num2=0, num3=0, num4=0, num5=0, num6=0,num7=0;
 srand(time(NULL));
 string player1;
 string player2;

 cout << "Welcome to the Dice Roller!  The simple rule of this game is to roll"<< endl;
 cout << "the die and finish on a total score of 10,000 or more points."<< endl << endl;


 cout << "Player 1, please enter your name: ";
 cin >> player1;
 cout<< endl;
 cout << "Welcome, " << player1 << "."<< endl << endl;
	
 cout << "Player 2, please enter your name: "; 
 cin >> player2;
 cout<< endl;
 cout << "Welcome, " << player2 << "."<< endl << endl;
	
 cout << "Let's begin!" << endl;
 system ("Pause");
 cout<<endl;
 
 cout << player1 << " rolled:";
 Rolldie(num0,num1,num2,num3,num4,num5,num6,num7);
 cout<<endl;
 cout << player2 << " rolled:";
 Rolldie(num0,num1,num2,num3,num4,num5,num6,num7);
 cout << player1 << " rolled:";
 Rolldie(num0,num1,num2,num3,num4,num5,num6,num7);
 cout<<endl;
 cout << player2 << " rolled:";
 Rolldie(num0,num1,num2,num3,num4,num5,num6,num7);
 cout << player1 << " rolled:";
 Rolldie(num0,num1,num2,num3,num4,num5,num6,num7);
 cout<<endl;
 cout << player2 << " rolled:";
 Rolldie(num0,num1,num2,num3,num4,num5,num6,num7);
 cout << player1 << " rolled:";
 Rolldie(num0,num1,num2,num3,num4,num5,num6,num7);
 cout<<endl;
 cout << player2 << " rolled:";
 Rolldie(num0,num1,num2,num3,num4,num5,num6,num7);
 cout << player1 << " rolled:";
 Rolldie(num0,num1,num2,num3,num4,num5,num6,num7);
 cout<<endl;
 cout << player2 << " rolled:";
 Rolldie(num0,num1,num2,num3,num4,num5,num6,num7);
 cout << player1 << " rolled:";
 Rolldie(num0,num1,num2,num3,num4,num5,num6,num7);
 cout<<endl;
 cout << player2 << " rolled:";
 Rolldie(num0,num1,num2,num3,num4,num5,num6,num7);
 cout << player1 << " rolled:";
 Rolldie(num0,num1,num2,num3,num4,num5,num6,num7);
 cout<<endl;
 cout << player2 << " rolled:";
 Rolldie(num0,num1,num2,num3,num4,num5,num6,num7);

 system("PAUSE");
 
}
 void Rolldie (int num0,int num1, int num2, int num3, int num4, int num5,int num6,int num7)
{
 int num;
 int counter = 0;
	
 while (counter <= 5)// loop which allows code to be executed repeatedly	
{       
 num = rand() % 6+1;//gives the random generated number (num)between 1 to 6
 
 cout << " " << num << " " ;
 
 if (num==0)// if else statements that determines the numbers generated
     num0++;
    else if(num == 1)
            num1++;
	else if(num == 2)
		    num2++;
	else if(num == 3)
		    num3++;
	else if(num == 4)
		    num4++;
	else if(num == 5)
		    num5++;
	else if(num == 6)
		    num6++;
	else
            num7++;
	counter++;
	
}
 int results (int num0,int num1, int num2, int num3, int num4, int num5,int num6, int num7);
 //results will return the amount of 1's to 6's that are randomly generated
{
 
 cout << num << endl;
 
}
 int score (int num0,int num1, int num2, int num3, int num4, int num5,int num6, int num7);
{
 
 int total0=num0*0;
 int total1=num1*1*100;
 int total2=num2*2*100;
 int total3=num3*3*100;
 int total4=num4*4*100;
 int total5=num5*5*100;
 int total6=num6*6*100;
 int T_total= total0 + total1 + total2 + total3 + total4 + total5 + total6;
 cout<<endl;
 cout<<"                   Roll points is: "<<"  "<< T_total <<endl;//returns the overall score
 
 
}
}







Last edited on
So you started another topic on the same program, huh?
I don't have time right now to help with the winner, but as in the last post I can tell you - don't use system("pause")... You can use the code I gave you there http://www.cplusplus.com/forum/beginner/95971/

I also personally think it is better to use "\n" instead of endl but it doesn't make much of a difference in this program.

And there's no need to roll the dice that many times in the code, in the code I gave you before the program just asks you, if you want to play again, so you can roll the dice as many times as you want... If you don't like the fact that you have to re enter the player names every time, it's easy to change :)

The line int getRand(); doesn't do anything as far as I know...

Another thing, why do you even have int total0=num0*0; if this returns 0 in the end anyway? And what about num7 if it isn't used anywhere?

There's no need for the functions
1
2
void results (int num0,int num1, int num2, int num3, int num4, int num5,int num6, int num7);
void score( int num0,int num1, int num2, int num3, int num4, int num5,int num6, int num7);

Because they're in a function already and don't do anything by themselves.
Then there's the other strange thing, because you declare them as void at the top and then as int in Rolldie().

You're using strings, so better add #include <string> at the top (not all compilers need it, but some do)

In my version of the code, I also clear the screen which could be very useful if you want to add the ability to play again.

In big programs it is not a good idea to add using namespace std;
just prefix your functions with std:: (which I already did in the code I wrote)

Are those enough reasons to switch to the code I gave you?
Last edited on
Thanks for the help you gave but i was given specific instructions to show the seven indevidual rolls and in your code it only did one run then restarted.
Then my instructions was that at the end, show the winner after the scores were added up .

I'd imagine you'd add up the 7 totals for each individual player. Then have it compare the two: If playerA's total is greater than playerB's, say that playerA won. Otherwise, say playerB won.
No problem with the individual rolls, I'll just fix that right up :)
As for the winner, I'll let you try to figure it out yourself as it's not really difficult, but of course ask again, if you really don't get it.

So here's the code with seven rolls:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <cstdlib>
#include <string>
#include <windows.h>

void rolldice (int num1, int num2, int num3, int num4, int num5, int num6);
void ClearScreen();

int num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0;
int player1score, player2score;

int main()
{
    srand(time(0));
    std::string player1;
    std::string player2;

    ClearScreen();
    std::cout << "Welcome to the Dice Roller!  The simple rule of this game is to roll \n";
    std::cout << "the die and score 10,000 or more points. \n\n";

    std::cout << "Player 1, please enter your name: ";
    std::cin >> player1;
    std::cout << "\nWelcome, " << player1 << ". \n\n";

    std::cout << "Player 2, please enter your name: ";
    std::cin >> player2;

        while (player2 == player1)
        {
            std::cout << "Player 1 has already chosen this name!\n\n";
            std::cout << "Player 2, please enter your name: ";
            std::cin >> player2;
        }

    std::cout << "\nWelcome, " << player2 << ". \n\n";

    std::cout << "Let's begin!\n\n";
    std::cout << "Press any key to continue...\n";
    std::cout << "(Now where's the 'any' key?)\n\n\n";
    std::cin.get();
    std::cin.ignore();

    for (int roll7 = 0; roll7 < 7; roll7 ++)
    {
    std::cout << player1 << " rolled: ";  //1
    rolldice (num1, num2, num3, num4, num5, num6);
    }

    std::cout << "He/She scored " << player1score << " points!\n\n";

    for (int roll7 = 0; roll7 < 7; roll7 ++)
    {
    std::cout << player2 << " rolled: ";  //1
    rolldice (num1, num2, num3, num4, num5, num6);
    }

    std::cout << "He/She scored " << player2score << " points!\n\n";

    return 0;
}

void rolldice (int num1, int num2, int num3, int num4, int num5, int num6)
{
    num1 = rand() % 6 + 1; //randomly generates numbers from 1 to 6 for all 6 dice
    num2 = rand() % 6 + 1;
    num3 = rand() % 6 + 1;
    num4 = rand() % 6 + 1;
    num5 = rand() % 6 + 1;
    num6 = rand() % 6 + 1;

    //results will return the amount of 1's to 6's that are randomly generated
    std::cout << " " << num1 << " " << num2 << " " << num3 << " " << num4
              << " " << num5 << " " << num6 << "\n";

    int total1 = num1 * 1 * 100;
    int total2 = num2 * 2 * 100;
    int total3 = num3 * 3 * 100;
    int total4 = num4 * 4 * 100;
    int total5 = num5 * 5 * 100;
    int total6 = num6 * 6 * 100;

    player1score = total1 + total2 + total3 + total4 + total5 + total6;
    player2score = total1 + total2 + total3 + total4 + total5 + total6;
}

void ClearScreen()
{
  HANDLE                     hStdOut;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD                      count;
  DWORD                      cellCount;
  COORD                      homeCoords = { 0, 0 };

  hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  if (hStdOut == INVALID_HANDLE_VALUE) return;

  /* Get the number of cells in the current buffer */
  if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  cellCount = csbi.dwSize.X *csbi.dwSize.Y;

  /* Fill the entire buffer with spaces */
  if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Fill the entire buffer with the current colors and attributes */
  if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Move the cursor home */
  SetConsoleCursorPosition( hStdOut, homeCoords );
}


I haven't tested this code yet, if there's a problem, I can fix it...
Last edited on
As your num0-num7 are in main. When you pass them to the function and change them, they wont change in main().

To do that, you need to pass by reference.

First, the way you're doing it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

void func( int integer );

int main()
{
	int myInt = 5;

	std::cout << "myInt in main: " << myInt << '\n';

	func( myInt );

	std::cout << "myInt in main, after function: " << myInt << '\n';

	return 0;
}

void func( int integer )
{
	std::cout << "integer in the function: " << integer << '\n';

	integer += 7;

	std::cout << "integer in the function, after addition: " << integer << '\n';
}
myInt in main: 5
integer in the function: 5
integer in the function, after addition: 12
myInt in main, after function: 5


Now, passing by reference:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

void func( int &integer );

int main()
{
	int myInt = 5;

	std::cout << "myInt in main: " << myInt << '\n';

	func( myInt );

	std::cout << "myInt in main, after function: " << myInt << '\n';

	return 0;
}

void func( int &integer )
{
	std::cout << "integer in the function: " << integer << '\n';

	integer += 7;

	std::cout << "integer in the function, after addition: " << integer << '\n';
}
myInt in main: 5
integer in the function: 5
integer in the function, after addition: 12
myInt in main, after function: 12


Note the ampersand '&' on lines 3 and 18. And that the value has been changed in the result when back in main().
Last edited on
Thank you! !! Vidminas (36) for your patience in helping me with this program and Ispil (16) for the 'winner' comment. I am working on not making so many mistakes on simple programming but im still learning. And i think i figured out how to tell who wins! though when it runs it dosnt give the correct winner sometimes its

1
2
3
4
 if (player1score>player2score)
  std::cout<<player1<<"wins \n";
 else 
 std::cout<<player2<<"wins\n";


And does the void clearscreen() have to be there? because i took that out and compiled the program and it didnt crash.

Thanks :)
Last edited on
The void clearscreen(); at the top has to be there if the function is defined after the main, but in this case I don't think you need that... You'd certainly have to do it if you had multiple files.

Then your winner won't be displayed correctly, because you don't have an option where both players win, if their scores are the same. In your current code it would say that player 2 won. Once you fix that it should be all right :)
Ok what about
1
2
3
4
5
6
7
 if (player1score > player2score)
   
   std::cout << player1 <<" Wins!! \n";

   else if (player1score < player2score)

   std::cout << player2 <<" Wins!!\n";

I tried this but its not working, what am i doing wrong?


Also if this program was to have a structure chart would it look like this?

main| leads into|

void rolldice--int(num1, num2, num3, num4, num5, num6)

|which leads into|

rolldice(randomly generate 1-6 of dice)

| that has the calculation of player score|

player1&player2 score

|the scores are called back to |

main to show results
I wrote up my own version of this and managed to get it down to 70 total lines. I used an integer array instead of that long parameter list, and it made things much simpler and cleaner. Maybe using an array would be easier?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cstdlib>
#include <ctime>

const static int NUM_DICE = 7;
const static int NUM_SIDES_PER_DIE = 6;

int RollDice(int dice[NUM_DICE])
{
    int total = 0;
    for(int i = 0; i < NUM_DICE; i++)
    {
        dice[i] = ((rand() % NUM_SIDES_PER_DIE) + 1);
        total += dice[i];
    }
    return total;
}

void PrintDice(int dice[NUM_DICE])
{
    for(int i = 0; i < NUM_DICE; i++)  std::cout << "Dice #" << i+1 << ": " << dice[i] << "\n";
}

int main()
{
    srand(time(NULL));
    int diceRoll[NUM_DICE];
    RollDice(diceRoll);
    PrintDice(diceRoll);

    return 0;
}


EDIT: Added some constants to make it easier to change some aspects of the program.
Last edited on
Overall this program is around 80 lines because the clear screen thing isnt realy necessary. What are your views on the structure chart for the program how should it look?
I don't really know. I don't know if I'm familiar with those. I'm more familiar with class and sequence diagrams. Sorry!
I wonder why you need parameters for the RollDice function at all? You're clearly not assigning any values to num1 through num7 in your main function, nor using them anywhere else outside of the RollDice function.
Last edited on
Yes, but I am accessing those values in a different function. As an alternative to global data, I have main create the array, then pass it to the roll and print functions. This way, I don't have to use global data. Also, if I wanted, I could create more than one DiceArray and still use both of my functions on the new arrays.
Sorry, I was referring to the code posted by fluffy and Vidminas.
Last edited on
Oh, apologies. BTW, I edited the code sample I provided to move those integer literals into one place at the top of the code. It should make it easier to change later if necessary.
Does anyone know how to determine the winner of the game because i keep getting the wrong results:

1
2
3
4
5
if (player1score > player2score)
     std::cout << player1 <<" Wins!! \n";

     else 
     std::cout << player2 <<" Wins!!\n";
I told you above
Then your winner won't be displayed correctly, because you don't have an option where both players win, if their scores are the same. In your current code it would say that player 2 won.


Add
1
2
else if (player1score == player2score
std::cout << player1 << " and " << player2 << " win! They both got the same scores! \n";
But it does not work. I followed what you said and I keep giving incorrect results . There must be something wrong.

1
2
3
4
5
6
 if (player1score > player2score)
     std::cout << player1 <<" Wins!! \n";
     else if (player1score < player2score)
     std::cout << player2 <<" Wins!! \n";
     else if (player1score == player2score)
     std::cout << player1 << " and " << player2 << " win! They both got the    same scores! \n"; 
You should post your new code. There's probably something else going wrong somewhere. Probably with how you are calculating or storing the player scores.
Pages: 12