Thread: double incremending a variable

  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,336
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Exclamation

    Code:
    (number++)++
    should work...but +2 is better..

  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
    733
    Quote Originally Posted by jackson6612 View Post
    Code:
        cout << "number after increment is: " << number++ << endl;
    This gives value before increment.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Why are the parentheses must ?

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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 ?

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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, 03: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, 03:21 PM