[問題] 字串判斷是否為亂數

看板C_and_CPP作者 (咕嚕咕咕)時間8年前 (2015/07/30 11:43), 編輯推噓3(304)
留言7則, 5人參與, 最新討論串1/1
語言:使用C++語言 問題:本身有個.txt檔, 需判斷九九乘法表哪行出錯, 並在螢幕上印出 "第XX行出錯字樣" (.txt檔 內容如下) 4x2=8 4x3=12 1667475582 4x5=20 4x6=24 4x7=28 4x8=32 4x9=36 這是我的程式碼: include <iostream> #include <string> #include <fstream> #include <cctype> using namespace std; int main() { ifstream fip("data2.txt"); if(!fip){ cout << "輸入檔案[data2.txt]無法開啟" << '\n'; return -1; } int time = 0; string str; while(getline(fip, str)){ time++; if(str.isdigit()) cout << "第" << time << "行錯誤!\n" << str[0] << "X" << time << " = " << time*str[0]; else {} } fip.close(); return 0; } 想要判斷整行字串是否皆為數字, 若是輸出"第XX行錯誤" 程式編譯不過, 是不是isdigital用錯了! 該如何改善, 謝謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.113.112.32 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1438227825.A.983.html

07/30 12:53, , 1F
07/30 12:53, 1F

07/30 12:54, , 2F
這題目應該不需要用到 isdigital
07/30 12:54, 2F

07/30 12:55, , 3F
要用的話你就 google 一下 isdigit 的用法. 跟你想的差很多
07/30 12:55, 3F

07/30 16:04, , 4F
這個其實該用regex, 你可以參考一下一些std::regex*
07/30 16:04, 4F

07/30 17:47, , 5F
擅用sscanf或fscanf傳回值。
07/30 17:47, 5F

07/30 20:47, , 6F
好的,感謝!^^
07/30 20:47, 6F

07/30 23:17, , 7F
這個用char*來做應該滿快的吧?
07/30 23:17, 7F
文章代碼(AID): #1LkPrnc3 (C_and_CPP)