infinite looping when 2 large integers divide

This is a discussion on infinite looping when 2 large integers divide within the C++ Programming forums, part of the General Programming Boards category; relatively new to c++ and to the forum. wrote a program that divides two integers: Code: int main() { unsigned ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    4

    infinite looping when 2 large integers divide

    relatively new to c++ and to the forum. wrote a program that divides two integers:
    Code:
    int main()
    {
    	unsigned long a, b, c;
    	do
    	{
    		cout << "\n\nEnter a and b respectively.\n";
    		cin >> a; cin >> b;
    		cout << "a/b= " << (double) a/b << "\n";
    		cout << "Continue? 1=yes, 0=no\n";
    		cin >> c;
    	}while(c);
    
    	return 0;
    }
    i understand that a digit that is more than 10 digits long cannot be fed into a or b. But what I don't understand is why would the program loop infinitely without pausing for an input in c when a huge number is entered (besides c doesn't have a value for while to respond). have search the faqs and threads but couldn't find what i wanted.

  2. #2
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    I don't understand is why would the program loop infinitely without pausing for an input in c when a huge number is entered
    even if i entered a number that much larger than unsigned long can hold, this program didn't loop infinitely without pausing.
    everthing is fine.

    blow me ... ...

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    5
    Well, I remember vaguely having this kind of issue... And yes, I have tried program on my machine and it shows the symptoms.

    I maybe wrong here...

    You have to remember that input and output using cin and cout are buffered. This means that until EOF (ctrl + d on unix) is sent, cin doesn't know where the current input ends. Now, to be able to tell if you have met the end of input when using cin, you should be using cin.eof() to check it.

    When you/I enter a large number to the program, it gets buffered, cin reads in as much as it can for a and b. Then reads in c. Now if you are lucky and there's 0 somewhere on the input buffer, and c becomes 0 by some luck, then the code terminates. Otherwise, c will take a value greater than 0 (probably). This implies that the loop has to keep going.

    After reading everything from input buffer,
    Code:
    cin >> c;
    will simply not read in anything. So, c gets to keep its old value, and there we have infinite loop.

    Please note that you cannot check for error with cin until you actually have tried to read in from the stdin. So rather than looping on the value c, you might as well check to see if EOF has been sent from the stdin.

    Another note, EOF is NOT a character. It's just a convinient way to denote the end of a file. And, I think ctrl + d sends an EOF signal.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Apparently, how the compiler handles this type of problem is undefined. I used MSVC 6 with the following code:

    int num1;
    cout << num1;

    and, as expected I got garbage out, as num1 isn't initialized. Then I did this:

    int num1;
    cin >> num1;
    cout << num1;

    and input progressively larger values. The following values worked fine:

    999
    999999
    999999999

    but

    999999999999

    caused output to be garbage again, not a never ending loop. Then I did this:

    int num1, num2 = -1;
    cin >> num1;
    cin >> num2;
    cout << num1 << endl;
    cout << num2;

    and input
    999<enter>
    111<enter>

    which yielded appropriate output. But when I input this:

    999999999999<enter>

    the program went directly to output without waiting for my second input and output garbage for num1 and -1 for num2.

    It would be interesting if those who have tried this program/problem posted what compiler they used to look into this question to see if different compilers do indeed give different results--because the behaviour is undefined and therefore up to the implentation team for each compiler to deal with, or if there is something out there that none of the posters so far (incluiding me) know about.
    You're only born perfect.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Horse22
    relatively new to c++ and to the forum. wrote a program that divides two integers:


    i understand that a digit that is more than 10 digits long cannot be fed into a or b. But what I don't understand is why would the program loop infinitely without pausing for an input in c when a huge number is entered (besides c doesn't have a value for while to respond). have search the faqs and threads but couldn't find what i wanted.
    When using cin to accept numerical data, it is wise to test to see if everything is OK (what if the user inters a non-numeric character or does someting else to cause cin to enter a "fail" state).

    Try this:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
      unsigned long a, b, c;
      do
      {
        cout << "\n\nEnter a and b respectively." << endl;
        cin >> a; 
        cout << "You entered a = " << a << endl;
        if (!cin) {
          cout << "cin is NULL after reading a" << endl;
          break;
        }
        cin >> b;
        cout << "You entered b = " << b << endl;
        if (!cin) {
          cout << "cin is NULL after reading b" << endl;
          break;
        }
        cout << "a/b= " << (double)a / b << endl;
        cout << "Continue? 1=yes, 0=no"<< endl;
        cin >> c;
        cout << "You entered " << c << endl;
        if (!cin) {
          cout << "cin is NULL after reading c" << endl;
          break;
        }
      }while(c);
    
      return 0;
    }
    Then enter 11111111111 and 22222222222, or whatever large numbers (or non-numbers) that you feel like

    Regards,

    Dave

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Right. The problem is, if you try to input a number that won't fit into the variable (or the variable is the wrong type, etc.), cin will set an error and go into a 'fail state', and from then on every time you try using cin it will simply return immediately without doing any input. To check if cin is till 'ok' (i.e. no error has been set), you check if(cin) or if(cin.good()). If an error has occurred and you still want to keep inputting data, you call:
    cin.clear(); //clear error flags

    Alternately if you want to check if cin is NOT ok, you can check if(cin.fail()), and if something is wrong you can find out if it's something not good or of it's something horrible by checking if(cin.bad()). If bad() returns true, then it's something horrible; otherwise, it's something not good.

    Hope this helps!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Infinite looping
    By InvertedSaint in forum C Programming
    Replies: 5
    Last Post: 04-23-2006, 12:59 AM
  3. Infinite Looping Program Prob's
    By dld333 in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2005, 12:44 AM
  4. Messing with Large Integers
    By johnnie2 in forum Windows Programming
    Replies: 3
    Last Post: 11-16-2002, 12:22 PM
  5. Storing large integers....why doesn't this work?
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 09-21-2001, 09:41 PM

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