使用 C isdigit 进行错误检查

发布于 2024-11-17 19:40:51 字数 761 浏览 3 评论 0原文

在对 int num 使用布尔检查时,此循环不起作用。它后面的行无法识别。输入像 60 这样的整数,它就会关闭。我使用 isdigit 错误吗?

int main()
{
    int num;
    int loop = -1;

    while (loop ==-1)
    {
        cin >> num;
        int ctemp = (num-32) * 5 / 9;
        int ftemp = num*9/5 + 32;
        if (!isdigit(num)) {
            exit(0);  // if user enters decimals or letters program closes
        }

        cout << num << "°F = " << ctemp << "°C" << endl;
        cout << num << "°C = " << ftemp << "°F" << endl;

        if (num == 1) {
            cout << "this is a seperate condition";
        } else {
            continue;  //must not end loop
        }

        loop = -1;
    }
    return 0;
}

While using the boolean check for the int num this loop doesn't work. The lines after it go unrecognized. Enter and integer like 60 and it just closes. Did I use isdigit wrong?

int main()
{
    int num;
    int loop = -1;

    while (loop ==-1)
    {
        cin >> num;
        int ctemp = (num-32) * 5 / 9;
        int ftemp = num*9/5 + 32;
        if (!isdigit(num)) {
            exit(0);  // if user enters decimals or letters program closes
        }

        cout << num << "°F = " << ctemp << "°C" << endl;
        cout << num << "°C = " << ftemp << "°F" << endl;

        if (num == 1) {
            cout << "this is a seperate condition";
        } else {
            continue;  //must not end loop
        }

        loop = -1;
    }
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独患者 2024-11-24 19:40:51

当您调用 isdigit(num) 时,num 必须具有字符的 ASCII 值(0..255 或 EOF)。

如果它被定义为int num那么cin>>> num 将在其中放入数字的整数值,而不是字母的 ASCII 值。

例如:

int num;
char c;
cin >> num; // input is "0"
cin >> c; // input is "0"

isdigit(num) 为 false(因为在 ASCII 的第 0 位不是数字),但 isdigit(c) 为 true(因为在 ASCII 的第 30 位) ASCII 有一个数字“0”)。

When you call isdigit(num), the num must have the ASCII value of a character (0..255 or EOF).

If it's defined as int num then cin >> num will put the integer value of the number in it, not the ASCII value of the letter.

For example:

int num;
char c;
cin >> num; // input is "0"
cin >> c; // input is "0"

then isdigit(num) is false (because at place 0 of ASCII is not a digit), but isdigit(c) is true (because at place 30 of ASCII there's a digit '0').

错爱 2024-11-24 19:40:51

isdigit 仅检查指定字符是否为数字。一个字符,而不是两个字符,也不是一个整数,如 num 的定义所示。您应该完全删除该检查,因为 cin 已经为您处理了验证。

http://www.cplusplus.com/reference/clibrary/cctype/isdigit/

isdigit only checks if the specified character is a digit. One character, not two, and not an integer, as num appears to be defined as. You should remove that check entirely since cin already handles the validation for you.

http://www.cplusplus.com/reference/clibrary/cctype/isdigit/

千纸鹤 2024-11-24 19:40:51

如果您试图保护自己免受无效输入(超出范围、非数字等)的影响,则需要担心几个问题:

// user types "foo" and then "bar" when prompted for input
int num;
std::cin >> num;  // nothing is extracted from cin, because "foo" is not a number
std::string str;
std::cint >> str;  // extracts "foo" -- not "bar", (the previous extraction failed)

更多详细信息请参见此处:
忽略预期之外的用户输入选自

If you're trying to protect yourself from invalid input (outside a range, non-numbers, etc), there are several gotchas to worry about:

// user types "foo" and then "bar" when prompted for input
int num;
std::cin >> num;  // nothing is extracted from cin, because "foo" is not a number
std::string str;
std::cint >> str;  // extracts "foo" -- not "bar", (the previous extraction failed)

More detail here:
Ignore user input outside of what's to be chosen from

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文