作者plowsavior (咕嚕咕咕)
看板C_and_CPP
標題[問題] 字串判斷是否為亂數
時間Thu Jul 30 11:43:38 2015
語言:使用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
→ Feis: 是 07/30 12:53
→ Feis: 這題目應該不需要用到 isdigital 07/30 12:54
→ Feis: 要用的話你就 google 一下 isdigit 的用法. 跟你想的差很多 07/30 12:55
推 Killercat: 這個其實該用regex, 你可以參考一下一些std::regex* 07/30 16:04
推 EdisonX: 擅用sscanf或fscanf傳回值。 07/30 17:47
→ plowsavior: 好的,感謝!^^ 07/30 20:47
推 stupid0319: 這個用char*來做應該滿快的吧? 07/30 23:17