Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with c++ program - Generate three random numbers (0, 1, or 2) - Save t

ID: 3834446 • Letter: N

Question

Need help with c++ program

- Generate three random numbers (0, 1, or 2)
   - Save the three numbers in an array called: int compplay[3]
   -- Or ... if number is 0, save an 'R' in: char compplay[3]
   -- ...... if number is 1, save a 'P,
   -- ...... if number is 2, save a 'S'
   -- Or ... you can use enumerated types

   -Set a flag to 1 (later, if user wants to quit, set it to 0)

   -Have a for loop that loops 3 times (0-2) unless user wants to quit
   -- Have user1 enter R for rock, P for paper, or S for scissors
   -- If the user entered an R and complay[0] is an R, then you have a tie
   -- else if the user entered an R and complay[0] is a P then user loses
   -- else if the user entered an R and complay[0] is an S then user wins

   --- Ask user if they want to keep playing. If it's an n or N, then
   use break to get out of loop

   The loop should now repeat and you do this again comparing to complay[1], etc...

   -Thank user for playing when they are done with their three attempts.

   HERE are the rules which determines who wins:
   Rock beats scissors
   Paper beats rock
   Scissors beats paper

Explanation / Answer

#include<iostream.h>
#include<stdlib.h>
#include<time.h>

void main() {

   int num,i,flag;
   char ch;
  
   char compplay[3];
   srand(time(NULL));
  
  
   for (i = 0; i<3; i++){
       num = rand() % 2;
       switch (num) {
          case 0:
              compplay[i] = 'R';
         case 1:
              compplay[i] = 'P';
         case 2:
              compplay[i] = 'S';
       }
              
   }
   flag = 1;
   for (i=0; i<3 && flag == 1; i++){
      cout << "Please enter 'R' for rock, 'S' for Scissor ,'P' for paper , 'N' to quit" << endl;
      cout << "Enter your choice:" << endl;
      cin >> ch;
      switch (ch) {
          case 'R':
             if (compplay[i] == 'S')
                cout << "User wins" << endl;
             if (compplay[i] == 'P')
                cout << "User loses" << endl;
            if (compplay[i] == 'R')
                cout << "Game ties" << endl;  
          case 'P':
             if (compplay[i] == 'R')
                cout << "User wins" << endl;
             if (compplay[i] == 'S')
                cout << "User loses" << endl;
            if (compplay[i] == 'P')
                cout << "Game ties" << endl;      
          case 'S':
             if (compplay[i] == 'P')
                cout << "User wins" << endl;
             if (compplay[i] == 'R')
                cout << "User loses" << endl;
            if (compplay[i] == 'S')
                cout << "Game ties" << endl;
          case 'N':
             flag = 0;
      }

   }  
}