我試圖寫一個函數來檢測分隔字符由分配定義的,我知道這是不好的編程風格鑄字,整數和C++
#define EXCLAMATION_POINT 33, but to instead do #define EXCLAMATION_POINT '!'
這是我的代碼:
#include <iostream>
#include <ostream>
using namespace std;
#define PERIOD '.'
#define QUESTION_MARK '?'
#define EXCLAMATION_POINT '!'
#define COMMA ','
#define COLON ':'
#define SEMICOLON ';'
inline bool IsSeparator(int x)
{
if (isspace(x) ||
x == PERIOD ||
x == QUESTION_MARK ||
x == EXCLAMATION_POINT ||
x == COMMA ||
x == COLON ||
x == SEMICOLON) {
return true;
}
else {
return false;
}
}
int main (int argc, char * const argv[]) {
int input;
cout << "Enter characters: \n";
input = cin.get();
if (!IsSeparator(input))
cout << "true";
else {
cout << "false";
}
return 0;
}
但是在我的IsSeparator()中,如何將int轉換爲與'!'進行比較的字符。我想如果我做了像(char)EXCLAMATION_POINT這樣的工作,但它不會,值是留在一個int。我在這裏做錯了什麼?謝謝!
呃,不知道我是否完全明白你對'#define'和style的看法,但是* best *方法根本就沒有這樣做。只需用''.''替換'PERIOD',等等。如果你真的需要常量,至少要使它們成爲'static const char Period ='。',根本不需要使用'#define',更不用說尖叫了。 :] – GManNickG 2010-01-24 18:48:43
此外,您的代碼爲我工作正常。 – GManNickG 2010-01-24 18:50:51