2

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]);
        }
    }

R4444
  • 2,016
  • 2
  • 19
  • 30
jdoecs420
  • 35
  • 6
  • You need to use the address operator: scanf("%d\n", &remove_number); – PowerStat May 02 '19 at 06:02
  • `sizeof(int)` is (probably) 4, and `sizeof(char)` is (definitely) 1, so your input loop only accepts the number 4 as input, and your output loop would only print 1's (as an unprintable character) and 4's. Also, you loop for input six times, not five, which will cause undefined behaviour when you write outside `list`. – molbdnilo May 02 '19 at 08:19

1 Answers1

4

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.

R4444
  • 2,016
  • 2
  • 19
  • 30
  • It still doesnt eliminate the problem. – jdoecs420 May 02 '19 at 05:34
  • The program is working fine in my case. If you have other issues related to the program, then post another question. Chances are you'll get help very quickly. – R4444 May 02 '19 at 05:47
  • Compilations is succesful in my case. The problem is when I run the executable. I build with gcc filename.c -o filename and run filename on cmd. After entering a value, say, '2', the program just pauses and I have to terminate using Ctrl+C to exit. – jdoecs420 May 02 '19 at 05:49
  • 1
    Ok, you have to enter the number one more time and then press enter again. The problem is with your loop, not with scanf. – R4444 May 02 '19 at 06:16