Is it possible to use scanf(“%d” &i) and use the first number inputted only, and nothing else?

后端 未结 2 1574
耶瑟儿~
耶瑟儿~ 2021-01-28 23:13

First off, I am not familiar with c at all. It would be great if you treated me like a total beginner, which I am.

So, the problem I have is that I don\'t seem to be ab

2条回答
  •  暖寄归人
    2021-01-28 23:50

    the program ends because of the character [enter] left in the input buffer.
    You give input value for type then for i and press [enter]. this [enter] is a character left in the input buffer which will be read by next

    scanf("%c",type);
    

    so the loop exits. Therefore use getchar() after

    int ret = scanf("%d", &i);
    

    To clear the input buffer. and the loop will not end unexpectedly.
    Make these changes,

     printf("\t Please input one ASCII code \n"); 
                 int ret = scanf("%d", &i);
                 getchar();          //this will read the [enter] character in input buffer
    /* My aim here is to get the amount of integers the user inputs,*/ 
    /* and use that to categorize, but I think I am failing to do so. */
                 if(ret==1){
    

提交回复
热议问题