MichaelX
MichaelX

Reputation: 202

Typing a letter into a number array

I have this simple project where I'm supposed to create 4 arrays and enter employee's data into, the arrays are: The employee's ID, their salary, the salary cuts, and the net salary. I am also supposed to print the average salary, the ID # of the employee with the highest salary, and the ID # of the employee with the lowest cut.

The code works fine, except if I enter a letter, then it just executes the whole program in a weird way (no formatting, random numbers).

Why is that? The letter gets converted to an integer, right? What causes it to stop working properly?

Also, would you mind looking at the coding style I used and telling me if there's a way I can improve it?

#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]);


}

Upvotes: 1

Views: 114

Answers (3)

RoadRunner
RoadRunner

Reputation: 26315

In terms of coding style, you can use a struct to store information about each employee, and create an employees[] array of structs that way. This helps in organizing your code better, and avoids having to use 4 separate arrays to store information about the employees.

The struct can look like this:

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

And then you can create an array of structs like this:

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

It is also good to check the return of scanf().

So instead of doing simply:

scanf("%i", &n);

Be extra careful and change this to:

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

I wrote some code to demonstrate these points:

#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;
}

Upvotes: 2

Aaron3468
Aaron3468

Reputation: 1794

Letters are char or char* when taken as input for many C and C++ functions. char can generally be output to the console as an ASCII encoded digit or as an integer value.

char can be cast into int very easily in C because it's actually considered a numeric value. For example, A becomes 41, and B becomes 42.

Upvotes: 0

barak manos
barak manos

Reputation: 30136

You should check the value returned by every call to function scanf.

If this value is smaller than the number of input arguments that the function is supposed to obtain from the user, then it has failed and you should call it again.

Upvotes: 3

Related Questions