如何循环产生字符串并带出循环使用?

netciw 2012-01-07 04:32:01
我想循环产生这样的数据 比如

5+0,5+1,5+2,5+3,5+4,5+5
如何去做呢。下面是我写的代码可是不太会弄了带出来!



#include <string>
#include <iostream>
using namespace std;
void main()
{



string mya("5-");


string temp="";
for(int i =0 ;i<5;i++)
{
char a[10];

string str;
itoa(i, a, 10);
str = a;
temp = mya+a+",";

printf("%s",temp.c_str());


}




}


...全文
327 27 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
开水 2012-01-09
  • 打赏
  • 举报
回复
楼主是不是想这么干啊,看了老半天才明白,代码改成这样:

#include <string>
#include <iostream>
using namespace std;

string g_str = "";

int main()
{
string mya("5-");
string temp="";
for(int i =0 ;i<5;i++)
{
char a[10];

string str;
itoa(i, a, 10);
str = a;
// temp = mya+a+",";
temp = mya + a;
if (i != 4)
temp += ",";
// printf("%s",temp.c_str());
g_str += temp; // 连到全局变量里
// 输出不输出就看你自己想不想这么干了,变量放在g_str里了
cout << g_str << endl;
}
getchar(); // 程序停顿用
return 0;
}

zmkkobe 2012-01-08
  • 打赏
  • 举报
回复
string和字符串有一些差距的,建议都换成字符串再用strcat
cbzjzsb123 2012-01-07
  • 打赏
  • 举报
回复
Kimshuen 2012-01-07
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 netciw 的回复:]

strcat()还是报错啊!郁闷。
[/Quote]
string类型和C语言的字符串不太一样,建议转化下思想
面包大师 2012-01-07
  • 打赏
  • 举报
回复
如果你非得用strcat的话,就用下边的代码吧

char *p = const_cast<char*>(str1.c_str());
const char *pp = "5-5";
strcat(p, pp);
printf("%s",p);
netciw 2012-01-07
  • 打赏
  • 举报
回复
to lile1234_show

不知道如何解决啊
lee_鹿游原 2012-01-07
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 netciw 的回复:]

C/C++ code

#include <string>
#include <iostream>
using namespace std;
void main()
{

string mya("5-");
string temp="";
string str1="";
for(int i =0 ;i<5;i++)
{
ch……
[/Quote]

查strcat 的函数用法.. 我在10L 写了一个例子
netciw 2012-01-07
  • 打赏
  • 举报
回复
'strcat' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<c

说的类型不对?
netciw 2012-01-07
  • 打赏
  • 举报
回复
error C2146: syntax error : missing ';' before identifier 'printf' 这个是我忘记加 ";"号了
netciw 2012-01-07
  • 打赏
  • 举报
回复

#include <string>
#include <iostream>
using namespace std;
void main()
{

string mya("5-");
string temp="";
string str1="";
for(int i =0 ;i<5;i++)
{
char a[10];

itoa(i, a, 10);
temp = mya+a+",";
str1 = str1 + temp;
}
string str2="5-5";
str2=strcat(str1,str2)
printf("%s",str2.c_str());
}




dd.cpp(30) : error C2664: 'strcat' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\Users\yby\Desktop\CCC\ddddddd\dd.cpp(31) : error C2146: syntax error : missing ';' before identifier 'printf'
执行 cl.exe 时出错.

ddddddd.exe - 1 error(s), 0 warning(s)
阿标 2012-01-07
  • 打赏
  • 举报
回复
我现在怎么看不懂程序了!我勒个去!
面包大师 2012-01-07
  • 打赏
  • 举报
回复
string str2="5-5";
str2=strcat(str1,str2);//字符串直接相加就好。。。
面包大师 2012-01-07
  • 打赏
  • 举报
回复
把错误贴下
netciw 2012-01-07
  • 打赏
  • 举报
回复
我已经
char *p=str1.data();
char *pp=str2.data();
netciw 2012-01-07
  • 打赏
  • 举报
回复
strcat()还是报错啊!郁闷。
面包大师 2012-01-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 netciw 的回复:]

嗯 关于","的问题我解决了
[/Quote]
不错,不错。。。呵呵
xiebinghu 2012-01-07
  • 打赏
  • 举报
回复
虽然多了一个逗号,你可以截取最后一个逗号,或者加上flag=true 或者false 去生成逗号。
lee_鹿游原 2012-01-07
  • 打赏
  • 举报
回复

//临时写的
int main()
{
string mya("5+");
string temp="";
char** Src =new char*[20];
for(int nIndex = 0; nIndex < 20; ++nIndex)
{
Src[nIndex] = new char [20];
strcpy(Src[nIndex],"");
}
for(int i =0 ;i<6;++i)
{
char a[10];
string str;
itoa(i, a, 10);
str = a;
temp = mya+a+",";
strcat(Src[i],temp.c_str());
}
for(int i =0 ;i<6;++i)
{
printf("%s\n",Src[i]);
}
delete []Src;
system("pause");
return 0;
}



netciw 2012-01-07
  • 打赏
  • 举报
回复
嗯 关于","的问题我解决了
netciw 2012-01-07
  • 打赏
  • 举报
回复
chaoyue0724前辈,我想用strcat()实现。但是报错了

string mya("5-");
string temp="";
string str1="";
for(int i =0 ;i<5;i++)
{
char a[10];

itoa(i, a, 10);
temp = mya+a+",";
str1 = str1 + temp;
}
string str2="5-5";
str2=strcat(str1,str2);

//str2=str1+str2;
printf("%s",str2.c_str());



error C2664: 'strcat' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
执行 cl.exe 时出错.
加载更多回复(7)

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