Hi all. I know that others have posted about Rock Paper Scissors, but none of their posts are exactly what I am needing.

My issue is that for my program, we are required to use at least 1 switch and 1 if statement, as well as keep track of the wins and display this along with the number of games played to the user before ending the game.

Here is what I have so far:

/*
* rockPaperScissors
* a game of Rock Paper Scissors
*
* Zack Conway
* Created: 9/7/11
* Last Modified: 9/10/11
*
* References:
*
*/

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int main()
{

    string playerName;
    char replay;
    int cpuPick, playerPick, gamesPlayed, win, loss;
    gamesPlayed = 0;
    win = 0;
    loss = 0;

    cout << "Hello player, please enter your name" << endl;
    cin >> playerName;
    cout << endl << "Welcome " << playerName << ", are you feeling lucky?" << endl;
    cout << "So we are going to play a game of Rock, Paper, Scissors." << endl;
    cout << "Are you ready to test your luck?" << endl << endl;

    restart:

    srand( time(0) );
    cpuPick = rand() % 3 + 1;

    cout << "Please select from the following:" << endl;
    cout << "1 - Rock" << endl;
    cout << "2 - Paper" << endl;
    cout << "3 - Scissors" << endl << endl;

    cout << "Please make your selection." << endl;
    cin >> playerPick;
    cout << endl <<"Lets see how you do." << endl;

    switch (cpuPick)
    {
        case 1:
            if ( playerPick == 1 )
            cout << "Rock and rock, nobody wins." << endl;
            cout << "Try again:" << endl;
            gamesPlayed++;
            cout << "Want to play again? Press 'Y' or 'y' if you do." << endl << endl;
            cin >> replay;

            if (replay == 'Y' || replay == 'y')
            {
                system("CLS");
                goto restart;
            }

            else
            {
                system("CLS");
                cout << "You won " << win << " out of " << gamesPlayed << ". Thats great." << endl;
                cout << "And you only lost " << loss << " times." << endl;
                return(0);
            }
            break;

            if ( playerPick == 2 )
            cout << "Paper covers rock." << endl;
            cout << "Great job." << endl;
            win++;
            gamesPlayed++;
            cout << "Want to play again? Press 'Y' or 'y' if you do." << endl << endl;
            cin >> replay;

            if (replay == 'Y' || replay == 'y')
            {
                system("CLS");
                goto restart;
            }

            else
            {
                system("CLS");
                cout << "You won " << win << " out of " << gamesPlayed << ". Thats great." << endl;
                cout << "And you only lost " << loss << " times." << endl;
                return(0);
            }
            break;

            if ( playerPick == 3 )
            cout << "Scissors are crushed by rock." << endl;
            cout << "Maybe next time." << endl;
            loss++;
            gamesPlayed++;
            cout << "Want to play again? Press 'Y' or 'y' if you do." << endl << endl;
            cin >> replay;

            if (replay == 'Y' || replay == 'y')
            {
                system("CLS");
                goto restart;
            }

            else
            {
                system("CLS");
                cout << "You won " << win << " out of " << gamesPlayed << ". Thats great." << endl;
                cout << "And you only lost " << loss << " times." << endl;
                return(0);
            }
            break;

        case 2:
            if ( playerPick == 1 )
            cout << "Paper covers rock." << endl;
            cout << "Maybe next time." << endl;
            loss++;
            gamesPlayed++;
            cout << "Want to play again? Press 'Y' or 'y' if you do." << endl << endl;
            cin >> replay;

            if (replay == 'Y' || replay == 'y')
            {
                system("CLS");
                goto restart;
            }

            else
            {
                system("CLS");
                cout << "You won " << win << " out of " << gamesPlayed << ". Thats great." << endl;
                cout << "And you only lost " << loss << " times." << endl;
                return(0);
            }
            break;

            if ( playerPick == 2 )
            cout << "Paper and paper do nothing." << endl;
            cout << "Try again." << endl;
            gamesPlayed++;
            cout << "Want to play again? Press 'Y' or 'y' if you do." << endl << endl;
            cin >> replay;

            if (replay == 'Y' || replay == 'y')
            {
                system("CLS");
                goto restart;
            }

            else
            {
                system("CLS");
                cout << "You won " << win << " out of " << gamesPlayed << ". Thats great." << endl;
                cout << "And you only lost " << loss << " times." << endl;
                return(0);
            }
            break;

            if ( playerPick == 3 )
            cout << "Scissors cut paper." << endl;
            cout << "Great job." << endl;
            win++;
            gamesPlayed++;
            cout << "Want to play again? Press 'Y' or 'y' if you do." << endl << endl;
            cin >> replay;

            if (replay == 'Y' || replay == 'y')
            {
                system("CLS");
                goto restart;
            }

            else
            {
                system("CLS");
                cout << "You won " << win << " out of " << gamesPlayed << ". Thats great." << endl;
                cout << "And you only lost " << loss << " times." << endl;
                return(0);
            }
            break;

        case 3:
            if ( playerPick == 1 )
            cout << "Rock breaks scissors." << endl;
            cout << "Great job." << endl;
            win++;
            gamesPlayed++;
            cout << "Want to play again? Press 'Y' or 'y' if you do." << endl << endl;
            cin >> replay;

            if (replay == 'Y' || replay == 'y')
            {
                system("CLS");
                goto restart;
            }

            else
            {
                system("CLS");
                cout << "You won " << win << " out of " << gamesPlayed << ". Thats great." << endl;
                cout << "And you only lost " << loss << " times." << endl;
                return(0);
            }
            break;

            if ( playerPick == 2 )
            cout << "Scissors cut paper." << endl;
            cout << "Maybe next time." << endl;
            loss++;
            gamesPlayed++;
            cout << "Want to play again? Press 'Y' or 'y' if you do." << endl << endl;
            cin >> replay;

            if (replay == 'Y' || replay == 'y')
            {
                system("CLS");
                goto restart;
            }

            else
            {
                system("CLS");
                cout << "You won " << win << " out of " << gamesPlayed << ". Thats great." << endl;
                cout << "And you only lost " << loss << " times." << endl;
                return(0);
            }
            break;

            if ( playerPick == 3 )
            cout << "Scissors and scissors do nothing." << endl;
            cout << "Try again." << endl;
            gamesPlayed++;
            cout << "Want to play again? Press 'Y' or 'y' if you do." << endl << endl;
            cin >> replay;

            if (replay == 'Y' || replay == 'y')
            {
                system("CLS");
                goto restart;
            }

            else
            {
                system("CLS");
                cout << "You won " << win << " out of " << gamesPlayed << ". Thats great." << endl;
                cout << "And you only lost " << loss << " times." << endl;
                return(0);
            }
            break;
    }

}

