如何检查输入是否有效?

时间:2013-07-07 15:46:46

标签: c++ io

在I / O中,例如从文件读取数据,如果文件中的数据类型错误,如何检查?例如

1.2 // in file

但读取整数

int i;
in_stream >> i; 

3 个答案:

答案 0 :(得分:2)

在这种情况下,你可以做的并不多。如果您尝试将浮点数读入整数变量,则不会产生任何错误,因为流(示例中的数字)将1读为整数,当然是。< / p>

答案 1 :(得分:1)

假设输入是C ++字符串,下面是递归解决方案的开始,以帮助您前进。我同意约阿希姆和拉森的评论;下面的方法更像是拉森提出的方法。

这里的关键是cctype。这样就可以检查给定字符是否为数字。

如上所述,如果遇到非数字,代码将返回NULL值,该值在返回的数字中显示为零;您需要根据需要修改此实现。例如,请注意“23.1”返回2301,其中“。”被零替换。

这可能不是你想要的,所以想想你如何实现逻辑,可能会在遇到非数字时返回指定的特殊字符,或类似的东西。然后,您可以在此返回值中搜索指定char的存在,提供布尔函数的基础,该函数将告知您给定的输入字符串是否可以转换为int数据类型。

main.cpp(下面列出)的输出是:

Here is the integer: Invalid character, entry must be a number: 
2301
Here is the integer: 22
Here is the integer: 0
Here is the integer: 1
Here is the integer: 32

以下代码:

//  main.cpp
//  Created by bruce3141 on 7/7/13.

/* Numeric Conversion (string to int)
 * ----------------------
 * Demonstrates a recursive implementation of converting a string into 
 * its representation as an int. Provides feedback to the user on invalid 
 * entries, using isdigit() from the <cctype> import, where a invalid 
 * character (a non-digit) is encountered.
 */

 #include <iostream>
 #include <string>
 #include <cctype>
 using namespace std;


 /* Function prototype */
 int stringToInt(string str);


// Main.cpp tests a few cases below:
int main() {
   int n = 5;
   string strNumbers[5] = {"23.1", "22", "-0", "+1", "32"};
   for (int i = 0; i < n; i++) {
      cout << "Here is the integer: "<< stringToInt(strNumbers[i]) <<endl;
   }
   return 0;
}



/* Convert from string -> int.  The code is longer because of the possibility
* that we might have a '-' or '+' preceding the integer input, and then of
* course multiple digits in combination with the '-' or '+' signs: 
*/
int stringToInt(string str) {

/* Get the number of characters in the string: */
int nS = str.length();

/* Base Case #1: a single positive integer as input: */
if (nS == 1) {
    /* This basic version provides a liitle feedback on
     * invalid entries, using isdigit() from the <cctype>
     import: */
    if (!isdigit(str[0])) {
        cout << "Invalid character, entry must be a number: "<<endl;
        return NULL;
    } else  {
        /* We have to subtract the ASCII code for the character '0' so
         that the string displays as a number in the proper range: */
        return str[0]-'0';
    }

    /* Base Case #2: a single negative integer as input, here
     * we deal with the possibility that a '-' precedes a number: */
} else if (nS == 2 && str.substr(0,1) == "-") {
    /* Below, subtract the ASCII code for the character '0' then
     * multiply by (-1) since the number is negative: */
    return (str[1] - '0')*(-1);

    /* Base Case #3: a single postive integer as input, as indicated
     * by a '+' character: */
} else if (nS == 2 && str.substr(0,1) == "+") {
    /* Below, subtract the ASCII code for the character '0': */
    return (str[1] - '0');

    /* Below is the recursive step for negative numbers with more
     * than one digit: */
} else if (nS >= 2 && str.substr(0,1) == "-") {
    int n1 = stringToInt(str.substr(0,nS-1))*10;
    int n2 = stringToInt(str.substr(nS-1,nS));
    return n1 - n2;

    /* Below is the recursive step for positive numbers with a
     * preceding '+' character and with more than one digit: */
} else if (nS >= 2 && str.substr(0,1) == "+") {
    int n1 = stringToInt(str.substr(0,nS-1))*10;
    int n2 = stringToInt(str.substr(nS-1,nS));
    return n1 + n2;
}

/* Below is the recursive step for positive numbers with more
 * than one digit, but with no preceding '+' character: */
else {
    int n1 = stringToInt(str.substr(0,nS-1))*10;
    int n2 = stringToInt(str.substr(nS-1,nS));
    return n1 + n2;
}

}

答案 2 :(得分:0)

您可以在字符串中输入数据,然后分析它是否格式正确。如果是,则将其转换为您想要的类型。