使用C isdigit进行错误检查 [英] Using C isdigit for error checking

查看:77
本文介绍了使用C isdigit进行错误检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对布尔值进行布尔检查时,此循环不起作用.其后的线无法识别.输入和整数,如60,它只是关闭.我是否使用isdigit错误?

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;
}

推荐答案

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

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

如果将其定义为int num,则cin >> num将在其中放置数字的整数值,而不是字母的ASCII值.

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.

例如:

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

然后isdigit(num)为假(因为ASCII的第0位不是数字),但isdigit(c)为真(因为ASCII的第30位有一个数字'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').

这篇关于使用C isdigit进行错误检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