Two values in i+=1

This is a discussion on Two values in i+=1 within the C++ Programming forums, part of the General Programming Boards category; Hi freinds. please read this code. Code: int main() { int i = 0; for(i=0;i<10;i+=1) cout << i; } When ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    Two values in i+=1

    Hi freinds.


    please read this code.

    Code:
    int main()
    {
         int i  = 0;
    
         for(i=0;i<10;i+=1)
    
         cout << i;
    }
    When I run this code in VC++, I get the output as 0,1. It shoyld come as 0, 1, 2......9. Does any one can tell me why could this happen?

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    cout is buffered. Try calling cout.flush() after your output. Calling << endl will also flush the buffer.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    it works ok for me in VC++ 6.0. This is the output. If you want spaces and commas then you have to program them, cout will not do that for you.
    Code:
    0123456789Press any key to continue

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    If you want spaces and commas then you have to program them, cout will not do that for you.
    Aren't there modifiers that do that sort of thing?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    To my limited knowledge of c++ iostreams, there are no such modifiers.

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    you could try

    Code:
    for(int i; i <10; i++)
    {
    i+=1;
    cout << i << endl;
    }

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Are you sure you want the i+=1;?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    oops, yeah, make that i a x or something..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  2. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 06:01 PM
  3. Struct Values
    By Muphin in forum C++ Programming
    Replies: 5
    Last Post: 08-13-2005, 10:24 PM
  4. Computing Large Values
    By swbluto in forum C++ Programming
    Replies: 8
    Last Post: 04-07-2005, 04:04 AM
  5. Replies: 1
    Last Post: 02-03-2005, 03:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21