Thread: Need ur lil time......2 wk in progrmming!

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    4

    Unhappy Need ur lil time......2 wk in progrmming!

    ( C++ )
    Should print multiple of 2 infinite

    Code:
    #include<iostream.h>
    #include<conio.h>
      void main( )
    {
        clrscr( );
    
      int number=2;    //declaration
        while( number>=2 )
             {
       
       cout <<number <<" \n ";   //will print multiples of 2 in in columns 
            number *=2;
                       }
    getch();
                  }

    unfortunately this will be terminated by printing 32768 value

    need ur help/suggestions .

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What you want cannot be done. At some point, you will have to stop printing multiples of two. In your case, it stopped because number was doubled beyond the maximum value for int, and presumably this arithmetic overflow resulted in number having a negative value, thus terminating the loop.

    Even if you change number from being an int to be say, an arbitrary precision integer type, you still cannot print multiples of two indefinitely: there is a limit to your computer's memory and such, and even if there wasn't, I doubt that you are immortal.
    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

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by laserlight View Post
    there is a limit to your computer's memory and such, and even if there wasn't, I doubt that you are immortal.
    Hold on now, let's not be too presumptuous...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Xodix View Post
    unfortunately this will be terminated by printing 32768 value
    [assumption]Plz drop that TurboCrap you're using for a compiler that's younger than me.[/assumption]
    Devoted my life to programming...

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by GReaper View Post
    [assumption]Plz drop that TurboCrap you're using for a compiler that's younger than me.[/assumption]
    How old is Turbo Crap?

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Too old

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Depending on version, release dates start at around 1987.

    Jim

  8. #8
    Registered User
    Join Date
    Nov 2013
    Posts
    4
    Quote Originally Posted by manasij7479 View Post
    How old is Turbo Crap?
    The version i was using is 3.8.0.1....

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Xodix View Post
    The version i was using is 3.8.0.1....
    Assumption wrong, still valid though!
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-17-2013, 11:32 PM
  2. Replies: 2
    Last Post: 04-17-2013, 12:25 AM
  3. [HELP] Code to Convert Army Time to Normal Time?
    By Kipper DeVera in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2011, 11:50 PM
  4. Replies: 7
    Last Post: 11-21-2009, 01:02 AM
  5. progrmming in C
    By zeetaQ53 in forum C++ Programming
    Replies: 18
    Last Post: 07-21-2009, 12:46 PM