getting numbers in an array

This is a discussion on getting numbers in an array within the C Programming forums, part of the General Programming Boards category; I have this code below that accepts up to 8 numbers and stores them in an array. I use a ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    55

    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. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

    Take your pick.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    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. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    cool I will check out memset .... thanks for the help

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    I tried adding

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

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    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");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To Find Largest Consecutive Sum Of Numbers In 1D Array
    By chottachatri in forum C Programming
    Replies: 22
    Last Post: 07-10-2011, 01:43 PM
  2. entering numbers to array in a row whithout length input
    By transgalactic2 in forum C Programming
    Replies: 55
    Last Post: 01-02-2009, 03:02 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21