Why doesn't getchar() wait for me to press enter after scanf()?

前端 未结 10 845
悲哀的现实
悲哀的现实 2020-11-22 16:22

I am learning C and I\'m using \"getchar()\" to stop the command windows so I can see the exercises am doing but it just doesn\'t work. heres a sample:

#incl         


        
10条回答
  •  执笔经年
    2020-11-22 17:09

    As mentioned already scanf leaves \n behind after reading user input.

    Soultion: add getchar() straight after scanf.

    That compensates scanf deficiency.

    i.e.

    int value; 
    printf("1. option 1.\n2. option 2.\n3. option 3.\n4. Exit\n\nMake an option: "); 
    scanf("%d", &value);
    getchar();
    switch (value) 
    

提交回复
热议问题