Thread: Infinite Loop

  1. #1
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870

    Infinite Loop

    In the earlier thread "Square Root Problem", I wrote a little program that contains this loop:
    Code:
    while (toupper(quit) != 'Y')
     {
      cout << "Please enter a non-zero number *****:" << flush;
      cin >> userinput;  //Get the number to be square rooted
     
      if (userinput <= 0 | isascii (userinput))  //Is it a nonvalid number or letter?
      {
       cout << "The number you entered was invalid. Would you like to quit (y/n)?" << flush;
       cin >> quit;  //Does the user want to quit?
         continue;  //If he wants to quit, then quit. Else, continue.
        }
      a = userinput;
      x = a;
     
      /*********Square root loop*************/
      lasty = 0;   //Null out lasty
      for (int i = 0; i <= 15; i++)
       {
       y = 0.5*(x+a/x); //This is our actual formula
       x = y;
       if (y == lasty)
       {
        break;  //Our formula has reached its end prematurely (under 15 loops)
       }
       lasty = y;
      }
    If I enter a letter (but not an invalid number) I have an infinite loop. THis doesn't make sense as both invalid numbers and letters have the same processing after being detected. When I enter -1, it presents me with the correct prompt. However, the second I enter a letter, it goes into an infinite loop showing the correct prompt over and over again with no time to input anything. Any ideas?

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    never mind, I fixed it.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >never mind, I fixed it.
    Just a tip. When you solve your own problem, it's polite to describe how you solved it. This accomplishes two things:

    1) If the solution is a good one, others can learn from it.
    2) If the solution can be improved, others can offer advice for doing so and you can learn.
    My best code is written with the delete key.

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    @Prelude:
    Being an idiot, I didn't look at the FAQ. The solution is there. Here is the replacement code:
    Code:
    while (cout << "Please enter a non-zero number *****:" << flush && !(cin >> userinput))   
      {
             cout << "The number you entered was invalid." << endl;
             cin.clear();
             cin.ignore(std::numeric_limits < int >::max(), '\n');
             continue;  
       }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    FWIW, note that the flush is unnecessary there as the use of cin causes an automatic flush. In addition, the continue is also unnecessary, the loop will automatically continue as it has reached the end of the block. That is why neither of those is part of the solution in the FAQ.

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    this was from a different thread, and I didn't write that code. It's just when I added an error checking statement for letters, it went into an infinite loop.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    To be strictly correct, this code
    Code:
    cin.ignore(std::numeric_limits < int >::max(), '\n');
    should be
    Code:
    cin.ignore(std::numeric_limits < std::streamsize >::max(), '\n');
    http://cboard.cprogramming.com/showp...06&postcount=5
    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
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    that has been copied straight from the FAQ, so maybe a revision?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Both are commonly used, and in practice both will work fine. If streamsize's max is smaller than int's max, then it is theoretically possible that some strange behavior could occur. The standard does specifically mention numeric_limits<streamsize>::max(), so that is probably the better one to use, but there's no need to rush and change it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM