Could anyone please tell me what's wrong with this code? The output for 10A, 10B, and 10C is somehow missing its seat[10].fname string. I've narrowed the culprit down to the following code.
Code:
for (i = 1; i <= 30; i++)
{
if (i <= 10)
{sprintf(seat[i].no, "%dA", i);
printf("%s%d", seat[10].fname, i); // the string still holds here
printf("%s%d", seat[10].lname, i);}
else if (i <= 20)
sprintf(seat[i].no, "%dB", i - 10);
else if (i <= 30)
sprintf(seat[i].no, "%dC", i - 20);
}
If I changed the code to
Code:
for (i = 1; i <= 30; i++)
{
if (i <= 10)
sprintf(seat[i].no, "%dA", i);
else if (i <= 20)
{sprintf(seat[i].no, "%dB", i - 10);
printf("%s%d", seat[10].fname, i); //string disappears!
printf("%s%d", seat[10].lname, i);}
else if (i <= 30)
sprintf(seat[i].no, "%dC", i - 20);
}
EDIT: Got it! I forgot about '\0' in the declaration.