可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm having trouble with isdigit. I read the documentation, but when I cout << isdigit(9), I get a 0. Shouldn't I get a 1?
#include <iostream> #include <cctype> #include "Point.h" int main() { std::cout << isdigit(9) << isdigit(1.2) << isdigit('c'); // create <int>i and <double>j Points Point<int> i(5, 4); Point<double> *j = new Point<double> (5.2, 3.3); // display i and j std::cout << "Point i (5, 4): " << i << '\n'; std::cout << "Point j (5.2, 3.3): " << *j << '\n'; // Note: need to use explicit declaration for classes Point<int> k; std::cout << "Enter Point data (e.g. number, enter, number, enter): " << '\n' << "If data is valid for point, will print out new point. If not, will not " << "print out anything."; std::cin >> k; std::cout << k; delete j; }
回答1:
isdigit()
is for testing whether a character is a digit character.
If you called it as isdigit('9')
, it would return nonzero.
In the ASCII character set (which you are likely using), 9 represents the horizontal tab, which is not a digit.
Since you are using the I/O streams for input, you don't need to use isdigit()
to validate the input. The extraction (i.e., the std::cin >> k
) will fail if the data read from the stream is not valid, so if you are expecting to read an int and the user enters "asdf" then the extraction will fail.
If the extraction fails, then the fail bit on the stream will be set. You can test for this and handle the error:
std::cin >> k; if (std::cin) { // extraction succeeded; use the k } else { // extraction failed; do error handling }
Note that the extraction itself also returns the stream, so you can shorten the first two lines to be simply:
if (std::cin >> k)
and the result will be the same.
回答2:
isdigit()
takes an int
which is the representation of the character. Character 9 is (assuming you're using ASCII) the TAB character. Character 0x39 or '9' (not 9) is the actual character representing the digit 9.
The digit characters are integer codes 0x30 through 0x39 (or 48 through 57) in ASCII - I reiterate that since ASCII is not a requirement of the ISO C standard. Hence the following code:
if ((c >= 0x30) && (c <= 0x39))
which I've seen before, is not a good idea for portability since there is at least one implementation that uses EBCDIC under the covers - isdigit
is the best option in all situations.
回答3:
isdigit()
works on characters, not ascii values, which you are currently passing. Try using isdigit('9')
.