0

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?

Laurel
  • 5,965
  • 14
  • 31
  • 57
user1897937
  • 379
  • 1
  • 4
  • 9
  • If `scanf` fails to match it will leave the input in there. So next time `scanf` runs it will see the same invalid input and hence infinete loop. See the duplicate post for more details and suggested fixes. – kaylum Feb 16 '22 at 05:26
  • Worthwhile read: [Using fflush(stdin)](https://stackoverflow.com/questions/2979209/using-fflushstdin) – Retired Ninja Feb 16 '22 at 05:31

1 Answers1

0

Adding fgetc(stdin); before continue statement in line number 16 and 23 solves the issue.

updated code:

#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");
       fgetc(stdin);
       continue;
     }

    printf("Enter Column value\n");
    if(!scanf(" %d",&col))
     {
       printf("Invalid column value\n");
       fgetc(stdin);
       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();
}
user1897937
  • 379
  • 1
  • 4
  • 9