infinite loop?multiples of 2/

This is a discussion on infinite loop?multiples of 2/ within the C++ Programming forums, part of the General Programming Boards category; Code: /* #include<iostream> #include<conio.h> using std::cout; using std::cin; using std::endl; int main() { // declare variables int x,counter; cout<<"\n\n\t\t"<<2; // ...

  1. #1
    o0o
    o0o is offline
    Registered User o0o's Avatar
    Join Date
    Dec 2003
    Posts
    37

    Question infinite loop?multiples of 2/

    Code:
    /*
    
    #include<iostream>
    #include<conio.h>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
        // declare variables
        int x,counter;
        
        cout<<"\n\n\t\t"<<2;
        
        // print squares
        for(x=2;x<=counter;x*=2)
        {
            cout<<"\n\n\t\t"<<x*2;
        }
        
    getch();
    }
    Is this code logically corrrect.In order to make it infinite what condition/s should be used.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Is the code correct. Doubt it. Especially as counter is unitialised to anything at first use.

    Infinite loop.... easy....
    Code:
    while ( true )
    {
       // blah blah
       if ( some condition ) break;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Alternatively:
    Code:
    for( ; ; ) {
    // ... etc ...
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Infinite loop the functional way:
    Code:
    template<typename F>
    void infinite( const F& f )
    {
        f();
        infinite(f);
    }
    Not very good in C++, though. In fact, don't use it.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269

    The Easy Way:

    Okay, let's do it the easy way:

    Code:
    #include <iostream>
    
    using std :: cin;
    using std :: cout;
    
    int main(int argc, char *argv[])
    {
      int X = 1;
      while(X = 1)
      {
        cout << "Infinite loop printing this... ";
      }
      cin.get();
      return 0;
    }
    -SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, while 'X = 1' will always be true. Did you mean 'X == 1', though? At any rate, I fail to see how that is easier or more elegant than 'while(true)' or 'while(1)'.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    or you could use a forever for loop.....
    Code:
    for (;;) //forever loop....
    {
        do something
        if  ( something )
            { break; }
    }
    Ph33r the sphericalCUBE

  8. #8
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Everyone's right but they didn't point out that you have to have an exit condition to exit these types of loops save stoned coder and littleweseth and you'd have to do something to modfy the value of "X" n SirCronos' code. (Sang-drax's may eventually drain your system resources; infinite recursion as opposed loops run you out of memory).
    Last edited by WDT; 12-25-2003 at 08:31 PM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by WDT
    Everyone's right but they didn't point out that you have to have an exit condition to exit these types of loops
    Well just going by what was given, the only thing the original poster requested was a change to make the loop infinite. They never said anything about breaking out of it.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Show them how to jump off the edge of the cliff because they asked; but not how to fly eh quzah??

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    It's their decision to jump and their fault if they break their neck
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. How to stop an infinite cyle?
    By opsis in forum Linux Programming
    Replies: 4
    Last Post: 01-07-2007, 11:58 AM
  3. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  4. Infinite loop
    By osal in forum Networking/Device Communication
    Replies: 1
    Last Post: 06-08-2004, 04:18 PM

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