简体   繁体   English

cin:检查number是一个整数还是float,即使float值是1.000

[英]cin: check if number is an integer or float even if float value is 1.000

while using std::cin >> number in the following way: 在以下方式使用std :: cin >> number时:

float number;
while(std::cin >> number) {
     //perform a check here if possible

    //if number does not contain a decimal point do this
    for(int i = 0; i < number; i++) {
        std::cin >> readMoreFloats; 
    }
    //otherwise read 1000 more floats
}

is there a way to be able to distinguish between the following types of lines: 有没有办法能够区分以下类型的行:

1.500 (1000 more floats)
2 2.000 2.000

The lines will either begin with a float or an int. 这些行将以float或int开头。 If the lines begin with a float I want to read 1000 more floats. 如果行以float开头,我想读1000多个浮点数。 If it is an int, I want to read that number of floats. 如果它是一个int,我想读取那个浮点数。 I know I could cast the float to an int and check if the casted int is equal to the float, but that would not work when a float has a flat value such as 1.000. 我知道我可以将float转换为int并检查casted int是否等于float,但是当float具有平坦值(例如1.000)时,这将不起作用。

I'd prefer to not read the entire line using getline() because splitting it afterwords is time consuming, and I already know that all of the input are floats except for possibly the first number. 我更喜欢不使用getline()来读取整行,因为在后面分割它是非常耗时的,而且我已经知道所有的输入都是浮点数,除了可能是第一个数字。 Another way would be to read in the first value of each line as a string and check to see if it contains a period. 另一种方法是将每行的第一个值作为字符串读入,并检查它是否包含句点。 Is there a way, without first reading the input as a string, to check for this? 有没有一种方法,没有先读取输入作为字符串,检查这个? Or is the string step required. 或者是需要的字符串步骤。

Do you really need to read it as a string to check if there is a dot? 你真的需要把它读作一个字符串来检查是否有一个点吗?

Why not take an integer first? 为什么不先取整数?

int intNum = 0; char ch;

cin >> intNum;
cin.get(ch);     

Now you could use another integer read & combine to a float if there ia dot ie ch=='.' 现在你可以使用另一个整数读取并组合到一个浮点数,如果有一个点,即ch=='.'

搜索点,如果它们是字符串,则将其解析为。

One way to to this is to read the first field in as a string and then test to see if it contains the decimal place. 一种方法是以字符串形式读取第一个字段,然后测试它是否包含小数位。 If so, then cast it to a float and read more. 如果是这样,那么将其投入浮动并阅读更多内容。 Otherwise, cast it to an integer and read that many. 否则,将其转换为整数并读取那么多。 This works fine because cin automatically breaks at spaces for strings, too. 这很好用,因为cin也会自动在字符串的空格处断开。

I think the following code should about work: 我认为以下代码应该是关于工作的:

#include <iostream>
#include <string>
int main()
{

    std::string myStr;
    std::cin >> myStr;

    float myVal = 0;

    if(myStr.find('.') != std::string::npos)
    {
        std::cout << "Is floating point, now convert this and read lots.\n";
        float myFloat = atof(myStr.c_str());
        for(int k =0; k < 1000; k++)
        {
            std::cin >> myVal;
        }
    }
    else
    {
        std::cout << "Is integer, so read just that many\n";
        int myInt = atoi(myStr.c_str());

        for(int k =0; k < myInt; k++)
        {
            std::cin >> myVal;
        }

    }

    return 0;
}

Assuming I understand the question correctly, perhaps you need something like this: 假设我正确理解了这个问题,也许你需要这样的东西:

using namespace std; // I'm lazy

string temp;
int number;
while(std::cin >> temp)
{
    number = 0;
    if (temp.find(".") != string::npos) number = 1000; // string has decimal point
    else istringstream(temp) >> number; // no decimal point, probably an integer

    for(int i = 0; i < number; i++) {
        std::cin >> readMoreFloats; 
    }
}

We use the rudimentary method of checking if the string has a decimal point to indicate whether it's a float or not. 我们使用基本的方法来检查字符串是否有一个小数点来表明它是否是浮点数。 Assuming your input file is always properly formatted, and always omits the decimal point for integers (even though, by some interpretations, values like "2." could be valid), this should work as you expect. 假设您的输入文件始终格式正确,并且总是省略整数的小数点(即使通过某些解释,像“2.”这样的值可能有效),这应该按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM