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 <iostream>
#include <string>
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 < bar.length())
{
int test = 0;
tempstring = bar.substr(count, SIZE);//split bar string into next five characters
for (int index = 0; index < SIZE; index++)
{
if (tempstring[index] == '1')
{
++test;
}
}
if (test != 2)
{
cout << "Invalid bar code!\n";
exit(1);
}
count += 5;
}
cout << "Valid bar code!\n";
}