# testing if input is a number

• 11-11-2011
ecsx00
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);         }   }```
• 11-11-2011
CommonTater
Look up scanf() in your C Library Documentation ... pay particular attention to the retrun value.
• 11-11-2011
ecsx00
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);         } }```
• 11-11-2011
Tclausex
• 11-11-2011
CommonTater
Quote:

Originally Posted by ecsx00
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