0

I want to increment the i without putting the whole statement in a loop.

#prag...
ostream &operator << ( ostream &output, const Course &xCourse )
{
	int i = 0;


        output << "  " << i++ << ".| " << xCourse.code << "    |  " << xCourse.section << "       |" << endl
                  << "    -----------------------";

	return ( output );
}

Any ideas on how to do that.

tx.

3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Ancient Dragon
0

Do you mean that you want i to be one larger every time you call the function to output a line?

How about making it a static variable, as in:

ostream &operator << ( ostream &output, const Course &xCourse )
{
	static int i = 0;

        output << "  " << i++ << ".| " << xCourse.code << "    |  " << xCourse.section 
                  << "       |" << endl  << "    -----------------------";

	return ( output );
}

Does that work for a class member?

Val

0

Yes it works for a class member.. I can't believe i forgot about the static ...

Thank You... :)

0

If you use static the value of i will be the same for all instances of the class and there will be no way to reset it back to 0 if you need to. I think a better solution is to use a different variable name and make it a class object so that each instance of the class has its own variable and you can reset it to 0 whenever you need to.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.