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.

Recommended Answers

All 3 Replies

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

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

Thank You... :)

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.

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.