int 和 double 的区别
Distinguishing between an int and a double
我搜索过这个答案,但似乎没有人知道如何解决这个错误。我希望输入严格为 int。如果输入是双精度,我希望它发送一个错误。
int creatLegs = 0;
string trash;
bool validLegs = true;
do
{
cout << "How many legs should the creature have? ";
cin >> creatLegs;
if(cin.fail())
{
cin.clear();
cin >> trash; //sets to string, so that cin.ignore() ignores the whole string.
cin.ignore(); //only ignores one character
validLegs = false;
}
if (creatLegs > 0)
{
validLegs = true;
}
if (!validLegs)
{
cout << "Invalid value, try again.\n";
}
} while (!validLegs);
好像差不多可以了。它发送错误,但仅在进入下一个循环之后。我怎样才能解决这个问题?为什么它仍然显示错误消息但在显示之前仍在继续?
int creatLegs = 0;
do
{
cout << "How many legs should the creature have? ";
cin >> creatLegs; // trying to get integer
if(!cin.fail()) // if cin.fail == false, then we got an int and leave loop
break;
cout << "Invalid value, try again.\n"; // else show err msg and try once more
cin.clear();
} while (1);
输入可以是整数或浮点数的表示以外的东西。
记住数字不是它们的表示:9
(十进制),017
(八进制,à la C),0b1001
(二进制,à la Ocaml) 、IX
(罗马记法)、8+1
(算术表达式)、neuf
(法语)都是同一个数字九的表示法。
所以你必须决定是否接受像 9 x
或 9
</code> 这样的输入(数字后有几个空格),...更一般地说,您必须定义可接受的输入(以及输入是否在行尾结束,是否应接受空格或标点符号等)。</p>
<p>您可以阅读整行(例如使用 <a href="http://en.cppreference.com/w/cpp/string/basic_string/getline" rel="nofollow">std::getline</a>) and use e.g. <a href="http://en.cppreference.com/w/cpp/io/c/fscanf" rel="nofollow"><code>sscanf
(where the %n
control format is useful, and so is the item count returned by sscanf
) or std::stol(您使用结束指针的地方)来解析它
另请注意,您问题的措辞 ("Distinguishing between an int and a double") 是错误的。 C++ 中没有单一的“int
或 double
”类型(但 int
是标量类型,而 double
是 C++ 中的标量类型,您可以定义一个class
用 tagged union 来容纳其中任何一个)。 AFAIU,如果你声明 int x;
然后使用 std::cin >> x;
用户输入 12.64
点和数字 64
之后它不会被解析并且 x
会变成 12.
我认为您应该将数据读取为字符串,然后逐个字符地检查它以验证它是否为整数 - 如果每个字符都是数字,那么我们就有了整数,我们可以解析它。
流的问题是,如果您尝试读取整数但传递了小数,它会读取数字直到点。而这部分是一个真整数,所以cin.fail()
returns false
.
示例代码:
#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>
using namespace std;
int main() {
int creatLegs = 0;
bool validLegs = true;
do
{
cout << "How many legs should the creature have? ";
string input;
getline(cin, input);
validLegs = true;
for (string::const_iterator i = input.begin(); validLegs && i != input.end(); ++i) {
if (!isdigit(*i)) {
validLegs = false;
}
}
if (!validLegs)
{
cout << "Invalid value, try again.\n";
} else {
creatLegs = atoi(input.c_str());
}
} while (!validLegs);
cout << creatLegs << endl;
}
这当然不是一个完美的解决方案。如果有任何前导或尾随空格(或任何其他字符,如 +
或 -
),程序将失败。但如果需要,您始终可以添加一些代码来处理这些情况。
这个问题已经有了一个可接受的答案,但是我将提供一个解决方案来处理所有整数,甚至是那些表示为浮点数(没有小数部分)的数字,并拒绝包含任何内容的输入除了数字后面的空格。
接受值的例子,这些都代表数字 4:
4
4.
4.0
+4
004.0
400e-2
拒绝值的示例:
3.999999
4.000001
40e-1x
4,
#include <iostream>
#include <sstream>
#include <cctype>
#include <string>
using namespace std;
bool get_int( const string & input, int & i ) {
stringstream ss(input);
double d;
bool isValid = ss >> d;
if (isValid) {
char c;
while( isValid && ss >> c ) isValid = isspace(c);
if (isValid) {
i = static_cast<int>(d);
isValid = (d == static_cast<double>(i));
}
}
return isValid;
}
int main( int argc, char *argv[] )
{
int creatLegs = 0;
bool validLegs = false;
do
{
string line;
do {
cout << "How many legs should the creature have? ";
} while (not getline (cin,line));
validLegs = get_int( line, creatLegs );
if (creatLegs <= 0)
{
validLegs = false;
}
if (not validLegs)
{
cout << "Invalid value, try again." << endl;
}
} while (not validLegs);
cout << "Got legs! (" << creatLegs << ")" << endl;
return 0;
}
如果你想要严格的整数(没有小数点和科学计数法)那么使用这个更简单的 get_int 函数:
bool get_int( const string & input, int & i ) {
stringstream ss(input);
bool isValid = ss >> i;
if (isValid) {
char c;
while(isValid && ss >> c) isValid = isspace(c);
}
return isValid;
}
我搜索过这个答案,但似乎没有人知道如何解决这个错误。我希望输入严格为 int。如果输入是双精度,我希望它发送一个错误。
int creatLegs = 0;
string trash;
bool validLegs = true;
do
{
cout << "How many legs should the creature have? ";
cin >> creatLegs;
if(cin.fail())
{
cin.clear();
cin >> trash; //sets to string, so that cin.ignore() ignores the whole string.
cin.ignore(); //only ignores one character
validLegs = false;
}
if (creatLegs > 0)
{
validLegs = true;
}
if (!validLegs)
{
cout << "Invalid value, try again.\n";
}
} while (!validLegs);
好像差不多可以了。它发送错误,但仅在进入下一个循环之后。我怎样才能解决这个问题?为什么它仍然显示错误消息但在显示之前仍在继续?
int creatLegs = 0;
do
{
cout << "How many legs should the creature have? ";
cin >> creatLegs; // trying to get integer
if(!cin.fail()) // if cin.fail == false, then we got an int and leave loop
break;
cout << "Invalid value, try again.\n"; // else show err msg and try once more
cin.clear();
} while (1);
输入可以是整数或浮点数的表示以外的东西。
记住数字不是它们的表示:9
(十进制),017
(八进制,à la C),0b1001
(二进制,à la Ocaml) 、IX
(罗马记法)、8+1
(算术表达式)、neuf
(法语)都是同一个数字九的表示法。
所以你必须决定是否接受像 9 x
或 9
</code> 这样的输入(数字后有几个空格),...更一般地说,您必须定义可接受的输入(以及输入是否在行尾结束,是否应接受空格或标点符号等)。</p>
<p>您可以阅读整行(例如使用 <a href="http://en.cppreference.com/w/cpp/string/basic_string/getline" rel="nofollow">std::getline</a>) and use e.g. <a href="http://en.cppreference.com/w/cpp/io/c/fscanf" rel="nofollow"><code>sscanf
(where the %n
control format is useful, and so is the item count returned by sscanf
) or std::stol(您使用结束指针的地方)来解析它
另请注意,您问题的措辞 ("Distinguishing between an int and a double") 是错误的。 C++ 中没有单一的“int
或 double
”类型(但 int
是标量类型,而 double
是 C++ 中的标量类型,您可以定义一个class
用 tagged union 来容纳其中任何一个)。 AFAIU,如果你声明 int x;
然后使用 std::cin >> x;
用户输入 12.64
点和数字 64
之后它不会被解析并且 x
会变成 12.
我认为您应该将数据读取为字符串,然后逐个字符地检查它以验证它是否为整数 - 如果每个字符都是数字,那么我们就有了整数,我们可以解析它。
流的问题是,如果您尝试读取整数但传递了小数,它会读取数字直到点。而这部分是一个真整数,所以cin.fail()
returns false
.
示例代码:
#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>
using namespace std;
int main() {
int creatLegs = 0;
bool validLegs = true;
do
{
cout << "How many legs should the creature have? ";
string input;
getline(cin, input);
validLegs = true;
for (string::const_iterator i = input.begin(); validLegs && i != input.end(); ++i) {
if (!isdigit(*i)) {
validLegs = false;
}
}
if (!validLegs)
{
cout << "Invalid value, try again.\n";
} else {
creatLegs = atoi(input.c_str());
}
} while (!validLegs);
cout << creatLegs << endl;
}
这当然不是一个完美的解决方案。如果有任何前导或尾随空格(或任何其他字符,如 +
或 -
),程序将失败。但如果需要,您始终可以添加一些代码来处理这些情况。
这个问题已经有了一个可接受的答案,但是我将提供一个解决方案来处理所有整数,甚至是那些表示为浮点数(没有小数部分)的数字,并拒绝包含任何内容的输入除了数字后面的空格。
接受值的例子,这些都代表数字 4:
4 4. 4.0 +4 004.0 400e-2
拒绝值的示例:
3.999999 4.000001 40e-1x 4,
#include <iostream>
#include <sstream>
#include <cctype>
#include <string>
using namespace std;
bool get_int( const string & input, int & i ) {
stringstream ss(input);
double d;
bool isValid = ss >> d;
if (isValid) {
char c;
while( isValid && ss >> c ) isValid = isspace(c);
if (isValid) {
i = static_cast<int>(d);
isValid = (d == static_cast<double>(i));
}
}
return isValid;
}
int main( int argc, char *argv[] )
{
int creatLegs = 0;
bool validLegs = false;
do
{
string line;
do {
cout << "How many legs should the creature have? ";
} while (not getline (cin,line));
validLegs = get_int( line, creatLegs );
if (creatLegs <= 0)
{
validLegs = false;
}
if (not validLegs)
{
cout << "Invalid value, try again." << endl;
}
} while (not validLegs);
cout << "Got legs! (" << creatLegs << ")" << endl;
return 0;
}
如果你想要严格的整数(没有小数点和科学计数法)那么使用这个更简单的 get_int 函数:
bool get_int( const string & input, int & i ) {
stringstream ss(input);
bool isValid = ss >> i;
if (isValid) {
char c;
while(isValid && ss >> c) isValid = isspace(c);
}
return isValid;
}