Infinite Loop problem

This is a discussion on Infinite Loop problem within the C++ Programming forums, part of the General Programming Boards category; The following segment of code is part of a program to calculate the NZVC bits when adding two binary numbers ...

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

    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
    2,779
    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
    67
    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
    2,779
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    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,341
    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%.

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, 01: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-19-2005, 11:08 PM
  5. infinite loop problem
    By Gil22 in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2003, 02:24 PM

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