Thread: so confused and frustrated

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

    so confused and frustrated

    Here is what I am doing. I have posted this a few times over the last few days and you guys have been great with help but I am just getting so frustrated.

    I am building an array that will take up to 10 numbers. If a user enters a zero I want to exit and stop taking numbers. If a user enters a letter I want to ignore it and ask for the next number. After the user enters all the numbers I want to give them the sum. I just can't make this work. I have made so many changes and tried so many things and yet I am getting more frustrated.

    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, 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 */
               if (!arr[i]) {
                 break;
               }
              ++i; /* next number */
           }
        }
        return 0;
     }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So what part exactly can't you do?

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

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    What about something like

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char theNums[10][50];
      int nums[10];
      int i, j, sum = 0; 
    	
      for(i=0; i < 10; i++)
      {
        printf("Enter up to 10 numbers or 0 to quit: ");
        fgets(theNums[i], 50, stdin);
    		
        j = strlen(theNums[i])-1;
    		
        theNums[i][j] = '\0'; /* get rid of '\n' */
    		
        if(theNums[i][0] == '0')
          break;
      else if(!isdigit(theNums[i][0]))
      {
        printf("%s is not a valid entry.\n", theNums[i]);
        nums[i] = 0;
      }
      else		
        nums[i] = atoi(theNums[i]);
      }
    	
      printf("\n\n");
    	
      for(j=0; j < i; j++)
        sum+=nums[j];
    
      printf("The sum of your numbers is %d", sum);
    	
      return 0;
    }
    Of course, you'd have to expand the test for valid digits beyond theNums[i][0] as a user could enter 2dfgt or something.

    I wonder if this would work.... or if it's valid... hmm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. frustrated beginner
    By JCK in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2002, 09:38 PM