0

When I execute the below code the for loop takes the input less number of times. I even used getchar() but the issue still did not resolve. Please provide the correct code for it

int main(void)
{
    struct node *head = NULL;
    int n;
    char c;
    int k;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        scanf("%c %d", &c,&k);
        if (c == 'a')
        {
            head = add(k, head);
        }
        else if (c == 'd')
        {
            head = del(k, head);
        }``
    }
    printstruct(head);
    return 0;
}
imk
  • 133
  • 6
  • If that doesn't answer your question please explain why not and then give the exact input, expected result and actual result. Also, always check the return value of `scanf` before using any of the values it may (or may not) have read in. – kaylum Oct 29 '21 at 04:33
  • ALWAYS check the return of `scanf()` -- otherwise you will miss a *Matching Failure* and invite *Undefined Behavior*. Try adding a `' '` before `" %c...` and see if it helps... – David C. Rankin Oct 29 '21 at 04:47

0 Answers0