Int Array - Stop taking values at [zero]

This is a discussion on Int Array - Stop taking values at [zero] within the C Programming forums, part of the General Programming Boards category; I'm working on an integer array. I have it to accept exactly ten values. How can I make it accept ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    6

    Int Array - Stop taking values at [zero]

    I'm working on an integer array. I have it to accept exactly ten values. How can I make it accept UP TO ten numbers, and stopping if a zero is entered? I should probably be using a WHILE loop instead?

    Here's a snippit of some code from my program.

    Code:
         void main (void)
    
    	int numbers[10];				
    	int x;	
    	
    
    	printf("Enter 10 numbers\n");
    	
    	for(x=0; x<=9; x++)
    	{
    		printf("Number %d:", x+1);
    		scanf("%d", &numbers[x]);
    	}

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by bunko View Post
    I'm working on an integer array. I have it to accept exactly ten values. How can I make it accept UP TO ten numbers, and stopping if a zero is entered? I should probably be using a WHILE loop instead?

    Here's a snippit of some code from my program.

    Code:
    int main (void)
    {
        int numbers[10];                
        int x;    
     
     
        printf("Enter 10 numbers\n");
     
        for(x=0; x<=9; x++)
        {
            printf("Number %d:", x+1);
            scanf("%d", &numbers[x]);
            if(numbers[x] == 0)
                break;
        }
    }
    Nope. Just do that.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    Thank you!

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    No problem dude

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM

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