C++ 不能在c+;中的字符串中包含整数+;

C++ 不能在c+;中的字符串中包含整数+;,c++,C++,当我尝试使用rand()函数生成整数时,我不能将它们包含在字符串中。以下是我尝试过的两种代码: int x1, x2; x1 = (rand() % 14 + 2); x2 = (rand() % 14 + 2); char c1 = char(x1); char c2 = char(x2); string Hand = {c1, c2}; cout << Hand << endl; intx1,x2; x1=(r

当我尝试使用rand()函数生成整数时,我不能将它们包含在字符串中。以下是我尝试过的两种代码:

 int x1, x2;
    x1 = (rand() % 14 + 2);
    x2 = (rand() % 14 + 2);

    char c1 = char(x1);
    char c2 = char(x2);

    string Hand = {c1, c2};

    cout << Hand << endl;
intx1,x2;
x1=(rand()%14+2);
x2=(rand()%14+2);
char c1=char(x1);
char c2=char(x2);
字符串指针={c1,c2};

coutC++是静态类型化语言<代码>标准::to_字符串
不会更改类型。。。它返回字符串


至于第一个代码,您正在处理不可打印的字符。

C++是静态类型化语言<代码>标准::to_字符串不会更改类型。。。它返回字符串


对于第一个代码,您处理的是不可打印的字符。

请注意,c1和c2不是向量,因此c1[0]没有意义。 to_string方法返回一个字符串,不要将参数转换为一个字符串。也就是说,代码可以像这样更好地工作:

int c1, c2;
 c1 = (rand() % 14 + 2);
 c2 = (rand() % 14 + 2);



    string Hand = {to_string(c2)[0], to_string(c1)[0]};

cout << Hand << endl;
#include <iostream>
#include <stdlib.h>     /* srand, rand */
#include <string>
#include <sstream>
using namespace std;

int main() {
    // your code goes here
    std::ostringstream ss;
    int x1, x2;
    x1 = (rand() % 14 + 2);
    x2 = (rand() % 14 + 2);
    ss << x1 << x2;
    std::string Hand = ss.str();

    cout << Hand << endl;

    return 0;
}
intc1,c2;
c1=(rand()%14+2);
c2=(rand()%14+2);
字符串手={to_string(c2)[0],to_string(c1)[0]};

注意c1和c2不是向量,所以c1[0]没有意义。 to_string方法返回一个字符串,不要将参数转换为一个字符串。也就是说,代码可以像这样更好地工作:

int c1, c2;
 c1 = (rand() % 14 + 2);
 c2 = (rand() % 14 + 2);



    string Hand = {to_string(c2)[0], to_string(c1)[0]};

cout << Hand << endl;
#include <iostream>
#include <stdlib.h>     /* srand, rand */
#include <string>
#include <sstream>
using namespace std;

int main() {
    // your code goes here
    std::ostringstream ss;
    int x1, x2;
    x1 = (rand() % 14 + 2);
    x2 = (rand() % 14 + 2);
    ss << x1 << x2;
    std::string Hand = ss.str();

    cout << Hand << endl;

    return 0;
}
intc1,c2;
c1=(rand()%14+2);
c2=(rand()%14+2);
字符串手={to_string(c2)[0],to_string(c1)[0]};

试试这样的方法:

int c1, c2;
 c1 = (rand() % 14 + 2);
 c2 = (rand() % 14 + 2);



    string Hand = {to_string(c2)[0], to_string(c1)[0]};

cout << Hand << endl;
#include <iostream>
#include <stdlib.h>     /* srand, rand */
#include <string>
#include <sstream>
using namespace std;

int main() {
    // your code goes here
    std::ostringstream ss;
    int x1, x2;
    x1 = (rand() % 14 + 2);
    x2 = (rand() % 14 + 2);
    ss << x1 << x2;
    std::string Hand = ss.str();

    cout << Hand << endl;

    return 0;
}
#包括
#包括/*srand,兰特*/
#包括
#包括
使用名称空间std;
int main(){
//你的密码在这里
std::ostringstream ss;
int-x1,x2;
x1=(rand()%14+2);
x2=(rand()%14+2);

ss试试这样的方法:

int c1, c2;
 c1 = (rand() % 14 + 2);
 c2 = (rand() % 14 + 2);



    string Hand = {to_string(c2)[0], to_string(c1)[0]};

cout << Hand << endl;
#include <iostream>
#include <stdlib.h>     /* srand, rand */
#include <string>
#include <sstream>
using namespace std;

int main() {
    // your code goes here
    std::ostringstream ss;
    int x1, x2;
    x1 = (rand() % 14 + 2);
    x2 = (rand() % 14 + 2);
    ss << x1 << x2;
    std::string Hand = ss.str();

    cout << Hand << endl;

    return 0;
}
#包括
#包括/*srand,兰特*/
#包括
#包括
使用名称空间std;
int main(){
//你的密码在这里
std::ostringstream ss;
int-x1,x2;
x1=(rand()%14+2);
x2=(rand()%14+2);

学生希望从使用
stringhand={c1,c2}中得到什么现在你的问题已经得到了答案,我真的建议读一本书或一个教程。你缺少一些基本的C++原理。从初学者部分挑选一本书,举几个例子,你期望什么样的输出。目标是什么?描述一个小的上下文。一个字符串/文本的两个随机数的数字?长度为2的环,有两个数字,从两个随机数开始,每个数字都是第一个数字?一个两位数的随机数?一个范围为0-99的随机数?请参见希望从使用
string Hand={c1,c2}得到什么现在你的问题已经得到了答案,我真的建议读一本书或一个教程。你缺少一些基本的C++原理。从初学者部分挑选一本书,举几个例子,你期望什么样的输出。目标是什么?描述一个小的上下文。一个字符串/文本的两个随机数的数字?长度为2的环,有两个数字,从两个随机数开始,每一个都是第一个数字?一个两位数的随机数?一个范围为0-99的随机数?谢谢,这很有效。我没有意识到to_string在那个实例中只返回字符串值,没有转换它。谢谢,这很有效。我没有意识到to_string只返回了s该实例中的字符串值,但未将其转换。