# Thread: Ignoring negative numbers and moving on

1. ## Ignoring negative numbers and moving on

I am trying to read from a file of integers, when the program encouters -1 then it is to skip over it and continue reading more integers that follow it. I was thinking about using fin.ignore, but anything I try with it doesn't seem to be working. Does anybody have any suggestions or resources I could use?

2. Continue might be a good word to use. (I'm assuming that the only thing you're supposed to skip is the -1, which you've already read in, so there's nothing to ignore.)

3. ## clarification

I'm not sure you understood what I was saying, let me give an example.

You have some input like this:

1 2 3 4 5 6 -1 7 8 9 10

When you reach (-1) you should print out the numbers up untill then, then move on to the next set of numbers.

The problem I'm having is that I can read in and print the first set of number, but when I try to move on the next output includes the (-1) and doesn't move on.

What I'm working on specifically is loading these numbers into a tree. So the first set of numbers goes into tree1, you hit the -1, skip it, and put the next set of numbers in tree2. The specific problem I'm having is that it is not skipping -1, it's loading it into tree2 and then stopping there.

Here is the code I am working on that I am facing this problem with:

Code:
```while (fin>>num)
{

if(num>=1) //If the number is greater than or equal to one (meaning not -1)
{
Tree[1].insert(num); //Insert that number into tree 1
compareInsert ++;
}
else
{
/*if (fin.peek() == '-' && fin.peek() == 1)
{
fin.ignore(1,-1);
}*/

for (int i=2; i < 10; i++) //Starting at tree 2
{
Tree[i].insert(num); //Insert next set of numbers
}
}

}```
As you may see, I tried using the peek and ignore function but the way I had it was not working, I also tried it several different ways, but had no luck.

Any suggestions on how to fix this?

4. So, is 7 8 9 10 considered "the next set of numbers"? Or do we mean, skip the rest of the line?

5. have you tried something like
Code:
```{
while ( fin >> num )
if ( num < 0 )
{
print_numbers();//whatever
fin >> num;
//start over again
}
}```
edit: it is incomplete on purpose. so please dont call me stupid.

6. On the assumption that 7 8 9 10 is the next set of numbers, all you have to do is
Code:
```current_tree_number = 0;  //I don't know why you start at 1 and leave the first tree blank
while (fin >> num) {
if (num == -1) {
current_tree_number++;
//maybe print it out if you want
//note the complete absence of ignoring things, since the -1 is already gone
}
else {
Tree[current_tree_number].insert(num);
compareInsert++; //whatever this does
}
}```

7. ## Thanks

Thanks tabstop, that did the trick. I was trying to do things the hard way and it turned out so simple!

Popular pages Recent additions