testing if input is a number

This is a discussion on testing if input is a number within the C Programming forums, part of the General Programming Boards category; hey guys so this program works by putting a number which denotes how much input is expected so lets just ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    41

    testing if input is a number

    hey guys so this program works by putting a number which denotes how much input is expected so lets just say i first input 5, so then i input 5 numbers like 10 20 30 40 50 and it computes the average of the 5. how do i test input of the expected and the other values as numbers? if i input any character it should display an error(havent implemented) and quit.
    at first i used the ACSII values but they dont work and the statement to test it makes any input false. im not that much good in C and i just started so please give me some slack
    here is the code:

    Code:
    #include <stdio.h>
    
    
    int main()
    {
      int expected;
      int count;
      double sum;
      int value;
    
    
            while (scanf("%d", &expected) != EOF)
            {
                    sum = 0;
                    for (count = 0; count < expected; count++)
                    {
                            scanf("%d", &value);
                            sum += value;
                    }
                    printf("Average of %d values is %.2f\n",
                           count, count != 0 ? sum / count : 0.0);
            }
      
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Look up scanf() in your C Library Documentation ... pay particular attention to the retrun value.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    is there a specific documentation for it? google gives me several different ones.
    i have a fixed code. it performs almost what i want to do but it goes into an infinite loop
    Code:
    #include <stdio.h>
    #include "tfdef.h"
    
    
    int main()
    {
       int expected;
       int  count;
       int  value;
            int flag1, flag2;
       double sum;
    
    
            while (flag1 = scanf("%d", &expected) != EOF)
            {
                    if (flag1==FALSE)
                    {
                            printf("Error! Can't read expected values.\n");
                            break;
                            return;
                    }
                    else
                    {
                    sum=0;
                    for (count=0; count<expected; count++)
                    {
                    flag2 = scanf("%d", &value);
                    if (flag2 == FALSE)
                    {
                            printf("Error!\n");
                            break;
                            return;
                    }
                    else
                    sum += value;
                    }
                    }
                    printf("Averager of %d values is %.2f\n",
                            count, count != 0 ? sum/count : 0.0);
            }
    }

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ecsx00 View Post
    is there a specific documentation for it? google gives me several different ones.
    i have a fixed code. it performs almost what i want to do but it goes into an infinite loop
    Read THIS thread.

    (Hint: you should read more than your own threads on this forum.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing User Input
    By dolfaniss in forum C Programming
    Replies: 9
    Last Post: 02-15-2011, 01:27 PM
  2. Replies: 1
    Last Post: 09-22-2008, 02:38 PM
  3. Testing user input question?
    By Hoser83 in forum C Programming
    Replies: 18
    Last Post: 02-15-2006, 01:18 PM
  4. Testing Random Number Generator
    By Roaring_Tiger in forum C Programming
    Replies: 7
    Last Post: 08-12-2005, 01:48 AM
  5. testing if a number is a float
    By UnknownImage in forum C Programming
    Replies: 11
    Last Post: 02-13-2003, 11:10 AM

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