1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
int evaluate (char [],const char [], int& , int&);
int checkSize(char []);
int main() {
const char nums [6] = { '2', '3', '6', '7', '8' };
char iiNum[6];
int counter=1, i=0, count1=0, count2=0, eva, sizee=0;
do
{
cout << " Try number " << counter << endl << "Enter a number(q to exit): ";
counter++;
cin >> iiNum;
sizee=checkSize(iiNum);
for (int j=0; iiNum[j]!='\0'; j++)
{
for (int k=0; iiNum[k]!='\0'; k++)
{
if (iiNum[0]=='q')
break;
if (!(iiNum[j]>='0' && iiNum[j]<='9'))
{
cout << "Enter only number digits!";
cin >> iiNum;
j-=1;
}
}
while (sizee!=5)
{
if (iiNum[0]=='q')
break;
cout << "You must enter a 5-digit number!";
cin >> iiNum;
sizee=checkSize(iiNum);
}
}
if (iiNum[0]=='q')
break;
eva=evaluate (iiNum,nums,count1,count2);
cout << "You have guessed " << count2 << " numbers and " << count1 << " of them are in the correct position!" << endl << endl;
if (eva==1)
{cout << "Congratulations you have guessed all five numbers!!" << endl;
break;
}
}
while (counter<=10);
if (iiNum[0]=='q')
cout << "You exited___Goodbye!" << endl;
else if (counter==10)
cout << "You run out of moves! " << endl << "You lost!" << endl;
return 0;
}
int evaluate (char inNum[], const char coNum[],int &count1,int &count2) // count1 counts the correct position and count 2 the correct number
{
int j=0,i=0,numcount=0, iNum[5];
count1=0;
count2=0;
while (j<5)
{
for (i=0; i<5; i++)
{
if((inNum[j]==coNum[i]) && (j==i)) //checks if numbers are equal AND if at the same position increase counter1
{count1++;
count2++;
if ((inNum[j]==inNum[i])&&(j!=i))
count2--;
}
else if (inNum[j]==coNum[i]) // if numbers equal BUT not correct position increase counter2
{count2++;
if (inNum[j]==inNum[i])
count2--;
}
}
i=0;
j++;
}
if (count1==5)
return 1;
else
return 0;
}
int checkSize(char iNum[])
{
int length=0;
while (iNum[length]!='\0')
length++;
return length;
}
|