Searching a string of consecutive characters in a binary bar code I am writing a program that will take a binary bar code and convert it to a zip-code. I need to validate the bar code. A valid binary bar code contains no more and no less than two 1s in any position per five digits. This is what I have for the validation part so far and would like simplify this code.

#include &lt;iostream&gt;
#include &lt;string&gt;

using std::cout;
using std::string;

int main()
{
    const int SIZE = 5;//number of characters to test in a set
    string bar = "100101010010001";

    string tempstring;
    int count = 0;
    while (count &lt; bar.length())
    {
        int test = 0;
        tempstring = bar.substr(count, SIZE);//split bar string into next five characters

        for (int index = 0; index &lt; SIZE; index++)
        {
            if (tempstring[index] == '1')
            {
                ++test;
            }
        }

        if (test != 2)
        {
            cout &lt;&lt; "Invalid bar code!\n";
            exit(1);
        }

        count += 5;
    }

    cout &lt;&lt; "Valid bar code!\n";
}
The constant SIZE and its comment don't quite seem consistent.  A name such as size would correspond to an actual size of, say, a structure.  But with such a comment, it doesn't seem to be for that.  Consider renaming the constant to something more accurate.
bar should be const since it's not modified.
test seems like an ambiguous name for an integer variable.  It sounds more like a Boolean test flag, but that's not the case here.  Consider renaming it to something more accurate.
Since tempstring is only used within the while loop, it can just be initialized there.  It doesn't need to be declared before it.
You shouldn't mismatch integer types when doing comparisons:


while (count &lt; bar.length())



Since length() returns an std::size_t, which is an unsigned type, have count be of type std::size_t as well.  If you had compiler warnings up, then this should've been reported.
This may be a small nit-pick, but you can just return 1 instead of calling exit() in main().