Ali
Ali

Reputation: 10463

The data isn't storing in the array

I'm trying to store some data in the array call holder and the problem is that when I display that array nothing being store in it and I don't know what is wrong even though the logic seem to be right for me. The data is coming from an array call sender I'm using two dimension array to store it up to 5 at MAX.

        for (int t = 0; t < strlen(sender) && stop == false; t++){ // stop is the bool that created to break the loop
            if (sender[t] != ';'){ // all the data being store in the holder will be separated by ';'
                holder[d][t] = sender[t];
            }
            if (sender[t] == ';') // if the sender at position of 't' number meet ';' then plus one to start store the next data
                d++;
            if (holder[d][t] == '\0'){ // if it meet the '\0' then exit from the for loop
                holder[d][t] = '\0';   // If `;` found, null terminate the copied destination.
                stop = true;

            }
        }

This is the sender array "Hello;How;Is;Good Bye"

The output is

Your holder-----> '

Actual holder---> 'Hello'

Upvotes: 0

Views: 96

Answers (3)

Anders Sj&#246;qvist
Anders Sj&#246;qvist

Reputation: 3482

I don't understand why you're not allowed to use break. Maybe you could clarify the objective of the exercise? Otherwise, maybe this code solves your problem? I would normally have used another technique, but since you said that you're not allowed to use break...

int pos = 0, col = 0, row = 0;
do {
  if(';' == sender[pos] || 0 == sender[pos]) {
    holder[row++][col] = 0;
    col = 0;
  } else {
    holder[row][col++] = sender[pos];
  }
} while(0 != sender[pos++]);

Upvotes: 3

Ofir Baruch
Ofir Baruch

Reputation: 10346

There's a problem with the last condition if (holder[d][t] == '\0'). There are 2 chars in this condition (\0) but the holder[d][t] is only one char, therefore this condition will never be true. What's about the next code?

int aStringLength = strlen(sender);
int t = 0;
while( stop == false )
{
 if(t == aStringLength)
   stop = true;

 if(sender[t] != ';')
 {
   holder[d][i] = sender[t];
   i++;
 }
 else
 {
   d++;
   i = 0;
 }

 t++;
}

Upvotes: 1

Vaughn Cato
Vaughn Cato

Reputation: 64308

One issue is that you are using t for both the index for holder and the index for your input string. This may work for the first section, but not for the ones afterwards.

You also want to store a null terminator when you hit a semicolon.

Upvotes: 2

Related Questions