[問題] 字串轉整數
以下是一字串轉整數的程式片段
例如將 *p = "12345" 轉成 int q = 12345
但有點小bug似乎是在char轉int 那部分
請給小弟一點提示??
#include<iostream>
#include<cstdlib>
#include<math.h>
using namespace std;
int convert(const char *p)
{
int len = strlen(p);
int output = 0;
for(int i=0;i<len;i++)
output += (*(p+i)) * (pow(10,len-1-i));
return output;
}
int main()
{
char *p = "12345" ;
int intp = convert(p);
cout<< "intp = "<< intp <<endl;
system("pause");
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.240.229.89
※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1415821455.A.44A.html
→
11/13 04:21, , 1F
11/13 04:21, 1F
推
11/13 04:48, , 2F
11/13 04:48, 2F
→
11/13 04:48, , 3F
11/13 04:48, 3F
→
11/13 04:49, , 4F
11/13 04:49, 4F
→
11/13 04:50, , 5F
11/13 04:50, 5F
→
11/13 05:47, , 6F
11/13 05:47, 6F
推
11/13 06:02, , 7F
11/13 06:02, 7F
→
11/13 06:26, , 8F
11/13 06:26, 8F
→
11/13 06:26, , 9F
11/13 06:26, 9F
→
11/13 07:50, , 10F
11/13 07:50, 10F