1

while using std::cin >> number in the following way:

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. If the lines begin with a float I want to read 1000 more floats. If it is an int, I want to read that number of floats. 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.

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. 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.

1
  • Since "number" is declared as a float then it will always be stored as a float, regardless of whether or not the user enters 1.000 or just plain 1. So no, there is no way that your code can possibly distinguish between the two cases. Commented Mar 17, 2013 at 6:12

4 Answers 4

4

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=='.'

Sign up to request clarification or add additional context in comments.

1 Comment

Ah now that's an interesting answer. I didn't think of that!
0

Search for the dot, if they are string, parse them as that.

Comments

0

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.

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;
}

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.