Thread: How-to not to Storing Array that is used to terminate

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    102

    How-to not to Storing Array that is used to terminate

    I was trying to get input from keyboard and place it in array. If the user input is -1, the program should end itself. This thing really work. The requirement i wanted is,

    • -1 is used to terminate<<----work

    • -1 will not be stored in array<<----not really



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(){
    	int num[]={};
    	int count=0;
    	double total=0;
    	int count2=1;
    	
    	printf("Input Value into Array\n");
    	
    	while(num[count]!=-1){
    		count++;
    		printf("value:");
    		scanf("%d", &num[count]);
    		total = total + num[count];
    		count2++;
    	}
    	printf("Total count: %d\n",count);
    	
    	printf("Value inserted: ");
    	for(count=1;count<count2;count++){
    	printf("%d  ",num[count]);	
    	}
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You're going to have to store the user input in a temporary variable then. Compare it to -1, and if it's true, exit the loop. Otherwise store the value in the array and keep looping.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... referring to message #1...

    Line 5... you have to give the array a size, C does not know how to expand an array as you stuff new things into it. You are writing data to memory you don't own and if it appears to work, that is just pure dumb luck. eg. int num[100];

    line 12... the while loop should exit on the maximum size set in Line 5... eg while (count < 100)... to prevent array overflows.

    line 13 to line 17 replace with...
    Code:
    printf("Enter value :");
    scanf("%d",&num[count]);
    if (num[count] == -1)
      break;
    count ++;
    Line22 ... should use your count2 variable ...
    Code:
    for (count2 = 0;count2 < count; count2++)
       printf("%d  ",num[count2]);
    Also, you should be aware that C arrays start at 0, not 1... thus an array[10] has valid index values from 0 to 9 ... not 1 to 10...

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    Thanks for your advice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-11-2011, 02:37 PM
  2. 2D array storing
    By anirban in forum C Programming
    Replies: 1
    Last Post: 07-22-2009, 07:16 PM
  3. Storing in an Array
    By peckitt99 in forum C++ Programming
    Replies: 24
    Last Post: 10-10-2006, 01:01 AM
  4. null struct to terminate array of structs?
    By eccles in forum C Programming
    Replies: 6
    Last Post: 01-24-2005, 06:27 AM
  5. storing a manipulated array in a new array
    By axon in forum C++ Programming
    Replies: 7
    Last Post: 03-04-2003, 01:27 PM