We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,599 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Paper Scissors Rock not counting Wins

Hey guys, so I'm trying to make a Paper, scissors, rock program (where you play against the computer). And i got pretty far, to the point where it seemed finished. But then i realized that i need to make the program exit itself after the player wins three times.

So i tried using a Do and While loop, but its not working.
Heres the main code...

/*
This is a simple Paper, Scissors, Rock program. 
You press 1, 2 or 3 to select your choice and 
once you win three times, the program closes.
*/
#include "Assignment4.h"
using namespace std;
int main()
{
    srand ( time(NULL) );
    int choice;
    int compchoice = (rand()%2)+1;
    int winCount = 0;

    //Creating the interface
    cout << "Welcome to the Rock, Paper, Scissors game." << endl;
    cout << " 1 = Rock, 2 = Paper, 3 = Scissor, 4 = Exit Program" << endl;
    cin >> choice;

    do
    {
        if (choice == 1) 
        {
            if (compchoice == 0)
            cout << "It's a tie!\n\n\n\n";
            else if (compchoice == 1)
            cout << "Paper beats rock! Sorry, you lose!\n\n\n\n";
            else if (compchoice == 2)
            {
                cout << "Rock beats scissors! You win!\n\n\n\n";
                winCount += 1;
            }
        }

        if (choice == 2)
        {
            if (compchoice == 0)
            cout << "It's a tie!\n\n\n\n";
            else if (compchoice == 1)
            {
                cout << "Paper beats rock! You win!\n\n\n\n";
                winCount += 1;
            }
            else if (compchoice == 2)
            cout << "Scissors beat paper! Sorry, you lose!\n\n\n\n";
        }

        if (choice == 3)
        {
            if (compchoice == 0)
            cout << "It's a tie!\n\n\n\n";
            else if (compchoice == 1)
            {
                cout << "Scissors beat paper! You win!\n\n\n\n";
                winCount += 1;
            }
            else if (compchoice == 2)
            cout << "Rock beats scissors! Sorry, you lose!\n\n\n\n";
        }
        if (choice == 4)
        {
            return 0;
        }

        return main();
    }

    while (winCount >= 3);
    {
        cout << "You won three times!" << endl;
        //return 0;

    }
    //return main();
}

These last few lines, after the While statement, is where the problem occurs; i thought it had something to do with my placement of the return main(); but whereever i place it, the program either exits the moment the computer (or the player) wins, or it doesnt actually exit at all and acts as if i never placed the loop (pressing 4 will still close it, however).

I'm pretty sure im missing something with this While function...can someone point me to where?

2
Contributors
7
Replies
3 Hours
Discussion Span
6 Months Ago
Last Updated
9
Views
Question
Answered
Magic681
Newbie Poster
7 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

In line 65 you've got return main(), that's part of the problem. You should never recursively call the entry point of a program.

The do/while loop should look like:

do
{
    // calculate compchoice
    // cin >> choice
    // process data

} while (winCount < 3);

After that you'd display the win message, pause execution of the program so the message can be read and finally return 0;

nullptr
Junior Poster
158 posts since Mar 2012
Reputation Points: 46
Solved Threads: 33
Skill Endorsements: 0

For compchoice you want values of 0 to 2 inclusive. At present you have compchoice = (rand()%2)+1;
meaning compchoice will either be 1 or 2.

Change it to compchoice = rand() % 3;

nullptr
Junior Poster
158 posts since Mar 2012
Reputation Points: 46
Solved Threads: 33
Skill Endorsements: 0

Okay...but now it loops infinitely if you lose to the computer.

Also...if i win, once, the message suddenly displays three times, in a row, then prompts me to press any key, of which it will then exit. Something tells me i'm on the wrong track...?

The lines i changed are now;

while (winCount < 3);
cout << "you won three times" << endl;

return 0;
Magic681
Newbie Poster
7 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Can you post the complete code you now have? What do you want to happen if you lose to the computer?

nullptr
Junior Poster
158 posts since Mar 2012
Reputation Points: 46
Solved Threads: 33
Skill Endorsements: 0

Uh, okay, nothing much has changed.

/*#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>*/
/*
This is a simple Paper, Scissors, Rock program. 
You press 1, 2 or 3 to select your choice and 
once you win three times, the program closes.
*/
#include "Assignment4.h"
using namespace std;
int main()
{
    srand ( time(NULL) );
    int choice;
    int compchoice = (rand()%2)+1;
    int winCount = 0;

    //Creating the interface
    cout << "Welcome to the Rock, Paper, Scissors game." << endl;
    cout << " 1 = Rock, 2 = Paper, 3 = Scissor, 4 = Exit Program" << endl;
    cin >> choice;

    do
    {
        if (choice == 1) 
        {
            if (compchoice == 0)
            cout << "It's a tie!\n\n\n\n";
            else if (compchoice == 1)
            cout << "Paper beats rock! Sorry, you lose!\n\n\n\n";
            else if (compchoice == 2)
            {
                cout << "Rock beats scissors! You win!\n\n\n\n";
                winCount += 1;
            }
        }

        if (choice == 2)
        {
            if (compchoice == 0)
            cout << "It's a tie!\n\n\n\n";
            else if (compchoice == 1)
            {
                cout << "Paper beats rock! You win!\n\n\n\n";
                winCount += 1;
            }
            else if (compchoice == 2)
            cout << "Scissors beat paper! Sorry, you lose!\n\n\n\n";
        }

        if (choice == 3)
        {
            if (compchoice == 0)
            cout << "It's a tie!\n\n\n\n";
            else if (compchoice == 1)
            {
                cout << "Scissors beat paper! You win!\n\n\n\n";
                winCount += 1;
            }
            else if (compchoice == 2)
            cout << "Rock beats scissors! Sorry, you lose!\n\n\n\n";
        }
        if (choice == 4)
        {
            return 0;
        }


    }

    while (winCount < 3);
    cout << "you won three times" << endl;

    return 0;
    //return main();
}

If computer wins, you must try again to beat the computer.

If you win three times (doesnt have to be consecutive), the game displays a message stating "You've won" and exits the program by pressing any key

Also...Im no longer a newbie! So to show my epicness, gonna have to not delete the message that was in the text field.

Congratulations! You're no longer a DaniWeb newbie.<br /> <br />
Your DaniWeb account has just been upgraded from newbie status and now you have the ability to take advantage of everything the community has to offer.<br /> <br />
You can now enjoy an advertisement-free DaniWeb by ticking the checkbox to Disable Ads in your profile. You will no longer have to fill out the human verification check when you post. You can also now send unlimited private messages, contribute new code snippets, and tag articles with never-before-used tags.

Magic681
Newbie Poster
7 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This should work for you. Note that you have only 3 choices so there's no need for
else if (compchoice == 2). As it's the only remaining condition, you can simply have else

int main()
{
    srand ( time(NULL) );
    int choice;
    int compchoice;
    int winCount = 0;

    //Creating the interface
    cout << "Welcome to the Rock, Paper, Scissors game." << endl;

    do
    {
        cout << " 1 = Rock, 2 = Paper, 3 = Scissor, 4 = Exit Program" << endl;

        compchoice = rand() % 3;
        cin >> choice;
        // flush input stream
        cin.ignore(90, '\n');

        if (choice == 1) 
        {
            if (compchoice == 0)
                cout << "It's a tie!\n\n\n\n";
            else if (compchoice == 1)
                cout << "Paper beats rock! Sorry, you lose!\n\n\n\n";
            else //if (compchoice == 2)
            {
                cout << "Rock beats scissors! You win!\n\n\n\n";
                winCount += 1;
            }
        }

        if (choice == 2)
        {
            if (compchoice == 0)
                cout << "It's a tie!\n\n\n\n";
            else if (compchoice == 1)
            {
                cout << "Paper beats rock! You win!\n\n\n\n";
                winCount += 1;
            }
            else //if (compchoice == 2)
                cout << "Scissors beat paper! Sorry, you lose!\n\n\n\n";
        }

        if (choice == 3)
        {
            if (compchoice == 0)
                cout << "It's a tie!\n\n\n\n";
            else if (compchoice == 1)
            {
                cout << "Scissors beat paper! You win!\n\n\n\n";
                winCount += 1;
            }
            else //if (compchoice == 2)
                cout << "Rock beats scissors! Sorry, you lose!\n\n\n\n";
        }

        if (choice == 4)
        {
            return 0;
        }

    } while (winCount < 3);

    cout << "You won three times" << endl;
    cout << "press Enter to exit...";

    cin.get();

    return 0;
}
nullptr
Junior Poster
158 posts since Mar 2012
Reputation Points: 46
Solved Threads: 33
Skill Endorsements: 0

Oh my god, this is perfect!
Theres a few things i dont understand, mostly that cin.ignore(90, '\n'); thing, but damn i hadnt realized the problem lay in the base Do loop.

Thanks so much man, been stuck on that one thing for ages.

Magic681
Newbie Poster
7 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 6 Months Ago by nullptr

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0837 seconds using 2.76MB