Who's the winner?

Pages: 12
Ok here it is.
NB. You said before to ignore using system pause but i had to use it for you to see the results else it closes after compiling

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
#include <iostream>
#include <cstdlib>
#include <string>

void rolldice (int num1, int num2, int num3, int num4, int num5, int num6);//function prototype for the dice rolls

int num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0;/*integer decleration of the six numbers equaled to zero
                                                                 such that the count starts from 1. */
                                                                 
int player1score, player2score;//integer decleration of players one and twos score

int main()
   {
    srand(time(0));//This is declared to help in the generation of random numbers by seeding rand with a starting value.
    
    std::string player1;//string decleration for the text that is to be inputed for the first player
    
    std::string player2;//string decleration for the text that is to be inputed for the second player
    
    int roll = 1;//allows player2 to roll
    
    //Welcome message that appears at start of program with goal of the game
    std::cout <<"***************************************************************************\n";
    std::cout << " Welcome to the Dice Roller!  The simple rule of this game is to roll \n";
    std::cout <<"\n";
    std::cout << " the die seven times and score up to 10,000 or more points. \n\n";
    std::cout <<"***************************************************************************\n";
    std::cout <<"\n";
    std::cout << "Player 1, please enter your name: ";//output message for first palyer
    std::cin >> player1;//player1 types desired name
    std::cout << "\nWelcome, " << player1 << ". \n\n";

    std::cout << "Player 2, please enter your name: ";//output message for first palyer
    std::cin >> player2;//player2 types desired name

        while (player2 == player1)//Loop which redirects player2 input a different name if player1 has similar name
        {
            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 << player1<<" goes first, press enter to continue...\n";
    
    std::cin.get();
    std::cin.ignore();

    for (int roll7 = 0; roll7 < 7; roll7 ++)//roll7 Loop which rolls seven times for player1
    {
    std::cout << player1 << " rolled: ";  //Random numbers 1-6 are generated and outputted from rolldice function
    rolldice (num1, num2, num3, num4, num5, num6);
    }
    std::cout <<"\n";
    
    std::cout << player1 << " scored " << player1score << " points!\n\n";//outputted player1 score after rolls
    
    std::cout << "Your turn, " << player2 << ".  Enter 1 to roll :";//start of second players roll
    std::cin >> roll;//inputs 1 to roll
    
     for (int roll7 = 0; roll7 < 7; roll7 ++)//roll7 Loop which rolls seven times for player1
    {
     std::cout << player2 << " rolled : ";  //Random numbers 1-6 are generated and outputted from rolldice function
     rolldice (num1, num2, num3, num4, num5, num6);
    }
     std::cout <<"\n";
    
     std::cout << player2 <<" scored " << player2score << " points!\n\n";//outputted player1 score
    
    //Loop which determines who wins if player1score is greater than player2score player1 wins if not then player2 wins
     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"; 

     system ("pause");
   }
    void rolldice (int num1, int num2, int num3, int num4, int num5, int num6)//voided rolldice function for generation of results
   {
    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;

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

    int total1 = num1 * 1 * 100; //for all 1's rolles its multiplied by 100
    int total2 = num2 * 2 * 100; //for all 2's rolles its multiplied by 100
    int total3 = num3 * 3 * 100; //for all 3's rolles its multiplied by 100
    int total4 = num4 * 4 * 100; //for all 4's rolles its multiplied by 100
    int total5 = num5 * 5 * 50;  //ecah 5 scores 50 points.
    int total6 = num6 * 6 * 100; //for all 6's rolles its multiplied by 100

    //After totals of each side 1-6 is calculated it is scored as player1score
    player1score = total1 + total2 + total3 + total4 + total5 + total6;
    //After totals of each side 1-6 is calculated it is scored as player1score
    player2score = total1 + total2 + total3 + total4 + total5 + total6;
    
    }//end of program 




The problem is in the rolldice function.

1
2
3
4
    //After totals of each side 1-6 is calculated it is scored as player1score
    player1score = total1 + total2 + total3 + total4 + total5 + total6;
    //After totals of each side 1-6 is calculated it is scored as player1score
    player2score = total1 + total2 + total3 + total4 + total5 + total6;


You are setting player1score and player2score equal every time you call the function.
How do i correct that?
Yep, I know... I didn't give you a very good example, I'll re-write it with vectors (I was almost done, but then my computer crashed and most of the changes weren't saved)
Last edited on
You could change the rolldice function to return an int, and then assign the value to player1score and player2score when you call it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int rolldice()
{
    int num1 = rand() % 6 + 1; //randomly generates numbers from 1 to 6 for all 6 dice
    int num2 = rand() % 6 + 1;
    int num3 = rand() % 6 + 1;
    int num4 = rand() % 6 + 1;
    int num5 = rand() % 6 + 1;
    int num6 = rand() % 6 + 1;


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

    int total1 = num1 * 1 * 100; //for all 1's rolles its multiplied by 100
    int total2 = num2 * 2 * 100; //for all 2's rolles its multiplied by 100
    int total3 = num3 * 3 * 100; //for all 3's rolles its multiplied by 100
    int total4 = num4 * 4 * 100; //for all 4's rolles its multiplied by 100
    int total5 = num5 * 5 * 50;  //ecah 5 scores 50 points.
    int total6 = num6 * 6 * 100; //for all 6's rolles its multiplied by 100

    return (total1 + total2 + total3 + total4 + total5 + total6);
}


1
2
player1score = rolldice();
player2score = rolldice();


and i changed what you said but im getting an error that says linker error did i miss something?

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
#include <iostream>
#include <cstdlib>
#include <string>

void rolldice (int num1, int num2, int num3, int num4, int num5, int num6);//function prototype for the dice rolls

int num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0;/*integer decleration of the six numbers equaled to zero
                                                                 such that the count starts from 1. */
                                                                 
int player1score, player2score;//integer decleration of players one and twos score

int main()
   {
    srand(time(0));//This is declared to help in the generation of random numbers by seeding rand with a starting value.
    
    std::string player1;//string decleration for the text that is to be inputed for the first player
    
    std::string player2;//string decleration for the text that is to be inputed for the second player
    
    int roll = 1;//allows player2 to roll
    
    //Welcome message that appears at start of program with goal of the game
    std::cout <<"***************************************************************************\n";
    std::cout << " Welcome to the Dice Roller!  The simple rule of this game is to roll \n";
    std::cout <<"\n";
    std::cout << " the die seven times and score up to 10,000 or more points. \n\n";
    std::cout <<"***************************************************************************\n";
    std::cout <<"\n";
    std::cout << "Player 1, please enter your name: ";//output message for first palyer
    std::cin >> player1;//player1 types desired name
    std::cout << "\nWelcome, " << player1 << ". \n\n";

    std::cout << "Player 2, please enter your name: ";//output message for first palyer
    std::cin >> player2;//player2 types desired name

        while (player2 == player1)//Loop which redirects player2 input a different name if player1 has similar name
        {
            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 << player1<<" goes first, press enter to continue...\n";
    
    std::cin.get();
    std::cin.ignore();

    for (int roll7 = 0; roll7 < 7; roll7 ++)//roll7 Loop which rolls seven times for player1
    {
    std::cout << player1 << " rolled: ";  //Random numbers 1-6 are generated and outputted from rolldice function
    rolldice (num1, num2, num3, num4, num5, num6);
    }
    std::cout <<"\n";
    
    std::cout << player1 << " scored " << player1score << " points!\n\n";//outputted player1 score after rolls
    
    
    
    std::cout << "Your turn, " << player2 << ".  Enter 1 to roll :";//start of second players roll
    std::cin >> roll;//inputs 1 to roll
    
     for (int roll7 = 0; roll7 < 7; roll7 ++)//roll7 Loop which rolls seven times for player1
    {
     std::cout << player2 << " rolled : ";  //Random numbers 1-6 are generated and outputted from rolldice function
     rolldice (num1, num2, num3, num4, num5, num6);
    }
     std::cout <<"\n";
    
     std::cout << player2 <<" scored " << player2score << " points!\n\n";//outputted player1 score
     
     
     
    //Loop which determines who wins if player1score is greater than player2score player1 wins if not then player2 wins
     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"; 

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


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

    int total1 = num1 * 1 * 100; //for all 1's rolles its multiplied by 100
    int total2 = num2 * 2 * 100; //for all 2's rolles its multiplied by 100
    int total3 = num3 * 3 * 100; //for all 3's rolles its multiplied by 100
    int total4 = num4 * 4 * 100; //for all 4's rolles its multiplied by 100
    int total5 = num5 * 5 * 50;  //ecah 5 scores 50 points.
    int total6 = num6 * 6 * 100; //for all 6's rolles its multiplied by 100

    return (total1 + total2 + total3 + total4 + total5 + total6);
player1score = rolldice();
player2score = rolldice();
    }//end of program 






Last edited on
So yeah I re-wrote the program using a vector for dice rolls. The winners work, but the scores are messed up a lot (they return weird values).
I'm not very good with vectors yet, so I'll let someone else fix this :)

Also only the last score is used for winner determination so you also need to somehow add up all the scores.
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>

int score, player1score, player2score;
std::vector<short> dice(6,0);
int rolldice();

int main()
{
    srand ( time(0) );

    //Players are welcomed
    std::string player1;
    std::string player2;

    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();
    //game actually starts

    std::cout << "\n" << player1 << " rolled:" << "\n";
    for (int roll7 = 0; roll7 < 7; roll7 ++)
    {
    rolldice();
    player1score = score;
    std::cout << "He/She scored " << player1score << " points! \n";
    }

    std::cout << "\n" << player2 << " rolled:" << "\n";
    for (int roll7 = 0; roll7 < 7; roll7 ++)
    {
    rolldice();
    player2score = score;
    std::cout << "He/She scored " << player2score << " points! \n";
    }

    //Determines the winner
    std::cout << "\n\n";
    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";

    std::cout << "\n\n" << "Press any key to exit...";
    std::cin.get();

return 0;
}

int rolldice()
{
    for (long index = 0; index < (short)dice.size(); index ++)
    {
    //randomly generates numbers from 1 to 6 for all 6 dice
    dice.at(index) = rand() % 6 + 1;

    //Returns the amount of 1's to 6's that were randomly generated
    std::cout << " " << dice.at(index) << " ";
    }

    score = dice[1] + dice[2] + dice[3] + dice[4] + dice[5] + dice[6];

return score;
}
Ok thanks for all your help :)
Haha, so I came back to this a week later and I realised that there's no need for vectors or anything else complicated. Instead I just looked at Booradley60's advice and I got everything working.
I also got rid of all global variables (because having them is a bad practice) and now everything is working like it should! No bugs, no nothing :)

