im making a program that will let you play rock paper scissors with the computer.

the code will be in function format with a switch statement for the menu.

this is how it should look:

welcome to rock paper scissors!
select a choice
1. rock
2. paper
3. scissors
4. quit

choice: rock
you picked rock.
the computer picked paper

you lose. paper wraps rock

hit enter key to continue......

program should loop to menu again and continue until you decide to quit.
as you quit, it should display how many games you won and how many games the computer won along with any ties.

this is my assignment and im confused as to how my layout should be,

we have just learned how to use function, and since im still new to that aspect, its hard for me to apply it rather than just to one long block of code (this code has to be a function by the way)

any help would be appreciated.

thanks!

Dani AI

Generated

Good decomposition so far. Building on ' suggestion to sketch the flow, break the program into small, single-purpose functions so main only orchestrates the loop and counters. Suggested responsibilities: showMenu(), getPlayerMove(), getComputerMove(), decideWinner(), showRoundResult(), showStats(). Keep the win/lose/tie counters in main and update them after each round. Use a switch for printing user-friendly messages (as recommended) and avoid globals—pass values or use return values.

A compact, robust way to generate the computer move and decide the winner (works well in modern C++):

enum Move { ROCK = 1, PAPER, SCISSORS };
enum Result { TIE, PLAYER_WIN, COMPUTER_WIN };

Move getComputerMove() {
    static std::random_device rd;
    static std::mt19937 gen(rd());
    std::uniform_int_distribution<int> d(1,3);
    return static_cast<Move>(d(gen));
}

Result decideWinner(Move p, Move c) {
    int diff = (static_cast<int>(p) - static_cast<int>(c) + 3) % 3;
    if (diff == 0) return TIE;
    return (diff == 1) ? PLAYER_WIN : COMPUTER_WIN;
}

Notes and troubleshooting tips: the arithmetic trick in decideWinner yields 0=tie, 1=player wins, 2=computer wins and avoids long if-chains. Input validation should loop until a valid integer in range 1–4 is provided. For the "press Enter to continue" pause use a safe getline/ignore pattern to clear the input buffer. If constrained to pre-C++11, the srand/rand approach mentioned by is acceptable, but prefer <random> when available. If the instructor insists the game be "a function," wrap the main loop in a single playGame() that calls the smaller helpers—this keeps the assignment requirement while still using clean, testable code.

Recommended Answers

All 6 Replies

Get out a piece of paper and a pencil.

Sketch out how you think the program should be broken up - you can use flowchart, psuedocode, whatever helps you organize your thoughts.

What are the main aspects? If you tried to write this as one massive main( ) function, what would be the logical parts you could isolate?

Looking at what you've written above, there's a menu, computer selecting choice, testing who won. Perhaps a couple other significant actions.

Work on that, post some code.

here is what i have:

1) display menu
2) ask for user choice
3) user input
--> display user input
4) computer choice (random)
5) test who wins
----> if statement?
6) display winner with statement
7) loop back to menu
---> repeat until quit
8) quit
---> show who won games/ties

That looks right to me, next state it to convert that into code, be it C++ or psuedo.
do each point indervidually, don't try and mix them up.

Chris

ok here is some of the code that i have started on:

#include <iostream>
using namespace std;

int main()

//function protoypes
void menu();
void userChoice();
void computerChoice();

int rock, paper, scissors;

do
{
menu();
cin >> userChoice;

while (userChoice < 1 || userChoice > 4)
{
cout << "please enter 1, 2, 3, or 4";
cin >> userChoice;
}

i know i havent written the actual functions yet...but how do i write a statemnent that will let the computer generate their own random choice? is there a special function to use?

im not gonna comment on errors in your code, since this may just be purposefull...

#include <ctime>

srand( (unsigned) time(NULL) );

randomnumber = rand() % 3 + 1;

that should give you a random number between 1 & 3.

Chris

Now use a switch case statement to use the input from the user.

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.