1

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

1.2 // in file

但读取一个整数

int i;
in_stream >> i; 
4

3 回答 3

2

在这种情况下,您无能为力。如果您尝试将浮点数读入整数变量,则不会产生任何错误,因为流(在您的示例中使用数字)将 读取1为整数,当然是这样。

可以做的是peek在下一个字符处查看它是否是您所期望的。

于 2013-07-07T15:50:11.677 回答
1

假设输入是一个 C++ 字符串,下面是递归解决方案的开始。我同意 Joachim 和 rasen 的评论;下面的方法更符合 rasen 建议的路线。

这里的关键是 cctype。这使您可以对给定字符是否为数字进行布尔检查。

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

这可能不是您想要的,因此请考虑您希望如何实现逻辑,可能返回指定的特殊字符,遇到非数字时,或类似的东西。然后,您可以搜索此返回值是否存在指定的字符,提供布尔函数的基础,让您知道给定的输入字符串是否可以转换为 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;
}

}

于 2013-07-07T16:45:36.380 回答
0

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

于 2013-07-07T15:57:32.593 回答