0

Intro to CS with C++ Practice.

I am having trouble figuring out how I can assign the random number that Player 1 and Player 2 get to the string values "p1_string_val" and "p2_string_val". I displayed the code I have so far down below.

Could someone please help me figure out how to assign the random number to the strings? Any further help would also be welcomed! Thank you so much.

INSTRUCTIONS FOR ASSIGN: I have completed step 0 and 1. I need help assigning the random number to the strings and any other help would be helpful.

My code reads as this rn:
"Player one has rock/paper/scissors
Player two has rock/paper/scissors
Please press any key to continue"

  1. Based off of the numerical values stored inside of P1_int_val and P2_int_val, use if, else, and else if statements to store the following strings in P1_string_val, P2_string_val.

int_val 0 string_val rock
int_val 1 string_val paper
int_val 2 string_val scissors


  #include <fstream>
     #include <iostream>
     #include <string>
     #include <time.h>

     using namespace std; 


      void main()

     {
         int p1_int_val, p2_int_val; 
    string p1_string_val, p2_string_val; 

     srand(time(NULL)); 

     p1_int_val = (rand() % 3);
     p2_int_val = (rand() % 3);

     //to check the random integer are between 0-2 
     /*cout << "Player one has this " << p1_int_val << endl; 
     cout << "Player two has this " << p2_int_val << endl; */


     if (p1_int_val == 0)
         cout << "Player one has rock " << endl; 

     if (p1_int_val == 1)
         cout << "Player one has paper" << endl;

     else if (p1_int_val == 2)
         cout << "Player one has scissors" << endl; 

     if (p2_int_val == 0)
         cout << "Player two has rock " << endl;

     if (p2_int_val == 1)
         cout << "Player two has paper" << endl;


      else if (p2_int_val == 2)
                 cout << "Player two has scissors" << endl;
 return 0; 
         } 

Once the program has been completed it should display:

//Player one has rock/paper/scissors (depends on what number they got)  
//Player two has rock/paper/scissors   
//Player one or two has won OR TIE



  • Try adding `else` to your second `if` in each block (for the `==1`) (won't fix your problem, but makes it easier to read.) – Andy Jun 17 '19 at 23:48
  • Obligatory warning about `rand()`: [Rand Considered Harmful](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) – user4581301 Jun 18 '19 at 00:52

2 Answers2

1

If you want to assign a number to string, you can certainly use std::to_string function. std::to_string is a part of standard library since C++11.

std::string random_number = std::to_string(rand()) 

Alternative way, if dealing with older versions of C++ is to use .str() which is part of sstream library

std::stringstream ss;
ss << rand();
std::string random_number = ss.str();
ewaolx
  • 98
  • 1
  • 8
0

Is this what it needs to do?

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

using namespace std;

int WhoWon(int p1, int p2)
{
    if (p1 == p2)
        return 0;
    else if (p1 == 0 && p2 == 2)
        return 1;
    else if (p2 == 0 && p1 == 2)
        return 2;
    return p1 > p2 ? 1 : 2;
}

int main()
{   
    int P1_int_val, P2_int_val;
    string P1_string_val, P2_string_val;

    srand(time(NULL));

    P1_int_val = rand() % 3;
    P2_int_val = rand() % 3;

    if (P1_int_val == 0)
    {
        P1_string_val = "rock";
    }
    else if (P1_int_val == 1)
    {
        P1_string_val = "paper";
    }
    else
    {
        P1_string_val = "scissors";
    }

    if (P2_int_val == 0)
    {
        P2_string_val = "rock";
    }
    else if (P2_int_val == 1)
    {
        P2_string_val = "paper";
    }
    else
    {
        P2_string_val = "scissors";
    }

    cout << "Player one got " << P1_string_val << endl;
    cout << "Player two got " << P2_string_val << endl;

    cout << "Winner is: ";
    const int won = WhoWon(P1_int_val, P2_int_val);
    if (won == 0)
    {
        cout << "tie" << endl;
    }
    else if (won == 1)
    {
        cout << "player one" << endl;
    }
    else
    {
        cout << "player two" << endl;
    }

    return 0;
}
Andy
  • 12,859
  • 5
  • 41
  • 56
  • 1
    This is a code only answer. You can strengthen it by explaining the problem, highlighting your changes, and if necessary, explaining how the changes solve the problem. – user4581301 Jun 18 '19 at 00:15
  • meh -- i think he was just asking someone to do his homework for him. Question is, does this rack up real world karma? or does it diminish it? – Andy Jun 18 '19 at 00:16
  • I dunno. It's legit attempt to help and it doesn't look like it's got any bad advice in it, so I didn't downvote. I don't get to downvote real-world Karma, but I do think an answer is most useful when delivered with some explanation of the process. – user4581301 Jun 18 '19 at 00:20
  • absolutely -- I totally am on board with you... but he posted the entire assignment instructions... The question he is asking is answered in said assignment instructions. – Andy Jun 18 '19 at 00:24
  • Hi hello! I wasn't asking anyone to do my homework for me but I appreciate every piece of advice! I am a Forensic Science major so computer science is a completely foreign language hehe, but thank you so much for the help. –  Jun 18 '19 at 00:27
  • 1
    Makes the explanation a bit simpler then. Quote the relevant section of the assignment and give a quick blurb of how you implemented it or would implement it. – user4581301 Jun 18 '19 at 00:33