c - Unclear about stdin input issue -


i write piece of code myself understand how things work when stdin , stdout involved.

here code:

#include<stdio.h> #include<stdlib.h>  void prompt(){     int i=0;     printf("please select:\n");                  //string1     printf("1.input\n2.print\n3.exit\n");        //string2     scanf("%d",&i);     switch(i){         case 1:             printf("data input!\n");             break;         case 2:             printf("data printed!\n");             break;         case 3:             exit(0);         case 10:             printf("newline detected!");                      //string3         default:             printf("please input valid number!(1-3)");    //string4     } }     int main() {     while(1)         prompt();      return 0; } 

what expect code is:

  1. prompt me input;
  2. then enter number 4,which out of cases;
  3. so default case matched , string 'please input valid...'(string 4) printed.
  4. as there still newline character left in stdin buffer, in next loop, variable 'i' automatically newline character, 10 in acsii
  5. so after printing out 'please select..1.input\n2.print....',, string 'newline detected!'(string 3) printed out.
  6. and code goes third loop, , prompt me input....

but never happen. cannot see 'newline detected!" in output if enter number 4, out of cases.

anyone can elaborate how snippet work exactly?

by way: assume when there printed in stdout, stdin buffer flushed automatically. fact proved me wrong. assumption true or false?

also, when enter character(for example, g) rather number, got string 1, string 2, sring 4 printed in screen infinitly. why that?

#######################################################################3

edit: after viewing answer, make snippet, understand.

#include<stdio.h> #include<stdlib.h>  void output();  int main() {     int i;     printf("enter number here:\n");     scanf("%d",&i);      output();      return 0; }  void output(){     char a;     if (scanf("%c",&a)!=1){         printf("scanf error!!");         exit(1);     }     switch(a){         case 'a':             printf("an char entered");             break;         case 'b':             printf("an char b entered");             break;         default:             printf("%c",a);             printf("other thing entered");     } } 

whatever enter first time program prompt you, never second prompt. example, when program prompt first time, if enter number 4, newline , string "other thing entered" printed on screen. why that?

as there still newline character left in stdin buffer, in next loop, variable 'i' automatically newline character, 10 in acsii

so after printing out 'please select..1.input\n2.print....',, string 'newline detected!'(string 3) printed out.

that not correct. when use

scanf("%d",&i); 

all white spaces ignored, includes newline.

and code goes third loop, , prompt me input....

now know stays in second loop, waiting number entered.

i assume when there printed in stdout, stdin buffer flushed automatically. fact proved me wrong. assumption true or false?

that assumption false. stdout flushed when wait input stdin.

also, when enter character(for example, g) rather number, got string 1, string 2, sring 4 printed in screen infinitly. why that?

that's because program not able read character when executes line:

scanf("%d",&i); 

the character stays in input stream. don't have code remove character input stream. makes program stay in infinite loop.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -