我正在尝试创建一个程序,要求用户仅用数字构建数组,并删除要求用户输入的特定元素
我的问题是,当我编译并运行代码时,它似乎卡在了我的scanf函数上。我已放置指示器以了解我的代码当前在哪里运行,如下面的程序所示
注意:我不能使用指针
#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]);
}
}