Re: [問題]如何把char轉成int

看板C_and_CPP (C/C++)作者 (acer)時間16年前 (2009/07/10 23:47), 編輯推噓1(102)
留言3則, 3人參與, 最新討論串2/6 (看更多)
※ 引述《hyestt (小傑)》之銘言: : 請問一下 : char ch[333],ch2[333]; : cin>>ch; : cin>>ch2; : 我所輸入的都是數字 : 那要怎麼使ch+ch2 : 我轉換過了static_cast<int>(ch)+static_cast<int>(ch2) : 不過沒辦法 #include <string> #include <sstream> #include <iostream> int main() { using namespace std; char input[10]; // using std::string is recommanded int output; input[9] = 0; cin.read(input, 9); stringstream convert; convert<<string(input); // 注意這個 string 建構式會用 '\0'來找結尾 // 如果 input[9] 不一定是 '\0' // 就要用別的建構式 convert>>output; // 如果要繼續使用 convert 要記得呼叫 convert.clear() cin.read(input, 9); convert.clear(); convert<<string(input); // etc... return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.244.140.23 ※ 編輯: adxis 來自: 60.244.140.23 (07/11 00:06)

07/11 07:18, , 1F
用atoi()
07/11 07:18, 1F

07/12 22:35, , 2F
請問原po~我可以把你的code複製一份代回家慢慢享用嗎
07/12 22:35, 2F

07/13 02:00, , 3F
隨便用~
07/13 02:00, 3F
文章代碼(AID): #1ALsALYI (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1ALsALYI (C_and_CPP)