I'm trying to make a loop that that will skip a number but has a certain limit to how high it goes...

For example:

I have a for loop:

for (i = 0; i < count;  i++ )
{
    stuff
}

but lets say I want to skip a number in that loop.

Like if count was 10. The loop will display all things from 0 to 10. What if I want it to skip like when i =5?

Re: loop that skips a number 80 80

Use the continue keyword. It skips to the next iteration.

for (i = 0; i < count;  i++ )
{
        if ( i == 5 )
                continue;

        stuff
}
Re: loop that skips a number 80 80

Can you look at my other thread on here, the one about help on structors.

This problem pertains to that. I tried doing an if statement to just print "ok" when it comes accross the invalid number. The invalid variable must not have a value or something, cause my if statment is

if(i != invalid)
printf("Ok");

but it always prints prints ok, even though the way I have my data file set up is that the invalid data is on line three, it even prints it in the validate function, but nothing happens later in the other function.

Re: loop that skips a number 80 80

I figured it out.

Re: loop that skips a number 80 80

was it the "return invalid" statement being in the wrong place?

Re: loop that skips a number 80 80

Yeah, I had return (invalid) at the end of the function, which meant it would return invalid after the whole loop had done, so it would always return invalid as 5. I moved it to be in the if statement, that way it actually returns the number of the line where the error is.

Be a part of the DaniWeb community

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