mardi 4 août 2015

Trouble with a switch function menu using structs in C

The program compiles fine however there are a few issues that come up...

  1. The calculations will not print out properly.
  2. After making a selection the menu will run again automatically without asking for input.
  3. Edit employees does not overwrite current employee only adds another to the list.

I am including all my code since I am not sure where the bug is.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//This is a macro intended for use with the emplyName array.
#define SIZE 20 

//This struct has all the varibles that I will be using in my functions
typedef struct person
{
    char emplyName[SIZE];
    float emplyHours;
    float emplyRate;
    float emplyGross;
    float emplyBase;
    float emplyOvrt;
    float emplyTax;
    float emplyNet;
    float emplyTotal;
}input;


void menu(void);

void editEmployees(input* emply);

void print(input* emply);

void employeeInfo(input* emply);

void calculations(input *emply);

int main(void)
{
    struct person payroll[5] = { 0 };
    int choice = 0, currentEmployee = 0;
    calculations(&payroll);
    do
    {
        menu();
        scanf_s("%c", &choice, 1);
        switch (choice){
        case '1':{
            employeeInfo(&payroll[currentEmployee]);
            currentEmployee;
            break;
        }
        case '2':{
            editEmployees(&payroll[currentEmployee]);
            break;
        }
        case '3':{
            print(&payroll[currentEmployee]);
            break;
        }
        case '4':{
            printAll(payroll);
            break;
        }
        case '0':{
            break;
        }
        default:
            printf("Invalid entry\n");
        }
    } while (choice != 5);
        system("pause");

}

void employeeInfo(input *emply)
 {
        printf("Enter employee name.\n");
        scanf_s("%s", &emply->emplyName,SIZE);
        printf("Enter employee hours.\n");
        scanf_s("%f", &emply->emplyHours);
        printf("Enter Hourly rate.\n");
        scanf_s("%f", &emply->emplyRate);
} 



void calculations(input *emply)/*Write a method that calculates the gross, base and overtime pay, pass by reference.*/
{
    if (emply->emplyHours > 40) {
            emply->emplyOvrt = (emply->emplyHours - 40) * (emply->emplyRate * 1.5);
        }
        emply->emplyGross = (((emply->emplyHours)*(emply->emplyRate)) + emply->emplyOvrt);
        emply->emplyBase = (emply->emplyGross) - (emply->emplyOvrt);
        emply->emplyTax = ((emply->emplyGross)*.2);
        emply->emplyNet = (emply->emplyGross) - (emply->emplyTax);
        emply->emplyTotal += emply->emplyGross;



}


void print(input *employees)
{
    int i;
    for (i = 0; i < 5; i++)
    {
        printf("Employee Name:%s\n", employees[i].emplyName);
        printf("Hours Worked:%.2f\n ", employees[i].emplyHours);
        printf("Hourly Rate:%.2f\n", employees[i].emplyRate);
        printf("Gross Pay:%.2f\n", employees[i].emplyGross);
        printf("Base Pay:%.2f\n", employees[i].emplyBase);
        printf("Overtime Pay:%.2f\n", employees[i].emplyOvrt);
        printf("Taxes Paid:%.2f\n", employees[i].emplyTax);
        printf("Net Pay:%.2f\n", employees[i].emplyNet);
    }
    printf("Total paid to all employees : %.2f\n", employees[i].emplyTotal);
}

void printAll(input *emply)
{
    int i;
    for (i = 0; i < 5; i++)
    {
        if (strcmp(emply->emplyName[i], "-1") == 0){ 
            break;
        }
        printf("Employee Name:%s\n", emply[i].emplyName);
        printf("Hours Worked:%.2f\n ", emply[i].emplyHours);
        printf("Hourly Rate:%.2f\n", emply[i].emplyRate);
        printf("Gross Pay:%.2f\n", emply[i].emplyGross);
        printf("Base Pay:%.2f\n", emply[i].emplyBase);
        printf("Overtime Pay:%.2f\n", emply[i].emplyOvrt);
        printf("Taxes Paid:%.2f\n", emply[i].emplyTax);
        printf("Net Pay:%.2f\n", emply[i].emplyNet);
    }
    printf("Total paid to all employees : %.2f\n", emply[i].emplyTotal);
}

void editEmployees(input*emply)
{
    int edit;
    int i;
    printf("which employee would you like to edit?\n");
    for (i = 0; i < 5; i++){
        printf("%d.%s\n", i + 1, emply[i].emplyName);
    }
    scanf_s("%d", &edit);
    employeeInfo(&emply[edit]);
}

void menu(void)
{
        printf("Main Menu\n");
        printf("1. Add Employee\n");
        printf("2. Edit Employee\n");
        printf("3. Print Employee\n");
        printf("4. Print All Employees\n");
        printf("0. exit\n");
}

Aucun commentaire:

Enregistrer un commentaire