[英]Checking if *char is whitespace or characters C++
我試圖舍棄無效值,即whitespaces
和characters
因為我只想解析雙精度值。
我的.txt
文件看起來像這樣...
1.45-1.46
-1.56
1.78-dead
-abc
1.34-2.45
1.56-9.01
基本上,我只是想存儲有效雙打,所以第一行1.45-1.46
,第五行1.34-2.45
最后一行1.56-9.01
是有效的,其余均為無效。
我已經寫了一些代碼,但是它不能識別whitespace
或characters
。 我正在獲取ISO C++ forbids comparison between pointer and integer
錯誤。
這是代碼
char **marks;
marks = new char*[7]
for(int i=0; i<7; i++)
{
marks[i] = new char[64];
istreams.getline(marks[i], 64);
char *delims;
delims = strtok (marks[i]," -");
while (delims != NULL)
{
if(delims == '') // DOES NOT LIKE THIS, I WANT IT TO CHECK FOR WHITESPACE AND ALPHA CHARACTERS LIKE 'A', 'B', 'C'...
{
cout << "Invalid double" << endl;
}
else
{
cout << atof(delims);
delims = strtok (NULL, " ,.-");
}
}
}
檢查功能
isalpha(int)
isspace(int)
但是,您需要通過將每個字符轉換為int來單獨檢查每個字符:
bool containsWhitespace(char const * c) {
char temp = *c;
if(temp != '\0') {
if( isspace(temp) )
return true;
} else {
return false;
}
return containsWhitespace(++c);
}
int main() {
if(containsWhitespace("ThisIsATest")) printf("First test failed.");
if(!containsWhitespace("This is a test")) printf("Second test failed.");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.