简体   繁体   English

MFC Visual C ++代码以检查有效的数字条目

[英]MFC Visual C++ code to check for valid number entries

I have this MFC app with the following piece of code to check for the validity of numbers entered using a edit control in the app. 我的MFC应用程序带有以下代码,用于检查使用应用程序中的编辑控件输入的数字的有效性。 Its validated as a string of chars. 经验证为一串字符。

bool invalid = FALSE;
int string_length = InputEdit.GetWindowText(text, 10);
if (text[0] == '-1')
    for(i=1; i<string_length; i++)
        if((text[i] < '0' || text[i] > '9' ) && text[i] != '.'){
            MessageBeep(MB_ICONEXCLAMATION);
            Reactive_Const_Status_Text.SetWindowText("Invalid AA Value");
            invalid = TRUE;
        }
else
    for(i=0; i<string_length; i++)
        if((text[i] < '0' || text[i] > '9' ) && text[i] != '.'){
            MessageBeep(MB_ICONEXCLAMATION);
            Reactive_Const_Status_Text.SetWindowText("Invalid AA Value");
            invalid = TRUE;
        }

if(!invalid){   
    double temp_value = atof(text);
    reac.VelAA  = temp_value;
}

So this edit control allows users to enter negative numbers. 因此,此编辑控件允许用户输入负数。 However can someone please shed some light about what this line "if (text[0] == '-1')" does in the code ? 但是,有人可以说明一下该行“ if(text [0] =='-1')”在代码中的作用吗? Is it checking for negative numbers and if so why does it use '-1' ? 它是否检查负数,如果是,为什么使用'-1'? Secondly, I'm wanting to convert this code to C# and so how can I convert this line ? 其次,我想将此代码转换为C#,那么如何转换此行呢?

Thanks in advance 提前致谢

Actually, this code is also checking for negative numbers. 实际上,此代码还在检查负数。 The first for loop checks from second digit onwards. 第一个for循环从第二个数字开始进行检查。

The line if (text[0] == '-1') is supposed to be if (text[0] == '-') 行if(text [0] =='-1')应该是if(text [0] =='-')

I guessed it this way because in C++ single quotes can be used only for single character and not for a string with more than one character. 我猜是这样,因为在C ++中,单引号只能用于单个字符,而不能用于具有多个字符的字符串。

In C# the same line if (text[0] == '-') will work. 在C#中,如果(text [0] =='-'),则在同一行。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM