问题描述
- 这程序输出不是我想要的。我希望在每个随机数中间插入一个换行符
-
#include
#include
#includeusing namespace std;
int main()
{
int num=10;
string str,longstr="";
stringstream ss;
int *list=new int[num];
std::cout<<"Hello world!"<<std::endl;
srand((unsigned int)time(0));
for(int i=0;i<num;i++){
list[i]=rand();
ss<<list[i];
str=ss.str();
longstr=longstr+str+'n';
}
std::cout<<longstr;
return 0;
}输出是:
Hello world!
11391
1139120221
11391202218590
1139120221859029226
11391202218590292261048
1139120221859029226104827899
11391202218590292261048278996273
1139120221859029226104827899627323760
11391202218590292261048278996273237603823
1139120221859029226104827899627323760382316476
请按任意键继续. . .我希望的输出是:
11391
20221
8590
29226
解决方案
longstr=longstr+str+'n';
上面这句改为longstr = str+'n';试试