[問題] 字串轉整數

看板C_and_CPP作者 ( )時間9年前 (2014/11/13 03:44), 編輯推噓2(208)
留言10則, 4人參與, 最新討論串1/1
以下是一字串轉整數的程式片段 例如將 *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
你跟樓上的是好朋友嗎?哪個老師的課啊 XDD
11/13 04:21, 1F

11/13 04:48, , 2F
樓上在說我嗎....
11/13 04:48, 2F

11/13 04:48, , 3F
原po問題是什麼?
11/13 04:48, 3F

11/13 04:49, , 4F
喔我懂了...你字元沒有轉型
11/13 04:49, 4F

11/13 04:50, , 5F
當然會有bug
11/13 04:50, 5F

11/13 05:47, , 6F
請問 該如何轉啊
11/13 05:47, 6F

11/13 06:02, , 7F
ASCII
11/13 06:02, 7F

11/13 06:26, , 8F
我剛剛才發過文,atoi那篇
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
文章代碼(AID): #1KOxYFHA (C_and_CPP)