You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言程序While循环未执行求助:基本工资输入校验失效

问题分析与修复方案

你的代码存在几个核心逻辑错误,导致输入非整数时无法触发错误提示,甚至直接终止程序:

核心问题

  1. 未检查scanf的返回值
    当输入非整数时,scanf("%d", &s[i].salary)会读取失败,返回值为0(表示没有匹配到任何整数),但你的程序完全忽略了这个返回值,直接使用未初始化的s[i].salary(垃圾值)执行后续逻辑。

  2. isInt函数完全无效
    你把读取到的(可能是垃圾值的)整数转成字符串后再检查是否为整数,这个逻辑毫无意义——整数转成的字符串必然是合法数字格式,isInt永远返回true,导致输入无效时也会执行return直接终止整个程序。

  3. 错误使用return退出程序
    在while循环内的return会直接终止main函数,导致程序提前退出,无法继续录入后续员工信息。

  4. 未清空输入缓冲区
    scanf读取失败时,非法输入会留在缓冲区中,后续的输入操作会反复读取这些无效字符,导致程序陷入异常状态。

修复后的代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>

struct employee
{
    char name[100];
    char hdate[100];
    int salary;
};

int calc(int a);

int main(){ // 标准main函数返回类型应为int

    int j,i;
    char temp[100]; // 用于清空缓冲区的临时数组

    printf("enter number of employees: ");
    // 检查员工数量输入的合法性
    while (scanf("%d",&j) != 1 || j <= 0) {
        printf("Invalid input, enter a positive integer: ");
        // 清空缓冲区
        while (getchar() != '\n');
    }
    struct employee s[j];

    printf("\nEnter details of employees: \n\n");
    getchar(); // 清除输入j后的换行符
    for (i=0;i<j;i++){

        printf("Name: ");
        fgets(s[i].name,sizeof(s[i].name),stdin);
        s[i].name[strcspn(s[i].name, "\n")] = '\0';
    

        printf("Hire Date: ");
        fgets(s[i].hdate,sizeof(s[i].hdate),stdin);
        s[i].hdate[strcspn(s[i].hdate, "\n")] = '\0';

        printf("Base Salary: ");
        // 循环直到输入有效整数
        while (scanf("%d",&s[i].salary) != 1) {
            printf("Invalid, Enter a valid Input \nBase Salary: ");
            // 清空缓冲区中的非法字符
            while (getchar() != '\n');
        }
        // 清除输入salary后的换行符,避免影响下一次fgets
        while (getchar() != '\n');
    }

    printf("Employee and Salary table\n");
    printf("-----------------------------------------------------\n");
    printf("Name\t\t\tHire Date\t\t\tBase Salary\n\n");
    for (i = 0; i <j; i++)
    {
        printf("%s\t\t\t%s\t\t\t%d\n",s[i].name,s[i].hdate,s[i].salary);
        printf("\nCalculations: \n");
        calc(s[i].salary);
        printf("-----------------------------------------\n");
    }
    return 0; // 标准main函数需要返回值
}

int calc(int a){

    int hra,da,ta,gs,pf,it,ns;

    hra=(24*a)/100;
    da=(15*a)/100;ta=4000;gs=a+hra+da+ta;
    pf=(10*a)/100;it=(5*a)/100;
    ns=gs-pf-it;

    printf("\n\tBasic Salary: %d \n\tHRA: %d \n\tDA: %d \n\tTA: %d \n\tGross Salary: %d \n\tPF: %d \n\tIncome Tax: %d \n\tNet Salary: %d\n\n",a,hra,da,ta,gs,pf,it,ns);

    return 0;
}

关键修改说明

  • 检查scanf返回值:通过scanf("%d", ...) != 1判断输入是否为有效整数,因为scanf成功读取一个整数时返回1。
  • 清空输入缓冲区:每次输入失败后,用while (getchar() != '\n')清空缓冲区中的所有非法字符,确保下一次输入能正常读取。
  • 移除无效的isInt函数:直接通过scanf的返回值验证输入合法性,无需多余的字符串转换检查。
  • 修正main函数的返回类型:标准C中main函数应返回int类型,最后需添加return 0;。
  • 修复员工数量输入的合法性检查:确保输入的员工数量是正整数,避免后续数组越界。
  • 调整循环逻辑:直接用while循环等待有效输入,无需额外的n变量控制循环。

内容的提问来源于stack exchange,提问作者Nipun Poswal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 16:53:07