Counter

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 32
Reputation: Max_Payne is an unknown quantity at this point 
Solved Threads: 0
Max_Payne's Avatar
Max_Payne Max_Payne is offline Offline
Light Poster

Counter

 
0
  #1
Dec 14th, 2007
I want to increment the i without putting the whole statement in a loop.

  1. #prag...
  2. ostream &operator << ( ostream &output, const Course &xCourse )
  3. {
  4. int i = 0;
  5.  
  6.  
  7. output << " " << i++ << ".| " << xCourse.code << " | " << xCourse.section << " |" << endl
  8. << " -----------------------";
  9.  
  10. return ( output );
  11. }

Any ideas on how to do that.

tx.
Last edited by Max_Payne; Dec 14th, 2007 at 12:37 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Counter

 
0
  #2
Dec 14th, 2007
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:
  1. ostream &operator << ( ostream &output, const Course &xCourse )
  2. {
  3. static int i = 0;
  4.  
  5. output << " " << i++ << ".| " << xCourse.code << " | " << xCourse.section
  6. << " |" << endl << " -----------------------";
  7.  
  8. return ( output );
  9. }
Does that work for a class member?

Val
Last edited by Ancient Dragon; Dec 14th, 2007 at 12:58 am. Reason: Corrected bold tag
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 32
Reputation: Max_Payne is an unknown quantity at this point 
Solved Threads: 0
Max_Payne's Avatar
Max_Payne Max_Payne is offline Offline
Light Poster

Re: Counter

 
0
  #3
Dec 14th, 2007
Yes it works for a class member.. I can't believe i forgot about the static ...

Thank You...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Counter

 
0
  #4
Dec 14th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC