Q:

why my program Auto assume value for scanf, cause the program to loop automatic and skip the next scanf?

I try to write a code as a counter machine, which can prompt user to enter how many item they want to buy, and then prompt user , which and what quantity of item they want to buy.
Below is my unfinished script.
    #include<stdio.h>
    #include<string.h>
    #include <math.h>
    #include <stdlib.h>
    #include <conio.h>

    void menu();
    int repeat();
    float determine_price(char);
    float calc(float,int);
    void print_result(float);

    int main()
    {

        int qty,quantity,choice=1;
        char selection;
        float price,pay,item_price,payment;
        //char set;

        while(choice!=2){
            menu();

            int n=0;//line24
            char arry[n][2];
            printf("enter Number of Order\n");
            scanf("%d",&n);//input number of loop//line 27

            int x=0;
            while(x<n){//loop until all item defined;line30
                printf("enter Set and quantity:\n");
                scanf("%c %d",&selection,&qty);//input selection & quantity;line32

                price=determine_price(selection);//funct for return item price
                pay= calc(price,qty);//funct return calculated price

                arry[x][0]=selection;//put into array, for further use later 
                arry[x][1]=qty;//put into array, for further use later
                print_result(pay);//print price result 

                x++;//counter
            }

            choice = repeat();//prompt user either to end or re run program;line 46

       }

    return 0;
    }

    void menu()// return string for menu
    {
        printf("**************************\n");
        printf("|AZRI & YANA'S CAKE HOUSE|\n");
        printf("**************************\n");
        printf("\n");
        printf("***************************\n");
        printf("|1 CUP-------------RM3.50 |\n");
        printf("|SET A(3 CUPS)-----RM10.00|\n");
        printf("|SET B(6 CUPS)-----RM20.00|\n");
        printf("|SET C(12 CUPS)----RM38.00|\n");
        printf("***************************\n");
        printf("\n");
    }
    void clr(){
        system("cls");
        //system("clear");
        //clrscr ();
    }
    int repeat(){
        int choicex;
        printf("\n\n\n\n");
        printf("Press\n[1] to return to menu\n[2] to end system\n");
        scanf("%d",&choicex);

        if(choicex==1){
            clr();
        }
        else{
            //choicex=2;
        }
        return choicex;
    }
    float determine_price(char selection){
        float setprice;

        if(selection=='1'){
            printf("1 cup = RM3.50\n");
            setprice=3.50;
        }
        else if(selection=='A'){
            printf("Set A = RM10.00\n");
            setprice=10.00;
        }
        else if(selection=='B'){
            printf("Set B = RM20.00\n");
            setprice=20.00;
        }
        else if(selection=='C'){
            printf("Set C = RM38.00\n");
            setprice=38.00;
        }
        else
            printf("set not available\n");

        return(setprice);
    }
    float calc(float item_pricing,int quantity){
        float answer;
        answer = item_pricing*quantity;
        return(answer);
    }
    void print_result(float payment){
        printf("total cost = RM%4.2f\n",payment);
    }

The problem is, when program execute n=2, it will prompt user for x=0,(line30) but for x=1, it will auto assume value as when x=0, cause it to ignore scanf in line 32
(I mark line 30, 32 in code).
If I enter n=3, it will also do same as n=2, but, it will stop at x=2 to prompt user for >scanf("%c %d",&selection,&qty);
but, however, it will start infinite loop after that, looping from line 24-line27, without enter while loop, and without execute line 46 too.
Can someone help me understand my program's problem?

A:

The First think you have to declare arry[n][2] after the user input a value for n. otherwise you will get a runtime Undefined behaviour.
You're getting an infinite loop because the buffer is not empty so you have to flush it.
If you check this code it will ignore the second scanf because the '\n' still in the buffer
int i;
char c;
printf("i >> ");
scanf("%d", &i);
scanf("%c", &c); // '\n' that remains in buffer is assigned to the c variable
printf("input is %d %d", i, c); // 2nd %d is to print character's ASCII Code

And the output goes <value of i> 10
the second number (10) refers to the ASCII code of the '\n'new line element.
scanf("%d",&n); // Line 27
while(getchar()!='\n'); // clearing the buffer
char arry[n][2]; // arry[n][2] must be declared after input a value for n
// ...
scanf("%c %d",&selection,&qty); // Line 32 + 1
while(getchar()!='\n'); // clearing the buffer again

