int getvalue(int *arr_value, int number)
{
int i = 0;
int temp =0;
while(i < number)
{
if((scanf("%d",temp)) == 1) // to check whether input is integer
{
arr_value[i++] = temp; // to store it in array only if it is int
}
else
{
printf("\n Enter a proper integer: ");
continue;// go to the loop again to prompt value from user
}
}
for(i =0 ; i < number; i++)
{
printf("%d",arr_value[i]);
}
}
/* this program runs the enter integer line infinitely,
although I tried it with a continue statement.*/
Asked
Active
Viewed 36 times
0

Senjuti Mahapatra
- 2,570
- 4
- 27
- 38

user2670573
- 1
- 1
-
3Once the input fails, it will fail continuously. The input isn't acceptable as an integer — it stays unacceptable as an integer. You must do something to change the state of the input. Commonly, that is 'read and ignore characters up to the next newline (or EOF)': `int c; while ((c = getchar()) != EOF && c != '\n') ;`. – Jonathan Leffler Feb 14 '17 at 05:08
-
You cannot `scanf("%d"...)` a non-integer value. The function (`scanf`) will just return 0 (number of values scanned from `stdin`) when you do that. – barak manos Feb 14 '17 at 05:09
-
Replace `temp` with '&temp' inside `scanf`. Also as @barakmanos said, `scanf` returns the number of scanned values. Positive return value doesnt mean that the value scanned is an integer – Avantika Saini Feb 14 '17 at 09:34
-
Is there any other way to check whether the given input is integer ?? – user2670573 May 31 '17 at 05:28