1

The following code is meant to take all floating numbers for calculation from the file "float.txt" The problem is the floating numbers has junk information between it.

example file:

23.5 aujsaN8.2<:::32

After the first floating point is gotten, the while loop never ends, the program no longer gets any information from the file. please help.

int main()
{
    float num;
    ifstream input("float.txt");

    input >> num;
    while (!(input.eof()))
    {
        input >> num;
    }
    input.close();

    return 0;
}
0x499602D2
  • 87,005
  • 36
  • 149
  • 233

3 Answers3

2

You could do something like this:

decltype(input)::Traits::int_type c;
while ((c = input.peek()) != decltype(input)::Traits::eof())
{
    if (std::isdigit(c))
    {
        input >> num;
        ... use num ...
    }
    else
        input.get();
}

The idea here is to peek for the next character, and if it's a digit then we know a >> streaming to double will succeed, otherwise we actually get() the character to remove it from the input stream.

It gets trickier if you need to extract negative numbers (hint: one way - use a bool to track if the last value of c seen was -, then have if (the_bool) num = -num; before using num). While the code above handles e.g. X0.23X -> 0.23, you may need or may not need to also handle X.23X - if so, check for . then see if the next character is a digit... the tricky thing is that peeking for the digit means you've already consumed the ., so it won't be there for input >> num... you can try input.putback('.') but I'm not certain it's Standard-required to work when you've consumed a character then peeked - you'd have to check....

Tony Delroy
  • 94,554
  • 11
  • 158
  • 229
  • im a newbie to c++ ... can u dumb that down for me? – Machination Nov 07 '14 at 01:13
  • @Machination: which bits? basically... mentally simplify `decltype(input)::Traits::int_type` to `int` and `decltype(input)::Traits::eof()` is likely `-1` - the traditional value for an `int` indicating an EOF condition. As for `peek` and `get` - say you have input of "A123Z" - initially `peek()` would return 'A', but if you `get()` then the next peek will see `1`, and `std::isdigit(c)` will become `true`, then `input >> num` removes `"123"` from the stream and puts `123` into `num`, then the next `peek()` and `get()` see `Z`, then the final `peek()` reports `eof`.... – Tony Delroy Nov 07 '14 at 03:35
0

Look i don't know how the statement input>>num; works i never used those, instead i will do these in order to extract floats from your specified file.

float floats=0;
char string[100], ch;
while (file.get(ch)!=' ')
{
string[i]=ch;
i++;
}
string[i]='\0';
floats=atof(string);

This program simply copies the characters untill a ' ' (Space) is found, like the file u shown, then the function aotf() converts the string to floating point number. Is this the answer to your question??, if yes then please vote +1, and if any question u can ask me, i will sure help u...

Rishabh
  • 54
  • 1
  • 10
-1

this works

//  g++ -o parse_float_file  parse_float_file.cpp  -std=c++11

#include <iostream>
#include <fstream>
#include <string> 

int main() {

    float curr_number;
    std::ifstream inFile("float.txt");
    std::string line;

    while(getline(inFile, line)) {

        try {

            curr_number = std::stof(line);

            std::cout << "-->" << curr_number << "<--"  << std::endl;

        } catch (const std::exception &e) {

            std::cout << "ERROR - not a float : " << line << std::endl;
        }
    }

    return 0;
}
Scott Stensland
  • 22,853
  • 10
  • 81
  • 88