Hey guys!
I'm new to the forums and C overall
I'm learning for a degree in Math and computer Sciences and I've stumbeled across something I Can't seem to debug... (First semester lol, a noob still).
Well the program needs to get a list of IDs until it reaches EOF, and does some algorithm on it. if the ID is abcde, then ((a+b)*3+(c+d)*7)%10 should be like "e" in the original ID.
The first input, everything goes ok but on the second one and so forth it seems as tho there is a problem.... my Array might be getting messed up?
Some directions will be well appreciated
Edit:
just to make things clear:
ID that is invalid - prints a warning.
ID that doesn't follow the calculating rule - Printing it and saying it's incorrect.
ID that is correct - doesn't do anything
Here's the program:
Code:
int main()
{
int counter=0, id, fail, loop=2;
int ids[5] = {0,0,0,0,0};
int sum=0,last=0;
printf("Enter list of IDs\n");
while (loop==2)
{
counter=0;
fail=0;
id=0;
sum=0;
last=0;
while (counter<5) //Gets 5 chars!!!
{
id=getchar();
ids[counter] = (id-'0');
if ((id)== EOF)
{
return 0;
}
if ((id<'0') || (id>'9'))
{
fail=2;
}
if ((fail==2) && (counter==4))
{
printf("invalid input, it should be 5 digits (0-9)\n");
fail=0;
counter=0;
while (counter<5) //Array reset!!!
{
ids[counter] = 0;
counter+=1;
}
counter=0;
continue;
}
counter+=1;
}
counter=0;
sum+= ((ids[0]+ids[1])*3);
sum+= ((ids[2]+ids[3])*7);
sum%=10;
last= (ids[0] * 10000);
last+=(ids[1] * 1000);
last+=(ids[2] * 100);
last+=(ids[3] * 10);
last+=(ids[4]);
if (sum==(ids[4]))
{
sum=0;
}
else
{
printf("incorrect id: %d\n", last);
sum=0;
last=0;
}
while (counter<5) //Array reset!!!
{
ids[counter] = 0;
counter+=1;
}
}
return 0;
}