Thread: Infinite Loop problem

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    73

    Infinite Loop problem

    The following segment of code is part of a program to calculate the NZVC bits when adding two binary numbers together. This particular segment will determine the Z bit (whether or not the entire binary number equals zero).

    However, it is an infinite loop and I can't figure out why. I've already nailed down the problem to somewhere in this segment of code, as everything else worked perfectly when I commented it out.

    For those wondering, the extra space of b3 (b3[0]) in a 9-space array, contains the Carry-Out (C) bit.

    Code:
            z = 1;
            i = 8;
            while (z != '0')
            {
                while (i>0)
                {
                    if (b3[i] == '1')
                        z = 0;
                    else
                        i--;
                }
            }
            cout << "The Zero bit is " << z << endl;

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The char '0' is not the same value as the number 0 so your first loop will never end.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    73
    How do I store the integer 0 there instead? I removed the 's, but same result. Infinite loop.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What? You're already storing the integer into z. But you're trying to compare the character '0' to this int. The char '0' has an int value of 48. You probably want to compare z to 0 the integer not the character '0'.

    Jim

  5. #5
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Even though the char 0 has the ASCII value of 48 (this is implementation defined by the way -- the character constant 'a' might not be 48) don't use 48, use the char constant '0' (i.e. with the single quotes around the 0)

    Google terms: Character constants C

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The character '0' is the digit zero.
    The character '\0' is the number 0; the backslash escapes away the digit meaning.
    You could also use the number 0 anywhere you can use '\0', but people will bug you about it from time to time.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Presumably b3[i] is never equal to the digit '1' (which does not have a value of 1, BTW, just as '0' and 0 are non-equal). That means z will never be set to zero.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having a problem with an infinite loop
    By Bacondeluxe in forum C Programming
    Replies: 2
    Last Post: 10-04-2012, 05:13 AM
  2. infinite loop problem
    By langamer101 in forum C Programming
    Replies: 7
    Last Post: 12-12-2011, 02:55 AM
  3. Infinite Loop, problem with while()
    By JonathanS in forum C Programming
    Replies: 2
    Last Post: 09-08-2011, 03:47 AM
  4. Infinite loop problem
    By Xanth in forum C++ Programming
    Replies: 2
    Last Post: 02-20-2005, 12:08 AM
  5. infinite loop problem
    By Gil22 in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2003, 03:24 PM