I wrote a little program to read soma data from a *.txt file.
The content of the file is "5 , 10, 15".

The program shall identify the numbers, e.g. "5" and "10" and "15".
This works until the last two numbers. Unfortunately the program doesn't read the last two numbers.

When I set a "," at the end of the file, "5, 10, 15," then it works.

I don't know what the reason for this behaviour is. Maybe someone could help me?

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

using namespace std;

int main()
{
    ifstream filestream("test.txt");
    string s;
    for(char ch;filestream.good();)
    {
        filestream.get(ch);
        if((ch>='0') && (ch<='9'))
        {
            s+=ch;
        }

        else 
        {
          cout<<s<<" ";
          s.clear();
        }
    }

    return 0;
}

To me your code seems ok logically. Just a small guess, are you sure you enter a newline in your text file at the end i.e. after 15. If not just give it a try. I am not sure this will work but can't think of anything else at this moment

No, there is no newline. If I enter a newline at the end of textfile, it works as well as a ",".
But normally reading from a file should also work, when there is no newline or ",", shouldn't it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.