1) You're not ever changing the values in the num
array, so testing to see if it contains a valid number doesn't work because the initial values you set don't fit the rules.
2) The validation is checking to see if both numbers are odd, not one or the other.
Here's a version that seems to work. The changes I made are to actually change the num
array and then use a helper function to validate the numbers in the array so you don't have a mess inside your loops. I removed the srand
call since you aren't using random numbers and the input of the first 4 digits to make testing easier for me. You can add that back if you like.
#include <iostream>
const int MAX_DIGITS = 10;
bool IsValid(int num[MAX_DIGITS])
{
int sum = 0;
for(int z = 4; z < MAX_DIGITS; ++z)
{
sum += num[z];
}
if(sum != 33)
{
return false;
}
int numodd = 0;
for(int z = 3; z < 5; ++z)
{
numodd += (num[z] % 2);
}
if(numodd != 1)
{
return false;
}
return true;
}
int main()
{
int num[MAX_DIGITS];
num[0] = 5;
num[1] = 5;
num[2] = 5;
num[3] = 1;
for (int e=0;e<MAX_DIGITS;e++)
{
num[4] = e;
for(int f=0;f<MAX_DIGITS;f++)
{
num[5] = f;
for(int g=0;g<MAX_DIGITS;g++)
{
num[6] = g;
for(int h=0;h<MAX_DIGITS;h++)
{
num[7] = h;
for(int i=0; i<MAX_DIGITS;i++)
{
num[8] = i;
for(int j=0;j<MAX_DIGITS;j++)
{
num[9] = j;
if(IsValid(num))
{
for(int z = 0; z < MAX_DIGITS; ++z)
{
if(z == 3 || z == 6)
{
std::cout << '-';
}
std::cout << num[z];
}
std::cout << std::endl;
}
}
}
}
}
}
}
}