Enough talk, here's your full solution:
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
129
130
131
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <windows.h>

int rolldice()
{
    int score = 0;
    const static short dice_amount = 6;
    const static short sides_per_die = 6;
    short dice[dice_amount];

    for(int index = 0; index < dice_amount; index ++)
    {
        dice[index] = ( (rand() % sides_per_die) + 1);
        score += dice[index];
        std::cout << " " << dice[index] << " ";
    }
return score;
}

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

int main()
{
    char playagain;
    do
    {
    ClearScreen();
    srand ( time(0) );

    //Players are welcomed
    std::string player1;
    int player1score = 0;
    std::string player2;
    int player2score = 0;

    std::cout << "Welcome to the Dice Roller!  The simple rule of this game is to roll" << "\n";
    std::cout << "the dice and score more points than the other player." << "\n\n";

    std::cout << "Player 1, please enter your name: ";
    std::cin >> player1;
    std::cout << "\n" << "Welcome, " << 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";
            std::cout << "Player 2, please enter your name: ";
            std::cin >> player2;
        }

    std::cout << "\n" << "Welcome, " << 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();

    //The game actually starts
    std::cout << "\n" << player1 << " rolled:" << "\n";
    for (int roll7 = 0; roll7 < 7; roll7 ++)
    {
    player1score = rolldice();
    std::cout << "His/Her score is " << player1score << " points!" << "\n";
    }

    std::cout << "\n\n" << player2 << " rolled:" << "\n";
    for (int roll7 = 0; roll7 < 7; roll7 ++)
    {
    player2score = rolldice();
    std::cout << "His/Her score is " << player2score << " points!" << "\n";
    }

    //Determines the winner
    std::cout << "\n\n";
    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";

    std::cout << "\n\n" << "Would you like to play again?" << "\n";
    std::cin >> playagain;
    } while (playagain == 'y' or playagain == 'Y');

    std::cout << "\n\n" << "Press any key to exit...";
    std::cin.get();

return 0;
}


void ClearScreen() is there in case you want to play again (then you would likely want to clear the screen, so you can't see your last game)
Topic archived. No new replies allowed.
Pages: 12