# Thread: getting numbers in an array

1. ## getting numbers in an array

I have this code below that accepts up to 8 numbers and stores them in an array. I use a zero to stop taking numbers. I am trying to keep the zero from being stored in the array. Is there a way to do it?

Code:
```    printf("\nPlease enter up to 8 numbers to stop entering numbers enter zero\n");
printf("\n");
for (i=0;i<8;i++)
{
fgets(theNums[i],50, stdin);

j=strlen(theNums[i])-1;

theNums[i][j] = '\0';

if (theNums[i][0]=='0') /*exit if zero*/
break;
else
nums[i]=atoi(theNums[i]);
numberCount ++; /*count numbers being entered*/
}
printf("\n\n");```

2. There are a few ways to do it.

1) Read into a buffer first, check it for zero, if it isn't zero, copy it into the array.

2) Use your current method, but instead of just using "break", clear out that spot in the array and then break.

Quzah.

3. How do I clear out the spot in the array? I don't know how to do that. Clearing the spot in the array seems like the easier way since I could just put it before the line that has break in it.

4. Originally posted by ct26torr
How do I clear out the spot in the array? I don't know how to do that. Clearing the spot in the array seems like the easier way since I could just put it before the line that has break in it.
array[blah][blah] = '\0';

Or, use something like memset to clear it.

Quzah.

5. cool I will check out memset .... thanks for the help

Code:
`theNums[i][j] = '\0';`
I still can't get it to work

7. I have this but it still seems to store the zero in the array

Code:
```    printf("\nPlease enter up to 8 numbers to stop entering numbers enter zero\n");
printf("\n");
for (i=0;i<8;i++)
{
fgets(theNums[i],50, stdin);

j=strlen(theNums[i])-1;

theNums[i][j] = '\0';

if (theNums[i][0]=='0') /*exit if zero*/
{
break;
}
else
{
nums[i]=atoi(theNums[i]);
numberCount ++; /*count numbers being entered*/
}
}
printf("\n\n");```