Thread: Stopping an Array before reaching size limit.

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    3

    Stopping an Array before reaching size limit.

    I'm trying to write a program that will allow the user to input up to 1000 numbers in a one dimensional array.

    Code:
    /*Function that loads the array*
    /void loadArray(int a[]){
         int i;
         for (i = 0; i < ASIZE; i++) {
             printf("Enter up to 1000 numbers: ");
             scanf("%i", &a[i]);
             }
         return;
         }
    How would I be able to have the user stop the array when they've entered the amount of numbers that they needed to enter. Right now it continues until 1000 numbers have been entered.

    Thank you so much!


    I found the answer online!

    Code:
    printf("Enter the number of elements in array\n");
        scanf("%d",&size);
     
        printf("Enter %d integers\n", size);
     
        for ( c = 0 ; c < size ; c++ )
            scanf("%d", &array[c]);
    Last edited by Endothes; 06-24-2012 at 06:14 PM. Reason: Fixed

  2. #2
    Registered User
    Join Date
    Jun 2012
    Posts
    39
    Make sure you either are working in a c99 compatible implementation or one that allows for variable length arrays.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Codegeek892 View Post
    Make sure you either are working in a c99 compatible implementation or one that allows for variable length arrays.
    actually - it is not required - you just need to store the number of filled elements.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    39
    That's actually smart. I didn't think of that.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Codegeek892
    That's actually smart. I didn't think of that.
    If you read Endothes' edit, you would have seen that that is pretty much the solution that he/she stumbled upon after searching online
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread memory size limit
    By 3Nex in forum C Programming
    Replies: 1
    Last Post: 05-22-2010, 07:33 AM
  2. Filesystem type and file size limit
    By suloku in forum C Programming
    Replies: 6
    Last Post: 12-22-2009, 02:07 PM
  3. Is there a limit to the size of 2-D arrays?
    By ashley in forum C Programming
    Replies: 2
    Last Post: 01-04-2007, 04:01 PM
  4. Replies: 4
    Last Post: 04-05-2004, 06:49 AM
  5. std::getline size limit
    By dirkduck in forum C++ Programming
    Replies: 4
    Last Post: 07-23-2003, 03:11 PM