ファイルからデータを読み取るなどの I/O で、ファイル内のデータの型が間違っている場合、どのように確認すればよいですか? 例えば
1.2 // in file
しかし、整数に読み込まれます
int i;
in_stream >> i;
入力が C++ 文字列であると仮定すると、以下は再帰的な解決策の始まりです。Joachim と rasen のコメントに同意します。以下のアプローチは、rasen によって提案された方針に沿っています。
ここに含まれるキーは 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;
}
}