小编Git*_*man的帖子

get() 只在 while 循环中输入一次

我是 C 新手,正在做一些练习,但在 while 循环中使用 get() 时遇到问题。在搜索中,我相信它可能与 \n 字符有关,但我希望有人能够更彻底地解释这里发生的事情:

这个循环只会运行一次——它会第二次打印“输入姓氏”以进行筛选,然后在 get() 有机会再次接受任何输入之前退出循环:

while (employee_num <= 10)
{
    printf("Enter last name ");
    gets(employee[employee_num].last_name);
    if(strlen(employee[employee_num].last_name) == 0)
        break;
    printf("Enter first name ");
    gets(employee[employee_num].first_name);
    printf("Enter title ");
    gets(employee[employee_num].title);
    printf("Enter salary ");
    scanf("%d", &employee[employee_num].salary);        
    ++employee_num;
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

c gets

3
推荐指数
1
解决办法
8399
查看次数

标签 统计

c ×1

gets ×1