jdoecs420
jdoecs420

Reputation: 33

Cannot continue after scanf function

I am trying to create a program that asks user to build an array with numbers only, and also to removes a specific element asked to the user

My problem is that when I compile and run the code, it seems to get stucked at my scanf function. I have placed indicators to know where my code is currently running at as shown in my program below

note: i cannot use pointers

    #include <stdio.h>


    #define MAX_LEN 5



    void remover(int list[], int item)
    {
        int temp[MAX_LEN] = {'e', 'e', 'e', 'e', 'e'}; //e stands for EMPTY
        int i, j, k;
        i = j = k = 0;
        for(i = 0; i < MAX_LEN ;  i++)
        {
            if(list[i] != item) //if array index doesnt match with item to be removed
            {                   
                temp[j++]=list[i]; //copy element to temporary array
            }
        }
        for(k; k<MAX_LEN; k++) //copy TEMP into LIST
        {
            list[k] = temp[k];
        }
    }

    void add(int list[], int item, int nth)
    {
        printf("\nentering add func listing");
        list[nth] = item;
    }

    int main()
    {
        int list[MAX_LEN];
        int item_number, remove_number;
        int inputFlag = 1;
        int i;
        putchar('\n');
        printf("\n------------------------------------------------------");
        printf("\n-------___Ordered List Array Implementation____-------");
        printf("\n------------------------------------------------------");
        printf("\nEnter 5 elements to be filled in:                     ");

        for( i = 0; i<6 && inputFlag; i++)
        {

            printf("\nEnter item number %d in list\t", i+1);
            scanf("%d\n", item_number); //I have tried removing the trailing \n after %d but still gives the same problem
            printf("\n..Done scanning input"); //PROGRAM CANNOT CONTINUE HERE

            if(item_number != sizeof(int))
            {
                printf("\nPlease input integers. Terminating...");
                exit(0);
            }
            add(list, item_number, i);
            printf("\nAdded to add func");
        }

        printf("\nShowing index of list.. ");
        for(int j=0; j<i; j++)
            printf("[%d] ==> %d", j, list[j]);

        printf("\n------------------------------------------");
        printf("\n__________________________________________");
        printf("\nEnter item to be removed:                 ");
        scanf("%d\n", remove_number);
        remover(list, remove_number);
        printf("\nNew list with item ' %d ' removed", remove_number);
        putchar('\n');
        for(int m = 0; m < MAX_LEN; m++)
        {
            if(list[m] == sizeof(int))
                printf("\n[%d] ==> %d", m, list[m]);
            if(list[m] == sizeof(char))
                printf("\n[%d] ==> %c", m, list[m]);
        }
    }

Upvotes: 1

Views: 806

Answers (1)

R4444
R4444

Reputation: 2114

You forgot to put & in your scanf call.

For e.g. you should try:

scanf("%d\n", &item_number);

and it should work fine.

Checkout this article for more information on scanf.

scanf or scan formatted string, requires you the location (address) of the variable in which you want to store your value.

Checkout this question for more information.

Upvotes: 4

Related Questions