Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 区分int和double_C++_Int_Double_Invalid Characters - Fatal编程技术网

C++ 区分int和double

C++ 区分int和double,c++,int,double,invalid-characters,C++,Int,Double,Invalid Characters,我一直在寻找这个答案,但似乎没有人知道如何修复这个错误。我希望输入严格为int。如果输入为double,我希望它发送一个错误 int creatLegs = 0; string trash; bool validLegs = true; do { cout << "How many legs should the creature have? "; cin >> creatLegs; if(cin.fail()) { ci

我一直在寻找这个答案,但似乎没有人知道如何修复这个错误。我希望输入严格为int。如果输入为double,我希望它发送一个错误

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=0;
字符串垃圾;
bool-validLegs=true;
做
{
双腿;
if(cin.fail())
{
cin.clear();
cin>>trash;//设置为字符串,因此cin.ignore()忽略整个字符串。
cin.ignore();//只忽略一个字符
validLegs=假;
}
如果(创建腿>0)
{
validLegs=真;
}
如果(!validLegs)
{
cout
int=0;
做
{
cout>createLegs;//正在尝试获取整数
如果(!cin.fail())//如果cin.fail==false,那么我们得到一个int并离开循环
打破

cout输入可以不是整数或浮点数的表示形式

请记住,数字不是它们的表示法:
9
(十进制)、
017
(八进制,a la C)、
0b1001
(二进制,a la Ocaml)、
IX
(罗马符号)、
8+1
(算术表达式)、
neuf
(法语)都是相同数字九的表示法

因此,您必须决定是否接受像
9x
9
(数字后面有几个空格)这样的输入……更一般地说,您必须定义哪些是可接受的输入(以及输入是否在行尾结束,是否应接受空格或标点符号等)

您可以读取整行(例如,使用)并使用(例如,
%n
控制格式很有用,由
sscanf
返回的项目计数也很有用)或(使用结束指针)对其进行解析


注意,你的问题的措辞(“区分一个int和一个double”)是错误的。C++中没有一个“<代码> int <代码>或<代码>双<代码> >类型(但是 int >代码>是标量类型, double < /C>是C++中的标量类型,并且可以定义一个<代码>类< /代码>,以保存它们中的任何一个。.AFAIU,如果您声明

int x;
,然后使用
std::cin>>x;
,用户输入
12.64
点和数字
64
,之后它将不会被解析,
x
将变为12。

我认为您应该将数据读取为字符串,然后逐字符检查它以验证它是否为整数-如果每个字符ar是一个数字,然后我们有整数,我们可以解析它

streams的问题是,如果您试图读取整数,但传递了十进制数,那么它将读取到小数点。而这部分是一个正确的整数,因此
cin.fail()
返回
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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
int=0;
bool-validLegs=true;
做
{

cout这个问题已经有了一个公认的答案,但是我将提供一个解决方案,处理所有整数,甚至是表示为浮点数(没有小数部分)的整数,并拒绝包含除数字后面的空格以外的任何输入

可接受值的示例,均表示数字4:

拒绝值的示例:


很抱歉,我不太清楚你的意思。不,它不会将其存储为int。它不知道如果我键入的不是int的内容该怎么办。它会吐出垃圾邮件,直到我添加了“if(cin.fail())”难道这不会拒绝
1.23
,因为当我输入一封信时,它会使我的程序崩溃。我现在设置它的方式是,在输入一封信时它会发送一个错误。我现在只想坚持使用ints,这样我就可以理解它是如何工作的。我最好包括所有不同的表示数字的方式,但因为这只是一个简单的程序,我不想深入。谢谢。我不确定你在最后部分所说的。我是C++的新手,如果你能稍微为我倒下来,我会很感激。请按照我给你的链接。我相信你会自己编代码。而且理解文档是一种VE。我是软件开发的重要组成部分,所以你需要学习如何阅读文档。玩得开心!你可以;)我试过投票,但它说我的声誉不够高,所以我认为我也不能接受答案。我的错,我必须习惯这个网站。谢谢你!
#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.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;
}
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;
}