Thread: Question

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

    Question

    I am writing a program that has the user enter 10 numbers and I am storing them in an array. If the user enters something other then a number I just accept it and store it in the array. All this works fine so far. What I am trying to do is tell the user at the end how many values they entered that were actual numbers and how many were not. Is there a way to do this? I have figured everything out but this part. Let me know if it helps for me to post the code I have so far?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm assuming you're reading into an array of strings (a multi dimensional array, or an array of pointers to which you're allocating space). If so, use something like atoi() to see if you're dealing with a number.

    If not, then just check each character or digit in your input and see if it's a number. If it isn't, they haven't entered a valid "number".

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

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    Here is the code I have so far. Am I even going about this the right way?

    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 10 numbers: \n");
        for (i=0; i<SIZE; ++i)
          scanf("%d",&arr[i]);
    
    	if (i<=0) {
    		exit(1);
    	}
    
    	SIZE=atoi(temp); /*convert char to int and verify*/
    		if (! verify (temp)) {
            printf("\nYou did not enter a number, This program will now exit\n");
    		getchar();
    		exit(0);
    		}

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Question

    >>If the user enters something other then a number I just accept it and store it in the array.
    That isn't what your code does.

    If scanf() doesn't read a valid number it will leave the bad input in the stream, and leave the contents of the target variable in an undefined state.

    >>SIZE=atoi(temp);
    You cannot assign anything to SIZE.

    Are you trying to input single digit numbers, or what. For example, if the users enters 10 numbers as this:
    0123456789
    you would need very different code to handle input like this:
    17 16 15 14 13 12 11 10 9 8
    Both are 10 numbers long, it's just how you interpret the question

    Anyway, always check the return code of scanf(), it'll tell you if you read in a number or not.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    But how do I check the return code of scanf?

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by ct26torr
    But how do I check the return code of scanf?
    The same way you check the return code from any function.

    Something like:

    Code:
    if (scanf("%d", &num) != 1)
    {
      puts ("Bad number");
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM