2

A very simple question. Why scanf is skipped in the first while loop. I have tried it by using getchar() and the result is same. getchar is skipped.

If you guys dont understand what I'm talking about you can tried compile it and you guys will understand what i'm asking about.

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

typedef struct rec{
    int num;
    char pref[16];
    float point;
    struct rec* next;
}rec;

void dataInput(float*,char*);
rec* insertNode(rec*);

int main(){

    int i=1;
    rec* entr,*pt = NULL;
    entr = (rec*) malloc(sizeof(rec));
    entr->num = i;
    dataInput(&entr->point,entr->pref);
    entr->next = NULL;
    char key;
    i++;

    while(1){

        printf("Continue ? If YES press 'y',or NO press 'n'\n");
        key = getchar();
        if(key == 'n')break;
        else if(key == 'y'){
            if(!pt){
                pt = insertNode(entr);
            }else{
                pt = insertNode(pt);
            }
            dataInput(&pt->point,pt->pref);
            pt->num = i;
            i++;
            continue;
        }else{
            printf("Wrong key! Please Press again! \n");
        }

    }

    pt = entr;
    while(pt){

        printf("num : %d, pref :  %s, point: %.1f\n",
                pt->num,
                pt->pref,
                pt->point);
        pt = pt->next;
    }

    getchar();
    getchar();
    return 0;
}

void dataInput(float* point,char* pref){

    printf("Input Point\t : ");
    scanf("%f",point);

    printf("Input Pref\t : ");
    scanf("%s",pref);
}

rec* insertNode(rec* current){
    rec* newnode = (rec*)malloc(sizeof(rec));
    current->next = newnode;
    newnode->next = NULL;
    return newnode;
}

3 Answers 3

8

It's because scanf will leave a '\n' (endline) symbol in the input buffer. This symbol will be consumed by getchar at the first iteration of this while(1) loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes! it works!! if(key == '\n'){ key = getchar(); } after i added it. Thank you
6

getchar() leaves a newline character in the input buffer which is read by the subsequent scanf().

You can use solve this by using a leading space in the scanf:

scanf(" %c ...", &c,..);

which tells the scanf to ignore all whitespace characters. Or use another getchar() right after the first getchar() to consume the newline.

2 Comments

Omg this second option you suggested has been the ONLY WORKING OPTION for me. I tried everything mentioned in the nearly 15 similar questions. Adding a space before the %c didn't work, adding a \n after the %c didn't work, using fflush(stdin) didn't work, gets, getchar, and scanf all had the same problem... Ugh... Do you know why this is?
Could be due to any subsequent scanf/fgets calls etc. Can't tell without looking at the code. But I'd suggest reading this: c-faq.com/stdio/scanfprobs.html and avoid scanf() altogether.
1

Just use another getchar() after the scanf() to consume the newline character.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.