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

作者: plowsavior (咕嚕咕咕)   2015-07-30 11:43:38
語言:使用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用錯了!
該如何改善, 謝謝!
作者: Feis (永遠睡不著 @@)   2015-07-30 12:53:00
是這題目應該不需要用到 isdigital要用的話你就 google 一下 isdigit 的用法. 跟你想的差很多
作者: Killercat (殺人貓™)   2015-07-30 16:04:00
這個其實該用regex, 你可以參考一下一些std::regex*
作者: EdisonX (卡卡獸)   2015-07-30 17:47:00
擅用sscanf或fscanf傳回值。
作者: plowsavior (咕嚕咕咕)   2015-07-30 20:47:00
好的,感謝!^^
作者: stupid0319 (徵女友)   2015-07-30 23:17:00
這個用char*來做應該滿快的吧?

Links booklink

Contact Us: admin [ a t ] ucptt.com