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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128`` ``````#include #include #include 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<

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
 ``12`` ``````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:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122`` ``````#include #include #include #include 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:
 ``12345678910111213141516171819202122232425`` ``````#include 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:
 ``12345678910111213141516171819202122232425`` ``````#include 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

 ``1234`` `````` if (player1score>player2score) std::cout<

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 :)
 ``1234567`` `````` 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?

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

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?
 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include #include 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:

 ``12345`` ``````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.

 ``12`` ``````else if (player1score == player2score std::cout << player1 << " and " << player2 << " win! They both got the same scores! \n";``````
 ``123456`` `````` 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"; ``````