I am trying to read user input row and column value and use it for set operation. The user can enter values until the row and column values are -1 and -1. When I gave input as an integer the logic works fine but if I give input other than integers it is entering into an infinite loop.
#include<stdio.h>
int user_pattern()
{
int temp_arr[500];
//int len = strlen(temp_arr);
int row = -1, col = -1;
static int index = 0;
while(1)
{
printf("Enter Row value");
int ret = scanf(" %d",&row);
if (!ret)
{
printf("Enter Row value range from 0 to 6\n");
fflush(stdin);
continue;
}
printf("Enter Column value\n");
if(!scanf(" %d",&col))
{
printf("Invalid column value\n");
continue;
}
printf("set -1 in row column to quit\n");
printf("Row:%d col:%d \n",row,col);
if(row == -1 || col == -1)
break;
if(row < 7)
{
temp_arr[index] = row;
temp_arr[index+1] = col;
index = index+2;
}
for(int set = 0; set < index; set = set+2)
{
printf("Enter set_user_pattern\n");
//set_user_pattern(temp_arr[set],temp_arr[set+1]);
}
}
return 0;
}
int main()
{
user_pattern();
}
I gave input as @ for the row then I am getting the output below infinite times. I expected it to take the next input from scanf. I tried adding space in scanf before %d but it's no use.
Enter Row value range from 0 to 6
Enter Row valueret value:0
Enter Row value range from 0 to 6
Enter Row valueret value:0
Enter Row value range from 0 to 6
Enter Row valueret value:0
Enter Row value range from 0 to 6
Enter Row valueret value:0
Enter Row value range from 0 to 6
Enter Row valueret value:0
Enter Row value range from 0 to 6
Enter Row valueret value:0
Enter Row value range from 0 to 6
Enter Row valueret value:0
Enter Row value range from 0 to 6
Enter Row valueret value:0
Enter Row value range from 0 to 6
Enter Row valueret value:0
Enter Row value range from 0 to 6
Enter Row valueret value:0
Enter Row value range from
What is wrong in the code?