Thread: Little Problem

  1. #1
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104

    Unhappy Little Problem

    Hi, I have a problem with this:

    Code:
    #include<iostream.h>
    
    typedef unsigned long int ulong;
    
    int main()
    {
    	ulong a,b;
    
       while(cin >> a >> b)
       {
    		if(a==0 && b==0)
          	return 0;
    
          int count = 0, cry = 0;
    
          while(a || b)
          {
          	int x = a%10, y = b%10;
    
             a/=10;
             b/=10;
    
             if((x+y+cry)>9)
             {
               	cry = 1;
             	count++;
             }
             else
             	cry = 0;
          }
    
          if(count>1)
          	cout << count << " carry operations." << endl;
          else if(count == 1)
             cout << "1 carry operation." << endl;
          else
          	cout << "No carry operation." << endl;
       }
    
       return 0;
    }
    IO:
    9 1
    1 carry operation.
    9 0
    1 carry operation.
    9 0
    No carry operation
    9 0
    No carry operation
    9 1
    No carry operation
    9 1
    1 carry operation.


    09 1
    No carry operatio
    1 carry operation
    But what you'll tell about it's output? Can anyone explain...why this is so ...???
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps because 09 is treated as an (out of bounds) octal number
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104
    Hmmm...may be....
    but what you'll tell about the 2nd and 3rd IO set ???
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  4. #4
    Registered User lobo's Avatar
    Join Date
    Oct 2001
    Posts
    71
    ..i compiled that myself and it works OK - carry ops as expected...what compiler did u use? Try code

    int x, y;

    x = a % 10;
    y = b % 10;

    instead of

    int x = a %10, y = b%10;

  5. #5
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104
    Hi, lobo but why this would happen???

    I use borland C++ 5.02

    And what your compiler (what's it?) says about 2 & 3 IO set???
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  6. #6
    Registered User lobo's Avatar
    Join Date
    Oct 2001
    Posts
    71
    I tried your code along with your inputs and it works fine -- tried it on bcc5.5... try the thing i wrote above, it may help (not surely, though ), else try different compiler if u can...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM