将int值附加到字符串?

时间:2019-06-17 23:35:30

标签: c++ string variables

C ++练习CS简介。

我在弄清楚如何将播放器1和播放器2获得的随机数分配给字符串值“ p1_string_val”和“ p2_string_val”时遇到麻烦。我在下面显示了到目前为止的代码。

有人可以帮我弄清楚如何为字符串分配随机数吗?任何进一步的帮助也将受到欢迎!非常感谢。

分配说明: 我已经完成了步骤0和1。我需要将随机数分配给字符串的帮助,其他任何帮助都将有所帮助。

我的代码读为rn:
 “第一人有石头/纸/剪刀
玩家二有石头/剪刀布
请按任意键继续”

  1. 基于存储在P1_int_val和P2_int_val内部的数值,使用if,else和else if语句将以下字符串存储在P1_string_val,P2_string_val中。

int_val 0 string_val岩石
int_val 1字符串_val纸
int_val 2条string_val剪刀


  #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



2 个答案:

答案 0 :(得分:1)

如果要为字符串分配数字,则可以使用std::to_string函数。 std::to_string自C ++ 11以来就是标准库的一部分。

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

或者,如果要处理旧版本的C ++,则使用.str()库中的sstream

std::stringstream ss;
ss << rand();
std::string random_number = ss.str();

答案 1 :(得分:0)

这是需要做的吗?

#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;
}