在数字数组中键入一个字母

时间:2016-12-26 16:09:23

标签: c arrays

我有这个简单的项目,我应该创建4个数组并输入员工的数据,数组是:员工的ID,工资,减薪和净工资。我也应该打印平均工资,薪水最高的员工的ID#,以及最低工资的员工的ID#。

代码工作正常,除非我输入一个字母,然后它只是以一种奇怪的方式执行整个程序(没有格式化,随机数字)。

为什么?这封信被转换为整数,对吧?是什么导致它停止正常工作?

另外,你是否介意看我使用的编码风格并告诉我是否有办法改进它?

#include <stdio.h>

#define SIZE 100

int main(void){
    int n; /*Number of employees*/
    float average; /*Average of the employee's salary*/
    int max; /*Highest salary*/
    int min; /*Minimum cut*/
    int i;
    int employee_number = 1;; /*The number (index +1) of the employee*/
    float sum = 0; /*The sum of the employee's salary*/
    int num_highest; /*The number of the employee with the highest
    salary*/
    int num_lowest; /*The number of the employee with the lowest cut*/




int id_number[SIZE]; /*Array to hold employee's IDs in*/
float salary[SIZE]; /*Array for employee's salaries*/
float cuts[SIZE]; /*Array for salary cuts*/
float net_salary[SIZE]; /*Net salary after cuts*/

printf("Enter the number of employees\t");
scanf("%i", &n);

for(i = 0; i < n; i++){
    printf("Enter the ID of employee #%i\t", employee_number);
    scanf("%i", &id_number[i]);

    printf("Enter the salary of employee #%i\t", employee_number);
    scanf("%f", &salary[i]);


    printf("Enter the salary cut of employee #%i\t", employee_number);
    scanf("%f", &cuts[i]);


    employee_number++;
}


for(i = 0; i < n; i++){
    net_salary[i] = salary[i] - (salary[i] * cuts[i] * 1/100);
}

for(i = 0; i < n; i++){
    sum += net_salary[i];
}

average = sum/(float)n;
printf("The average salary is %.2f\n", average);


max = net_salary[0];
min = cuts[0];

for(i = 0; i < n; i++){
    if(net_salary[i] > max){
        max = net_salary[i];
        num_highest = i;
    }
    if(cuts[i] <= min){
        min = cuts[i];
        num_lowest = i;
    }
}


printf("Employee with ID # %i has the highest salary which is %i\n", id_number[num_highest], max);

printf("Employee with ID # %i has the lowest cut of %i and a base salary of %.2f", id_number[num_lowest], min, salary[num_lowest]);


}

3 个答案:

答案 0 :(得分:3)

您应该检查每次调用函数scanf返回的值。

如果此值小于该函数应从用户获取的输入参数的数量,则它已失败,您应该再次调用它。

答案 1 :(得分:2)

在编码风格方面,您可以使用struct来存储有关每个员工的信息,并以这种方式创建employees[]结构数组。这有助于更好地组织代码,并避免使用4单独的数组来存储有关员工的信息。

struct可能如下所示:

typedef struct {
    int id_number;
    float salary;
    float cuts;
    float net_salary;
} employee_t;

然后你可以像这样创建一个结构数组:

employee_t employees[n]; /* n is number of employees */

检查scanf()的回报也很好。

所以不要简单地做:

scanf("%i", &n);

要格外小心并将其更改为:

if (scanf("%i", &n) != 1) {
    /*exit program if true */
}

我写了一些代码来证明这些要点:

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

typedef struct {
    int id_number;
    float salary;
    float cuts;
    float net_salary;
} employee_t;

void get_number_employees(int *n);
void get_user_input(employee_t employees[], int n, float *average);
void print_results(employee_t employees[], int n, float average);
int highest_salary(employee_t employees[], int n);
int lowest_cut(employee_t employees[], int n);

int
main(void) {
    int n;
    float average;

    get_number_employees(&n);

    employee_t employees[n];

    get_user_input(employees, n, &average);

    print_results(employees, n, average);;

    return 0;
}

void
get_number_employees(int *n) {
    printf("Enter number of employees: ");
    if (scanf("%d", n) != 1) {
        printf("Invalid number.\n");
        exit(EXIT_FAILURE);
    }
}

void
print_results(employee_t employees[], int n, float average) {
    int high_sal, low_cut;

    high_sal = highest_salary(employees, n);
    low_cut = lowest_cut(employees, n);

    printf("The average salary is %f\n", average);

    printf("Employee with ID # %d has the highest salary which is %f\n", 
            employees[high_sal].id_number, employees[high_sal].net_salary);

    printf("Employee with ID # %d has the lowest cut of %f and a base salary of %f\n", 
            employees[low_cut].id_number, employees[low_cut].cuts, employees[low_cut].salary);
}

void
get_user_input(employee_t employees[], int n, float *average) {
    int i, employee_number = 1, sum = 0;

    for (i = 0; i < n; i++) {

        printf("Enter the ID of employee #%d: ", employee_number);
        if (scanf("%d", &(employees[i].id_number)) != 1) {
            printf("Invalid number.\n");
            exit(EXIT_FAILURE);
        }

        printf("Enter the salary of employee #%d: ", employee_number);
        if (scanf("%f", &(employees[i].salary)) != 1) {
            printf("Invalid salary.\n");
            exit(EXIT_FAILURE);
        }

        printf("Enter the salary cut of employee #%d: ", employee_number);
        if (scanf("%f", &(employees[i].cuts)) != 1) {
            printf("Invalid cuts.\n");
            exit(EXIT_FAILURE);
        }

        employees[i].net_salary = employees[i].salary - (employees[i].salary * employees[i].cuts * 1/100);

        sum += employees[i].net_salary;

        employee_number++;
    }

    *average = (1.0f * sum)/n;
}

int
highest_salary(employee_t employees[], int n) {
    float max;
    int i, idx;

    max = employees[0].net_salary;
    for (i = 1; i < n; i++) {
        if (employees[i].net_salary > max) {
            max = employees[i].net_salary;
            idx = i;
        }
    }
    return idx;
}

int
lowest_cut(employee_t employees[], int n) {
    float min;
    int i, idx;

    min = employees[0].cuts;
    for (i = 1; i < n; i++) {
        if (employees[i].cuts < min) {
            min = employees[i].cuts;
            idx = i;
        }
    }
    return idx;
}

答案 2 :(得分:0)

作为许多C和C ++函数的输入时,字母为charchar*char通常可以作为ASCII编码数字或整数值输出到控制台。

char可以很容易地在C中转换为int,因为它实际上被视为数值。例如,A变为41,B变为42。