• Register
2 votes
1.2k views

Problem:

Hello good people,

I just completed the basic C++ programming course by taking help from a YouTube channel. It was a 15-days long course. After completing the course they give an exercise/assignment. They asked me to write a program of Rock-Paper-Scissors game of two players. So, I gave it a try and write some codes to make a game like that. Find my sample code snippet below and dig into it, please

#include <iostream> 
#include <cstdlib> 
#include <stdio.h>
using namespace std; 

int main() 
{ 
    string fp; 
    string sp; 
    string r; 
    string p; 
    string s; 

    cout << "Rock, Paper, Scissors Game\n"; 
    cout << "\nPlayer One, please enter your move: ('p' for Paper, 'r' for Rock, 's' for Scissor)";  

    cin >> fp; 

    cout << "\nPlayer Two, please enter your move: ('p' for Paper, 'r' for Rock, 's' for Scissor)"; 

    cin >> sp; 

    if (fp == sp) 
    { 
        cout <<"\nThere is a tie"<<endl; 
    } 

    if ( fp == r && sp == p) 
    { 
        cout << "\nPaper wraps rock, Player 1 win"; 
    } 
    else if (fp == r && sp == s) 
    { 
        cout << "\nRock smashes scissors, player 1 win"; 
    } 

    if (fp == p && sp == r) 
    { 
        cout <<"\nPaper wraps rock, player 1 win"; 
    } 
    else if ( fp == p && sp == s) 
    { 
        cout <<"\nScissors cut paper, player 2 win"; 
    } 

    if ( fp == r && sp == p) 
    { 
        cout << "\nPaper wraps rock, Player 1 win"; 
    } 
    else if (fp == r && sp == s) 
    { 
        cout << "\nRock smashes scissors, player 1 win"; 
    } 

    if (fp == p && sp == r) 
    { 
        cout <<"\nPaper wraps rock, player 1 win"; 
    } 
    else if ( fp == p && sp == s) 
    { 
        cout <<"\nScissors cut paper, player 2 win"; 
    } 

    if ( sp == s && fp == r) 
    { 
        cout <<"\nScissors cut paper, player 1 win"; 
    } 
    else if (sp == s && fp == p) 
    { 
        cout <<"\nRock smashes scissors, player 2 win "; 
    } 

    return 0; 
}

The above program is not printing any results. I tried so hard but still couldn’t figure the problem in my codes. Could anybody please save me this time?

Thanks for your kind help.

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

2 votes

Solution:

First of all, you missed the double quotes around your strings “r”, “p”, “s” inside the if-elf statement. Remember, you can’t write strings without the double-quotes. The strings must stay inside the ( “ ” ) So, change the action inside your if-elf statements first. One more thing you did wrong. You repeated the second and third if-elf block in your code. Omit them from your code. Then it should run fine. Let me write the code in this way:

#include <iostream> 
#include <cstdlib>
#include <stdio.h>
using namespace std;
int main(){ 
    string fp; 
    string sp; 
    string r; 
    string p; 
    string s; 
    cout << "Rock, Paper, Scissors Game\n"; 
    cout << "\nPlayer One, please enter your move: ('p' for Paper, 'r' for Rock, 's' for Scissor)";  
    cin >> fp; 
    cout << "\n\nplayer Two, please enter your move: ('p' for Paper, 'r' for Rock, 's' for Scissor)"; 
    cin >> sp;
    if (fp == sp){ 
        cout <<"\n\n\nThis is a tie"<<endl; 
    }

    if ( fp == "r" && sp == "p"){ 
        cout << "\nPaper wraps rock, Player 1 win"; 
    }
    
    else if (fp == "r" && sp == "s"){ 
        cout << "\nRock smashes scissors, player 1 win"; 
    } 

    if (fp == "p" && sp == "r"){ 
        cout <<"\nPaper wraps rock, player 1 win"; 
    }
    
    else if ( fp == "p" && sp == "s"){ 
        cout <<"\nScissors cut paper, player 2 win"; 
    }

    if ( sp == "s" && fp == "r"){ 
        cout <<"\nScissors cut paper, player 1 win"; 
    }
    
    else if (sp == "s" && fp == "p"){ 
        cout <<"\nRock smashes scissors, player 2 win "; 
    } 

    return 0; 
}

Leave your reply below if you got further queries. Thanks.

13 9 6
94,260 points
0 votes

Solution:

#include <iostream>
#include <cstdlib>  // For rand() and srand()
#include <ctime>    // For the time() function

// Constant variables
const int ROCK = 1,
          PAPER = 2,
          SCISSORS = 3;

// Function Prototypes
int inputValidate(int);
int computerChoice(int);
int getUserChoice(int);
void displayRandomNumber(int);
void winnerSelection(int, int, bool &);

using namespace std;

int main()
{
    int computer_choice,
        user_number;

    bool play_again;

    do
    {
        computer_choice = computerChoice(computer_choice);

        user_number = getUserChoice(user_number);

        displayRandomNumber(computer_choice);

        winnerSelection(computer_choice, user_number, play_again);

    } while (play_again == 1);

    return 0;
} // END int main()

int computerChoice(int computer_choice)
{
    // Get the system time.
    unsigned seed = time(0);

    // Seed the random number generator.
    srand(seed);

    // Random number
    return (rand() % (SCISSORS - ROCK + 1)) + ROCK;
}

int getUserChoice(int user_number)
{
    cout << "\nChoose 'rock', 'paper', or 'scissors'.\n"
         << "(1) for rock, (2) for paper, or (3) for scissors: ";
         
    user_number = inputValidate(user_number);

    return user_number;
}

int inputValidate(int num1)
{

    while (!(cin >> num1) || (num1 < 1 || num1 > 3))
    {
        cout << "Error. Number must not be "
             << " 0 or greater: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    return num1;
}

void displayRandomNumber(int computer_choice)
{
    cout << "\nComputer Choice = ";

    if (computer_choice == 1)
        cout << "Rock";
    else if (computer_choice == 2)
        cout << "Paper";
    else if (computer_choice == 3)
        cout << "Scissors";

    cout << endl;
}

void winnerSelection(int computer_choice,
                     int user_number,
                     bool &play_again)
{
    cout << endl;

    if (computer_choice == ROCK)
    {
        if (user_number == PAPER)
        {
            cout << "You win: (Paper covers rock).\n";
            play_again = 0;
        }
        else if (user_number == SCISSORS)
        {
            cout << "Computer wins: (Rock smashes scissors).\n";
            play_again = 0;
        }
        else if (user_number == ROCK)
        {
            cout << "Tie. Play again to determine the winner." << endl;
            play_again = 1;
        }
    }
    else if (computer_choice == PAPER)
    {
        if (user_number == ROCK)
        {
            cout << "Computer wins: (Paper covers rock).\n";
            play_again = 0;
        }
        else if (user_number == SCISSORS)
        {
            cout << "You win: (Scissors cuts paper).\n";
            play_again = 0;
        }
        else if (user_number == PAPER)
        {
            cout << "Tie. Play again to determine the winner." << endl;
            play_again = 1;
        }
    }
    else if (computer_choice == SCISSORS)
    {
        if (user_number == ROCK)
        {
            cout << "You win: (Rock smashes scissors).\n";
            play_again = 0;
        }
        else if (user_number == PAPER)
        {
            cout << "Computer wins: (Scissors cuts paper).\n";
            play_again = 0;
        }
        else if (user_number == SCISSORS)
        {
            cout << "Tie. Play again to determine the winner." << endl;
            play_again = 1;
        }
    }

    cout << endl;
}

Here's your issue:

if (pone == r && ptwo == p) 
{ 
    cout << "\nPaper wraps rock, Player 1 win"; 
} 
else if (pone == r && ptwo == s) 
{ 
    cout << "\nRock smashes scissors, player 1 win"; 
} 
//etc etc

Place those "r", "p", and "s" in quotes like such:

if (pone == "r" && ptwo == "p")
//etc etc

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 5 views
5 views
Problem: These are the instructions: Implement a C++ console application that simulates a Rock-Paper-Scissor game against the computer. When played between two people, each person picks one of three options (usually shown by hand gesture) at the same time, and a winner is determined. In the ... would like to know: * Why this code is not running. * How do I add a way to keep score on a text file.
asked 5 days ago Yeamin 21.4k points
0 votes
0 answers 1 view
1 view
Problem: I was assigned an assignment where we have to create a rock, paper, scissors game (this is beginning programming class that uses java) with methods and return statements. I am having trouble writing all the necessary methods w/h return statements needed to run the program .I am new at this so I have no idea what I am doing. Any advice would be greatly appreciated .
asked 1 day ago salim1 60.6k points
0 votes
1 answer 2 views
2 views
Problem: I have started learning C language. I wrote this program to find all prime numbers between the given range but I am unable to get the expected output.Can anyone tell me what's wrong with this program please?
asked 3 days ago sumaiya simi 43.9k points
0 votes
1 answer 4 views
4 views
Problem: I am trying to write a C program that calculates the min and max value from entered numbers. I managed find the min and max value, but for some reason i cannot print out the values outside the function. This is my code: #include <stdio.h> #include <stdlib.h> void find_largest_smallest ... "How many elements you want to store ?:"); scanf("%d", &elem); int store[elem]; for(i=0; i<elem; i++)
asked 4 days ago ochena 22.1k points
0 votes
1 answer 5 views
5 views
Problem I want to create a small calculator in c++ source code, Can anyone help me for write the source code
asked Apr 16 Subhendu 4.5k points
0 votes
1 answer 5 views
5 views
Problem: Can anyone guide me to create a palindrome program in c++ using string functions ?
asked Apr 10 zayed1 58.7k points
0 votes
1 answer 1 view
1 view
Problem: I actually created the code for palindrome of string using reverse and copy of strings without library function.But still it shows always it is palindrome.What mistake I have done. And it always prints it is always a palindrome regardless of whether I give palindrome or not. I checked the internet for code but want to know what mistake I made in my code so please help.
asked Apr 2 ummesalma 29.2k points
0 votes
1 answer 4 views
4 views
Problem: Help me to fix it String palindrome without using string functions in c?
asked Mar 30 zayed1 58.7k points
0 votes
1 answer 21 views
21 views
Problem: Why I got this error: how to find maximum and minimum of n numbers without using functions in c++ program any idea on what I need to fix this. I am new in c++ Thanks in advance.
asked Mar 11 jasimchaudary 1.5k points
0 votes
1 answer 800 views
800 views
Problem: Beginner here! I have got an assignment in which I have to write a program that finds the sum of array elements taken as input from the user. I have to define a function that calculates the sum and returns the sum. The program should print the sum on the ... logic is not so strong so I just failed to build logic. Could anyone from here help me find the solution to this task? Thanks!
asked Jan 8 Code Learner 9.8k points