It seems like everything works fine when you select 1 for rock, but does not work if you select 2 for paper or 3 for scissors.

I know where must be a logical error somewhere, but am having a hard time finding it. If you have any suggestions or might know where I should begin, that would be very appreciated.

Thanks,

SquigsSquigley

Yes, start over. 260 lines is way too many for this program.
1) Get rid of ALL the system("cls") calls -- they are annoying to the user at best.
2) NEVER use a goto statement. They are not needed in 99.99% of C/C++ programs.
3) In each switch statement for the cpuPick add another switch statement for the playerPick and set a simple result of win, loss, tie. Only.
4) After the switch statements, print the results.

Should be a very short program.

While WaltP's input is excellent, the actual problem with your playerPick is the absense of braces around the code-block for each choice. Starting at line 53, this should be:

...
    if (playerPick == 1) {
        // indent here!
        ...

and there should be a matching close-brace after line 73. Repeat throughout.

In addition, identical chunks of code are "A Bad Thing". If you realize you need to fix one, then (in your case) you need to fix 9 of them, all the same way, without missing any or accidentally getting one wrong.

Instead, I'd recommend writing simple functions at least for the following:

bool PlayAgain(void);  // prompt the user, get his response, return true or false
bool ShowStats(int wins, int losses, int gamesPlayed);

As WaltP suggested, see how short you can get your outer switch statement without losing any current functionality. Note: anything you do every time, no matter what, can be done after the end of the switch statement, instead of doing it each time it comes up.

As far as the goto statements, yeah, get rid of those. Instead try something like:

bool keepPlaying = true;
while (keepPlaying) {
    // srand() only needs to be called once, so do it before the loop
    // start with the CPU picking its number
    ...

    // any time you have a goto, instead do:
    keepPlaying = false;

    // and then make sure you don't inadvertently execute any code below that point --
    // since you have only one loop, you can simply do:
    continue;

    // (if you had nested loops, you would need to be more particular getting
    // control back to the top of the outer loop.)
}

// definitely done playing, this is a good place to print out your win/loss stats!

One issue with functions is I've only been programming for two weeks and don't know functions yet. I guess I will read ahead some in my book.

No problem. We were all just starting out at one point or another. As long as you can get it working, that's what counts for now. Points for style can wait until style is expected of you. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.