| | |
Counter
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
I want to increment the i without putting the whole statement in a loop.
Any ideas on how to do that.
tx.
c++ Syntax (Toggle Plain Text)
#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.
Last edited by Max_Payne; Dec 14th, 2007 at 12:37 am.
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:
Does that work for a class member?
Val
How about making it a static variable, as in:
C++ Syntax (Toggle Plain Text)
ostream &operator << ( ostream &output, const Course &xCourse ) { static int i = 0; output << " " << i++ << ".| " << xCourse.code << " | " << xCourse.section << " |" << endl << " -----------------------"; return ( output ); }
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
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.
![]() |
Similar Threads
- ASP .NET hit counter? (ASP.NET)
- letter and word counter (C)
- I'm looking for a counter.. (HTML and CSS)
- Counter issues (C)
- ASP .NET database hit counter (ASP.NET)
- Counter Strike issue (Windows Software)
- Page counter print accounting (*nix Software)
Other Threads in the C++ Forum
- Previous Thread: Char array, getting weird output
- Next Thread: CString into string
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






