• Register
2 votes
659 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,240 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 100 views
100 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 6.3k points
0 votes
1 answer 8 views
8 views
Write a palindrome program in c++ using string functions?
asked Oct 10, 2020 sikandar 2.4k points
0 votes
1 answer 71 views
0 votes
1 answer 2 views
2 views
Problem: How to use callback function in C++ ?
asked 2 days ago Ashty 1.7k points
0 votes
1 answer 20 views
20 views
Problem Hey, what are free and delete functions? Which parameters are used in them. Answer me as good as you can.
asked Dec 17, 2020 chris jordan 2.4k points
0 votes
2 answers 13 views
13 views
Problem Hey, I just want to know what Getters and Setters function is and what are the advantages of going so.
asked Dec 12, 2020 chris jordan 2.4k points
0 votes
1 answer 39 views
39 views
Problem: Hi there, I am a noob and recently while studying functions I came across the following program that created confusion in my mind: #include<iostream> using namespace std; void add(int a, int b) { cout<<"The sum of "<<a<<" and "<<b<<" is "<<a+b; } ... ,5,6); return 0; } First of all, why are all the functions named as add? Secondly, how does the compiler identifies which function to call?
asked Oct 14, 2020 Code Learner 6.3k points
0 votes
1 answer 18 views
18 views
Problem: Can member functions be constant in C++? If yes, then what is the purpose of constant member functions and how do we declare, define and use them?
asked Oct 14, 2020 Code Learner 6.3k points
1 vote
1 answer 75 views
75 views
Problem: I have recently started learning C language. Let&rsquo;s say I have a myprog.c file in which I have the local variable a. Now I want to know can I also have the function in my above c file which has the very same name a? As far as I know a local ... if I tried this my compiler greets me with the error. I want to know if this is the compiler specific error or it is actually the error.
asked May 25, 2020 Martin K 6.6k points
0 votes
1 answer 9 views
9 views
Problem: What is reverse string in c++ without using function
asked Feb 14 jasimchaudary 640 points