double incremending a variable

This is a discussion on double incremending a variable within the C++ Programming forums, part of the General Programming Boards category; Hi I have tested it on my compiler and it seems not possible to do - I mean the double ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    double incremending a variable

    Hi

    I have tested it on my compiler and it seems not possible to do - I mean the double increment. Is there a way to do double increment using some manipulation technique such as parentheses etc? Please let me know. Thank you.


    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    
    {
        int number = 5;
    
        cout << "number after increment is: " << number++ << endl;
    
        cout << "number after double increment is: " << number++++ << endl;
    
        system("pause");
        return 0;
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Given the existence of "number + 2", I doubt anyone's tried very hard to make it work.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Okay. Thanks, tabstop.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    Exclamation

    Code:
    (number++)++
    should work...but +2 is better..
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    +2 would be temporary
    you could
    Code:
    std::cout << (number+=2) << std::endl;
    note the parentheses have to be there.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by jackson6612 View Post
    Code:
        cout << "number after increment is: " << number++ << endl;
    This gives value before increment.
    I never put signature, but I decided to make an exception.

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Why are the parentheses must ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    It will not compile without them?

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot, everyone.

    Quote Originally Posted by kmdv View Post
    This gives value before increment.
    kmdv: thanks for pointing this out.

    Regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Quote Originally Posted by manasij7479
    Code:
    (number++)++
    should work...but +2 is better..
    It should not work since the result of (number++) is an rvalue, hence you cannot increment it. You could do ++++number instead, but I believe that that results in undefined behaviour since it modifies number twice between consecutive sequence points. However, if number were an object of class type and hence ++++number would result in nested function calls, the behaviour would be well defined.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Thanks for pointing that out...
    I thought that x++ is internally expanded as (x = x+1; ) and then the x; can be (re)used as the lvalue .
    btw..why isn't it implemented as I thought it'd intuitively be ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,244
    The result of a post-increment is NOT an lvalue. It is the value of the variable BEFORE incrementing. Thus the expression x++++ would try to assign to something which is not an lvalue.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using double codes in assigning value to an int variable
    By hitesh_best in forum C Programming
    Replies: 10
    Last Post: 08-02-2007, 01:12 AM
  2. how to change a random variable to double ?
    By arian in forum C# Programming
    Replies: 3
    Last Post: 10-05-2006, 10:01 AM
  3. retrieve double variable in scanf
    By cfriend in forum C Programming
    Replies: 1
    Last Post: 11-25-2004, 02:53 PM
  4. Double variable problem...
    By Dakkon in forum C++ Programming
    Replies: 15
    Last Post: 10-05-2002, 07:54 PM
  5. How do I initialize a variable bigger than a double?
    By Robert602 in forum C++ Programming
    Replies: 2
    Last Post: 12-21-2001, 02:21 PM

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