字符串转换为什么printf会出错?程序无法执行

netciw 2012-01-07 03:01:40


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


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

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

printf("%s\r\n",temp);



}


}



...全文
60 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
鲲尘千古 2012-01-07
  • 打赏
  • 举报
回复
不知道你想要的是不是下面这种结果:

#include <string.h>
#include <iostream>
using namespace std;
int main() {
char mya[20] = "5-";
char a[10]={0};
char * str;
for (int i = 0; i < 5; i++) {
sprintf(a, "%d", 10);
strcat(mya,a);
printf("%s\n", mya);
}
}

运行结果:
5-10
5-1010
5-101010
5-10101010
5-1010101010
lee_鹿游原 2012-01-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lile1234_show 的回复:]

C/C++ code

printf("%s\r\n",temp.c_str());
[/Quote]

还有 #include <iostream.h> 有这个头文件么?
面包大师 2012-01-07
  • 打赏
  • 举报
回复
printf("%s\r\n",temp.c_str());
lee_鹿游原 2012-01-07
  • 打赏
  • 举报
回复

printf("%s\r\n",temp.c_str());

64,281

社区成员

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

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