0
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

# define ListSize 100

typedef int DataType;
typedef struct {
DataType elem[ListSize];
int length;
}SqList;


SqList * createList(int n) 
{
    SqList *L;
    int i;
    L=(SqList *)malloc(sizeof(SqList));
    L->length=0;
    printf("Input the elements:\n");

    for (i=0;i<n;i++)
    {
        scanf("%d ",&(L->elem[i]));
        L->length++;
    }
    return L;
}


void display(SqList *L)
{
    int i;
    for (i = 0; i < L->length; i++)
    {
        printf("%d\t" , L->elem[i]);
        printf("\n");
    }
    
}

int insert(SqList *L, int mark, DataType x)//Insert element x into list in the position mark
{
    int i=0;
    if(mark>L->length||mark<0) 
    return 0;
    else
    {
        for (i=L->length-1;i>=mark;i--)
        L->elem[i+1] = L->elem[i]; //move elements
        L->elem[i+1]=x; //insert
        L->length++;
        return 1;
        
    }
}

void main()
{
    int num;
    int in,loc;
    SqList *List;
    printf("Input the number of elements:");
    scanf("%d",&num);
    List=createList(num);
    printf("\n The list is: \n");
    display(List);
    printf("Please input location and element to insert into the list: \n");
    scanf("%d %d",&loc,&in);

    insert(List,loc,in);
    display(List);
}

So my assignment was to create a sequential list and insert an element into the user typed position in the list. I came up with the above code but my scanf function is acting weird. It asks me to type in the number of elements I want in the list but for some reason it takes one more input that it should. And that input actually goes to the value of variable loc. I can't seem to understand why it's working like that.

Aether
  • 11
  • 2
  • Please [edit] your question and show the input you use, the actual output / behavior you get and the expacted output / behavior. – Bodo Mar 29 '21 at 17:09
  • is that because of `scanf("%d %d",&loc,&in); <- this makes input go to the value of variable loc? – user253751 Mar 29 '21 at 17:12
  • *Thou shalt not use `scanf` without checking the return value!* Never ever! If you check the return value you might find that the first calls after `scanf("%d",&num);` and `scanf("%d %d",&loc,&in);` will not succeed. – Gerhardh Mar 29 '21 at 18:01

1 Answers1

1

The problem is with the line scanf("%d ",&(L->elem[i])); in createList. Specifically, the trailing space in the format string "%d " is consuming the carriage return when you enter the first number and you must type something else for the first call to scanf to complete. So when you think you're entering the second element, you're actually still giving input to the first scanf call. For more details on spaces in scanf format string, see this answer.

How does C treat trailing spaces in scanf?

Simply remove the trailing space "%d" and it will work as you intended.

  • Rather than _consuming the carriage return_ we should say _consuming any amount of white space_. – Armali Mar 29 '21 at 17:24
  • I would suggest to move the space to the front of the string to consume some `\n` left over from `scanf("%d",&num);` and other function calls in `main`. – Gerhardh Mar 29 '21 at 18:03