skipping a line of text when using getline() function with a textfile

Hey guys,

I'm using getline() in a loop to get lines of text from a text file and do stuff with it. Now I need to make a function within this loop to skip lines of text with characters not equal to digits. I've tried some functions but they're not successfully skipping lines.

First line of text is
1 1 y
second line is
2 2 8;

Right now I'm using
string a[3];
string temp;
int r, c, rows1, columns1;
double val;
double matrix1[100][100];

while(!textfile1.eof())
{
while(getline(textfile1, temp))
{
if(temp.find_first_not_of("0123456789"))
{
continue;
}
else
{
break;
}
}

stringstream str(temp);
while(str >> a[i])
{
i++;
}
r = atoi(a[0].c_str());
c = atoi(a[1].c_str());
val = atof(a[2].c_str());
matrix1[r][c] = val;
if(r>=rows1) rows1=r;
if(c>=columns1) columns1=c;
i=0;
}
textfile1.close();
cout << matrix1[1][1] << endl;
when I run the program matrix1[1][1] equals some weird long number.

If I turn the while(getline(textfile1, temp)) loop into just getline(textfile1,temp), the program will straight up successfully store values from a textfile with only digits.

Does anyone know how I can skip these unwanted lines without it making me store it?

Thanks, jktexas1
I have not understood what you want to do but I would like point out that the following statement

if(temp.find_first_not_of("0123456789"))

is incorrect.

You shall write either as

if ( temp.find_first_not_of("0123456789") != std::string::npos )

or as

if ( temp.find_first_not_of("0123456789") == std::string::npos )

depending of your intention.
Last edited on
Hey I actually tried the first function, but I never saw the second function anywhere. BTW the second function just made my program work perfectly.
Basically I'm grabbing lines of text that have either numbers or numbers and non-numbers. The function I built grabs a line with characters other than numbers and then skips to the next line of the textfile. I don't want to parse any thing from any lines containing characters besides numbers is basically what I needed to solve.

Anyway the programs working as intended now.

Thank you
Topic archived. No new replies allowed.