看板 C_and_CPP 關於我們 聯絡資訊
以下是一字串轉整數的程式片段 例如將 *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
carylorrk: 你跟樓上的是好朋友嗎?哪個老師的課啊 XDD 11/13 04:21
penril0326: 樓上在說我嗎.... 11/13 04:48
penril0326: 原po問題是什麼? 11/13 04:48
penril0326: 喔我懂了...你字元沒有轉型 11/13 04:49
penril0326: 當然會有bug 11/13 04:50
ting301: 請問 該如何轉啊 11/13 05:47
johnpage: ASCII 11/13 06:02
penril0326: 我剛剛才發過文,atoi那篇 11/13 06:26
penril0326: 你可以參考看看 11/13 06:26
ting301: 謝些 我會了 11/13 07:50