I'm just wondering if anyone can take a look at my current program below and see what im missing. ive been working on it for so long that i'm just stuck and need another help to kind of get me going in the right direction.

I'm trying to create the Paper/Rock/Scissors game, but CANT get the computer to generate random outputs.


the objective: user inputs R (rock), P (paper), S (scissors) or Q (quit).
computer is then to automatically play the game with user.
the winner is determined, etc.

problem: how do i get to generate the computer to play with the user?!

limits/restrictions: i am currently in a beginner's c++ class. the furthest we've gone are Subprograms, Voids, Value-Returning Functions. Teacher wants us to use such tools to create the game.

The program below runs and compiles properly, but of course, the computer doesn't output anything because i have no idea how to come about including the computer to think for its own. I'm slowly giving up, but i cant stop thinking about it. i hope someone can help me. any suggestion would be greatly appreciated. thank you.


...KristiNE...
_________________________________________

#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;


void results();
char human();
char computer();


char human()
{
  char humchoice;

  while (true)
  {
    cout << " " << endl;
    cout << "Choose [R-Rock, P-Paper, S-Scissors, Q-Quit]: ";
    cin >> humchoice;

    if (humchoice == 'Q' || humchoice == 'q') break;
    if (humchoice == 'R' || humchoice == 'r') break;
    if (humchoice == 'P' || humchoice == 'p') break;
    if (humchoice == 'S' || humchoice == 's') break;
    cout << " INVALID ENTRY. Try again..." << endl;
 }
  return humchoice;
} // human


char computer()
{
  char compchoice = rand()%3;
     

  return compchoice;
}


void results()
{
   
}



int main ()
{

  // initialize the computer's random number generator
  srand((unsigned)time(0));


  // declare variables
  char humchoice;
  char compchoice;

  // start loop
  while (true)
  {

    // determine computer's choice
    compchoice = computer();

    // prompt for, and read, the human's choice
    humchoice = human();

    // if human wants to quit, break out of loop
    if (humchoice == 'Q' || humchoice == 'q') break;


  // print results
  cout << "Computer: " << compchoice << endl;
  cout << "Human: " << humchoice << endl;


  // end loop
  }

 
  // end program
  cin.ignore();
  cin.get();
  return 0;
} // main
Member Avatar for iamthwee

You have an idea.

Get a random number 1-3

If 1 then rock
if 2 then scissors
if 3 then paper

Yes?

commented: Yes :) +16
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.