1
votes

Why is this code only evaluating the first character of datatest?. I need it to test if the string contains only alphanumeric letter and whitespace characters and store it in records.data. rCount is a count of the records. It all works as intended expect the islanum and isspace function which is only evaluating the first character in the string and storing it. The way is works is good, except i want it to do it for the whole string not just the first character. If the data contains anything other than alphanumeric characters or whitespace i will store it elsewhere

for (i=0; i<=datalength; i++)
{
    if ((sourceint < 1025 && sourceint >0)&&
    (portint <1025 && portint >0) &&
    (typeint < 11 && typeint >=0) &&
    (destinationint <1025 && destinationint >0) &&
    (datalength < 51) &&
    (isalnum(datatest[i]) ||  isspace(datatest[i])))
    {
        records[*rCount].destination = destinationint;
        records[*rCount].type = typeint;
        records[*rCount].port = portint;
        records[*rCount].source = sourceint;

        for (i=0; i<=datalength; i++)
        {
            records[*rCount].data[i] = datatest[i];
            records[*rCount].data[i+1] = '\0';
        }

        printf("VALID DATA FROM STRUCT - %i - %i - %i - %i - %s\n", records[*rCount].source, records[*rCount].destination, records[*rCount].type, records[*rCount].port, records[*rCount].data);
    }
}
2

2 Answers

3
votes

Because you use the variable i as the iterator for both of your for() loops. Try using different variables. The inner one loops until i > datalength, which causes the outer loop to terminate as well since it uses the same condition.

0
votes

You use the same variable in both for loops when they're one inside the other. Basically i is 0, then it's in the inner loop and i is 0 and reaching to data length, on the first run i will turn to data length. Just declare some other integer such as j or whatever you'd liked. Also, rcount stays the same, you go the same spot in the array every time, maybe that's what you intended to do, try rCount++;.