Thread: C++ Problem with giving score to correct player

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question C++ Problem with giving score to correct player

    I have programmed a game where you guess a number (1-6) and if the number is equal to the random number then give the player score + 10. But if I have selected for example 4 players then if the game will give player 1 a score it gives player 2 a score instead? What can be causing this error?
    Code:
     #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
                int cube;
                int number[4];
                int player[4] = {1, 2, 3, 4};
                //               0, 1, 2, 3
                int victorys[4];
                int score[4];
                signed int howMany;
                string gotScore = "SCORE: ";
    class GameFunctions
    {
        public:
            void HowManyPlayers()
            {
                /* Lets the use enter how many players
                that want to play the game. Using a for-loop
                in another class and the variable hwoMany to
                represent the value that the user entered*/
                do
                {
                    system("CLS");
                    cout << "1 - One Player" << endl;
                    cout << "2 - Two Players" << endl;
                    cout << "3 - Three Players" << endl;
                    cout << "4 - Four Players" << endl;
                    cout << "How many players: ";
                    cin >> howMany;
                } while (howMany <= 0);
            }
            void throwCube()
            {
                /* Made to throw a random number to
                the user */
                srand(time(NULL));
                cube = rand() % 6 + 1;
                cout << "CUBE NUMBER: " << cube << endl;
            }
            void check()
            {
                /* Made to check if the user got any scores */
                    if (number[0] == cube)
                    {
                        score[0] += 10;
                        cout << gotScore << score[0] << endl;
                    }
                    else if (number[1] == cube)
                    {
                        score[1] += 10;
                        cout << gotScore << score[1] << endl;
                    }
                    else if (number[2] == cube)
                    {
                        score[2] += 10;
                        cout << gotScore << score[2] << endl;
                    }
                    else if (number[3] == cube)
                    {
                        score[3] += 10;
                        cout << gotScore << score[3] << endl;
                    }
                    //End of checking for scores
                    /* Checking for victorys */
                    if (score[0] == 10)
                    {
                        victorys[0]++;
                        cout << "VICTORYS: " << victorys[0] << endl;
                        cout << "PLAYER " << player[0] << " WON!" << endl;
                        system("PAUSE");
                        HowManyPlayers();
                    }
                    else if (score[1] == 10)
                    {
                        victorys[1]++;
                        cout << "VICTORYS: " << victorys[1] << endl;
                        cout << "PLAYER " << player[1] << " WON!" << endl;
                        system("PAUSE");
                        HowManyPlayers();
                    }
                    else if (score[2] == 10)
                    {
                        victorys[2]++;
                        cout << "VICTORYS: " << victorys[2] << endl;
                        cout << "PLAYER " << player[2] << " WON!" << endl;
                        system("PAUSE");
                        HowManyPlayers();
                    }
                    else if (score[3] == 10)
                    {
                        victorys[3]++;
                        cout << "VICTORYS: " << victorys[3] << endl;
                        cout << "PLAYER " << player[3] << " WON!" << endl;
                        system("PAUSE");
                        HowManyPlayers();
                    }
                    //End of checking for victorys
            }
            friend class Players;
    };
    class Players : public GameFunctions
    {
        public:
            int inputNumbers()
            {
                /* The for-loop is really important here.
                It makes the user to select how many players */
                for (int i = 0; i < howMany; i++)
                {
                    system("CLS");
                    cout << "VICTORYS: " << victorys[i] << endl;
                    cout << "PLAYER " << player[i] << endl;
                    cout << "SCORE: " << score[i] << endl;
                    cout << "ENTER NUMBER PLAYER " << player[i] << ": ";
                    cin >> number[i];
                    throwCube();
                    check();
                    system("PAUSE");
                }
                return 0;
            }
    };
    int main()
    {
        bool menu = true;
            Players obj;
            obj.HowManyPlayers();
        while (menu == true)
        {
            obj.inputNumbers();
        }
        return 0;
    }
    I do not understand what's wrong with this?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    First things first: victorys is victories!! :P

    Too many global variables, that's not a good approach in general, especially in the future, when your code is going to be read/modified by more than one programmers.

    Check what my output gave me:
    Code:
    sh: CLS: command not found
    1 - One Player
    2 - Two Players
    3 - Three Players
    4 - Four Players
    How many players: 4            <-- It used 2 players, instead of 4!
    sh: CLS: command not found   <-- Do not use system(..)...
    VICTORYS: 0
    PLAYER 1
    SCORE: 0
    ENTER NUMBER PLAYER 1: 5
    CUBE NUMBER: 2
    sh: PAUSE: command not found
    sh: CLS: command not found
    VICTORYS: 0
    PLAYER 2
    SCORE: 0
    ENTER NUMBER PLAYER 2: 5
    CUBE NUMBER: 5
    SCORE: 10
    VICTORYS: 1
    PLAYER 1 WON!        <-- Wrong, player 2 was the winner
    sh: PAUSE: command not found
    sh: CLS: command not found
    Try again
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    One thing I could suggest, would be to modify the check function to check for i-th number every time, thus passing the i as parameter to check.
    For example, check this output
    Code:
    ENTER NUMBER PLAYER 1: 1
    CUBE NUMBER: 3
    sh: PAUSE: command not found
    sh: CLS: command not found
    VICTORYS: 0
    PLAYER 2
    SCORE: 0
    ENTER NUMBER PLAYER 2: 3
    CUBE NUMBER: 1
    SCORE: 10
    VICTORYS: 1
    PLAYER 1 WON!                                     <-- because he had inputted what the cube gave at second's player turn
    sh: PAUSE: command not found
    sh: CLS: command not found
    As you can see, first player inputted 1 but the cube in his turn gave 3. So he did not win.
    Then, second player inputted 3, but the cube gave 1, so neither first nor second player wins.
    But the check function will check the selection of first player, even though we are in the round of the second player, because you have this if-else statements. You need to have one of, which will work with number[i]

    As you see, your problem is an algorithmic one, that is that you can write some code, but your idea has problems.
    Run your code in piece of paper and you will find out that I am right

    //Post if needed again of course
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    I know that it is a bad idea to use system("") commands but I have no other way of how to clear the screen. But I do not really understand what's wrong still. Once I have entered 2 players. The program says if it is player 1s turn or if it is player 2s turn. But then I will give player 2 a score my program still gives it to player 1. How do I fix this? Should I use a array? For the score[4] and victorys[4]. Or how should I fix this? I have really no idea. Please some hints or something . | EDITED | I'm Think I got a bit of what you meaned I am using the check() function to check both players numbers. If the number that player 1 input was 3 and the cube number 1 it do not happen anything. But if the cube number next time is 3 the program will say that player 1 got score. I'm Think I got it a bit. Thank you! You're right! . But still thinking of how to fix it.
    Last edited by DecoratorFawn82; 07-22-2013 at 08:44 AM.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Read again what I said about the check function and the i-th number.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    With regards to lines 44 to 97:
    You have used an array, which is the first step towards writing compact code.
    You have however, neglected to turn the code for these lines into loops. Have a try doing that. You'll learn plenty and will be glad you did it. We are here if you need us.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    1. Why do you have classes and inheritance? You're not actually using object-oriented programming methods; your objects have no state so everything "OOP" about your code is actually useless to you.

    2. Code smell: all your state is global.

    3. Code smell: You have multiple arrays of primitives (all your arrays[4]) instead of a single array or collection of a composite type.

    4. Code smell: Check spells out a series of duplicated code sections differing only by array index, instead of looping.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MD5 program not giving correct hashes.
    By Syscal in forum C Programming
    Replies: 4
    Last Post: 04-30-2012, 04:11 AM
  2. output for array of pointers is not giving correct values
    By nkrao123@gmail. in forum C Programming
    Replies: 8
    Last Post: 09-03-2011, 07:40 AM
  3. Replies: 9
    Last Post: 07-15-2011, 03:22 PM
  4. Replies: 3
    Last Post: 07-24-2002, 08:46 AM
  5. Trying to get it to name player who score the most
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 08:36 PM