做......而在C中使用getche()

时间:2012-11-23 16:45:33

标签: c

在我的程序中的以下代码中:

do
    {
        printf("\nEnter records of student %d: \n", i++);
        printf("Name: ");
        fgets(record.name, sizeof(record.name), stdin);
        printf("\nAddress: ");
        fgets(record.address, sizeof(record.address), stdin);
        printf("\nClass Level: ");
        scanf("%d", &record.classlevel);
        printf("\nTelephone Number: ");
        scanf("%ld",&record.telephone);
        fwrite(&record, sizeof(record),1,fptr);
        printf("\n\nAdd another record? [y/n]: ");


    }while(getche()=='y');

当我看到输出时,它第一次正常工作

  

输入学生1的记录

     

姓名:< some input>

     

地址:< some input>

     

班级:< some input>

     

电话号码:< some input>

     

添加另一条记录? [y / n]:<按Y>

     

输入学生2的记录

     

姓名:<没有光标,这里不能提供输入>

     

地址:<只能在这里输入>

     

     

     

.....等..

发生了什么,为什么我在do ... while循环中第一次迭代后无法输入Name。 getche()是否有任何问题?

希望你的帮助

先谢谢

1 个答案:

答案 0 :(得分:1)

您需要在fflush(stdin)开头do{...}while,因为getche()会在输入缓冲区中留下换行符。