exit array

This is a discussion on exit array within the C Programming forums, part of the General Programming Boards category; I have this code I have been working with and I am trying to exit an array if someone enters ...

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

    exit array

    I have this code I have been working with and I am trying to exit an array if someone enters a zero. I have tried some different things but cant manage to make it work. Is it even possible to do? My code is below.

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h>
    #include <ctype.h>
    
    #define SIZE 10 /* size of array */
    
    
    int verify (char* value) /* this function will verify the input is a number*/
    {
      int iChars = strlen (value);
      int i;
    
      for (i=0; i<iChars; i++) {
        if (! isdigit (value [i])) {
          return 0; /*Not a number*/
        }
      return 1; /*Is a number*/
      }
    }
    
    
    int main()
    {
       int i, arr[SIZE], sum, average;
       char temp[50];
    
       /* get the numbers */
       printf("Enter up to 10 numbers, to stop entering numbers enter a zero. \n");
       for(i=0; i<SIZE; )
       {
          if(fgets(temp, sizeof(temp), stdin) == NULL) /*make sure there has been input*/
          {
             printf("Please enter a valid number\n");
             return 1;
          }
    
          if(!verify(temp))
          {
             printf("Invalid input, please try again\n");
          }
          else
          {
             arr[i] = atoi(temp); /*convert char to int */
             ++i; /* next number */
          }
       }
       return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I am trying to exit an array if someone enters a zero
    Do you mean you want to exit the loop, when someone enters zero? If so, just put an if test in the loop somewhere appropriate, and if its true, use the break keyword.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    cool .... thanks. I will give it a try

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    This is what I have but it doesn't seem to work.

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h>
    #include <ctype.h>
    
    #define SIZE 10 /* size of array */
    
    
    int verify (char* value) /* this function will verify the input is a number*/
    {
      int iChars = strlen (value);
      int i;
    
      for (i=0; i<iChars; i++) {
        if (! isdigit (value [i])) {
          return 0; /*Not a number*/
        }
      return 1; /*Is a number*/
      }
    }
    
    
    int main()
    {
       int i, arr[SIZE], sum, average;
       char temp[50];
    
       /* get the numbers */
       printf("Enter up to 10 numbers, to stop entering numbers enter a zero. \n");
       for(i=0; i<SIZE; )
       {
          if(fgets(temp, sizeof(temp), stdin) == NULL) /*make sure there has been input*/
          {
             printf("Please enter a valid number\n");
             return 1;
          }
    
          if(fgets(temp, sizeof(temp), stdin) == 0) /*exit if zero is entered*/
          {
             printf("Thank you. This program will now stop accepting input.\n");
             break;
          }
    
          if(!verify(temp)) /*check to see if input is a number*/
          {
             printf("Invalid input, please try again\n");
          }
          else
          {
             arr[i] = atoi(temp); /*convert char to int */
             ++i; /* next number */
          }
       }
       return 0;
    }

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    > if(fgets(temp, sizeof(temp), stdin) == 0) /*exit if zero is entered*/


    That'll stop if they enter a literal 0. 0 and '0' are different things. Also, you can't compare strings with ==.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Your verify() function is wrong. If you lay it out like this, you can see the problem easier (imho):
    Hint: Look at the return statements
    Code:
    int verify(char *value) /* this function will verify the input is a number*/
    {
      int iChars = strlen(value);
      int i;
    
      for (i = 0; i < iChars; i++)
      {
        if (!isdigit(value[i]))
        {
          return(0);  /*Not a number*/
        }
    
        return(1);    /*Is a number*/
      }
    }
    Now, this:
    >>if (fgets(temp, sizeof(temp), stdin) == 0)
    is good code, but isn't doing what you think it is. fgets() returns a pointer to the array, or NULL if there's an error (or EOF) during input. Don't change this section, leave it in to handle input errors.

    Now to the solution for a 0 being entered.

    A quick hack would be something like this:
    >>if (temp[0] == 0) /*exit if zero is entered*/
    but this wouldn't be very good if the user entered 099.

    Instead, you can wait until you've been through the verify() function and then test arr[i].
    So, after this line:
    >>arr[i] = atoi(temp); /*convert char to int */
    put this statement:
    >>if (arr[i] == 0)
    and then whatever you want.


    [edit]
    Govt: you're off base
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    Oh man now I have like 6 errors.

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h>
    #include <ctype.h>
    
    #define SIZE 10 /* size of array */
    
    
    int verify (char* value) /* this function will verify the input is a number*/
    {
      int iChars = strlen (value);
      int i;
    
      for (i=0; i<iChars; i++) {
        if (! isdigit (value [i])) {
          return 0; /*Not a number*/
        }
      return 1; /*Is a number*/
      }
    }
    
    
    int main()
    {
       int i, arr[SIZE], sum, average;
       char temp[50];
    
       /* get the numbers */
       printf("Enter up to 10 numbers, to stop entering numbers enter a zero. \n");
       for(i=0; i<SIZE; )
       {
          if(fgets(temp, sizeof(temp), stdin) == NULL) /*Handles input errors*/
          {
             printf("Inout invalid, please try again\n");
             return 1;
          }
    
    
          }
    
          if(!verify(temp)) /*check to see if input is a number*/
          {
             printf("Invalid input, please try again\n");
          }
          else
          {
             arr[i] = atoi(temp); /*convert char to int */
    		 if (arr[i] == 0) 
    			printf("Thank you. This program will now stop accepting input.\n");
    			break;
             ++i; /* next number */
          }
       }
       return 0;
    }

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You still haven't fixed the verify() function.

    Here, I've fixed the problems, mainly to do with braces in the wrong place. The code runs now, but it doesn't verify your numbers correctly. I shan't spoil your fun and tell you how to fix it, if you can't work it out, post again. Hint: newline.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define SIZE  10  /* size of array */
    
    /* */
    int verify(char *value) /* this function will verify the input is a number*/
    {
      int iChars = strlen(value);
      int i;
    
      for (i = 0; i < iChars; i++)
      {
        if (!isdigit(value[i]))
        {
          return(0);  /*Not a number*/
        }
      }
    
      return(1);      /*Is a number*/
    }
    
    /* */
    int main(void)
    {
      int   i, arr[SIZE], sum, average;
      char  temp[50];
    
      /* get the numbers */
      printf("Enter up to 10 numbers, to stop entering numbers enter a zero. \n");
      for (i = 0; i < SIZE;)
      {
        if (fgets(temp, sizeof(temp), stdin) == NULL) /*Handles input errors*/
        {
          printf("Inout invalid, please try again\n");
          return(1);
        }
        
        if (!verify(temp))      /*check to see if input is a number*/
        {
          printf("Invalid input, please try again\n");
        }
        else
        {
          arr[i] = atoi(temp);  /*convert char to int */
          if (arr[i] == 0)
          {
            printf("Thank you. This program will now stop accepting input.\n");
            break;
          }
          ++i;                  /* next number */
        }
      }
    
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    thanks for the help. I will see if I can make it work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 12:50 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Couple of Q's.....
    By oobootsy1 in forum C++ Programming
    Replies: 18
    Last Post: 02-23-2004, 01:03 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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