Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_DEPRECATE
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <Windows.h>
- #include <math.h>
- #define pause system("pause")
- #define clrcmd system("CLS")
- // Define macros that are used later in the program to calculate federal tax and other tax
- #define FED_TAX 0.3;
- #define OTH_TAX 0.088;
- // Structs
- typedef struct NAME
- {
- char* firstName; // String for first name
- char middleInitial; // Character for middle initial
- char* lastName; // String for last name
- }NAME;
- typedef struct STRDATE
- {
- int month, day, year;
- }STRDATE;
- typedef struct PAYRECORD
- {
- int EmployeeID;
- NAME name;
- STRDATE payDate;
- float hoursWorked;
- float hourlyRate;
- float regPay;
- float otPay;
- float grossPay;
- float fedTax;
- float otherTax;
- float netPay;
- }PAYRECORD;
- // Function Prototypes
- // Display Functions
- void mainMenu();
- void displaySummary(PAYRECORD *payroll, int inputStubs);
- void addSpaces(int inputSpaces, int inputNum);
- // Input Functions
- void getUserChoice(int* ptrUserChoice);
- void getPayInfor(PAYRECORD *payroll, int inputStub);
- // Calculate Functions
- void calcPayroll(PAYRECORD *payroll, int inputStub);
- void calcRegPay(PAYRECORD *payroll, int inputStub);
- void calcOtPay(PAYRECORD *payroll, int inputStub);
- void calcGrossPay(PAYRECORD *payroll, int inputStub);
- void calcFedTax(PAYRECORD *payroll, int inputStub);
- void calcOtherTax(PAYRECORD *payroll, int inputStub);
- void calcNetPay(PAYRECORD *payroll, int inputStub);
- void main()
- {
- ShowWindow(GetConsoleWindow(), SW_MAXIMIZE); // Maximizes console window
- // Initialize variables
- int userChoice = 0, stub = 0;
- PAYRECORD payroll[500];
- mainMenu(); // Prints the main menu
- do { // The loop that runs the menu detection system
- getUserChoice(&userChoice); // Calls the function getUserChoice with the input of &userChoice so that it can receive the pointer from the function
- switch (userChoice) { // Switch statement based on what the user has entered
- case 1: // Enter new paycheck information
- if (stub == 499) // Stop if there are 500 stubs
- {
- printf("\nMax stubs inputted.\n");
- system("pause");
- mainMenu();
- break;
- }
- getPayInfor(payroll, stub); // Call the function getPayInfor with the input of the 2D array positions so that they can receive the pointer from the function
- calcPayroll(payroll, stub);
- stub++; // Increase the stub
- mainMenu(); // Resets the menu
- break;
- case 2: // Display Payroll Summary for all data saved
- displaySummary(payroll, stub); // Calls the displaySummary function with the input of the payroll and stub so that the table can be displayed with all of the necessary information
- mainMenu(); // Resets the menu
- break;
- case 3: // Quit
- break;
- case '\n': // Don't trigger default if you press enter, prevents the default from catching when swapping from menus
- break;
- default: // Catch anything else incase the wrong letter is typed
- printf("\nWrong Number\n"); // Error message
- system("pause"); // Waiting for user to realize their mistake
- break;
- }
- } while (userChoice != 3); // Ends the do-while loop with the exit being 3 for quit, since you want the loop to end when the user would like to quit
- }
- void getUserChoice(int* ptrUserChoice) // Function that points the user's input back to the called variable for which menu they would like to go in to
- {
- scanf_s(" %d", &*ptrUserChoice); // point the input from the user back to the input
- }
- void mainMenu() // Base menu, clears the console and outputs all of the menu choices
- {
- clrcmd; // Clears the window
- printf("\n *** PAYROLL MAIN MENU *** \n"
- "\n 1. Enter new paycheck information \n"
- "\n 2. Display Payroll Summary for all data saved \n"
- "\n 3. Quit \n\n");
- }
- void getPayInfor(PAYRECORD* payroll, int inputStub) // Function that points back the user's inputs for the employee information
- {
- fflush(stdin); // Flush the standard input
- clrcmd; // Clears the window
- // Get the first name
- printf("\n\n Input the employee's First Name: ");
- //fgets(payroll[inputStub].name.firstName, sizeof(payroll[inputStub].name.firstName), stdin);
- // Get the middle initial
- printf("\n\n Input the employee's Middle Initial: ");
- //scanf_s("%c", payroll[inputStub].name.middleInitial);
- // Get the last name
- printf("\n\n Input the employee's Last Name: ");
- //fgets(payroll[inputStub].name.lastName, sizeof(payroll[inputStub].name.firstName), stdin);
- // Get the employee number
- printf("\n\n Input the employee number: ");
- scanf_s(" %f", payroll[inputStub].EmployeeID);
- // Get the hours worked
- printf("\n\n Input the amount of hours worked: ");
- scanf_s(" %f", payroll[inputStub].hoursWorked);
- // Get the hourly rate
- printf("\n\n Input the hourly rate: ");
- scanf_s(" %f", payroll[inputStub].hourlyRate);
- }
- void displaySummary(PAYRECORD* payroll, int inputStubs) // Function that displays the summary table for the payroll
- {
- clrcmd; // Clears the console
- printf("\n" // Prints Heading
- "Employee Number"
- " Hourly Rate"
- " Hours Worked"
- " Regular Pay"
- " Overtime Pay"
- " Gross Pay"
- " Federal Tax"
- " Other Tax"
- " Net Pay\n");
- for (int curStub = 0; curStub < inputStubs; curStub++) // Prints each row for each stub in payroll
- {
- // Begin to print each value in the column for the current row
- printf("\n");
- printf("%.0f", payroll[curStub].EmployeeID); // Display Employee Number
- addSpaces(23, payroll[curStub].EmployeeID); // The function addSpaces gives enough spaces to evenly space out all values so that nothing is misaligned
- printf("$%.2f", payroll[curStub].hourlyRate); // Display Hourly Pay
- addSpaces(15, payroll[curStub].hourlyRate);
- printf("%.2f", payroll[curStub].hoursWorked); // Display Hours Worked
- addSpaces(17, payroll[curStub].hoursWorked);
- printf("$%.2f", payroll[curStub].regPay); // Display Reg Pay
- addSpaces(15, payroll[curStub].regPay);
- printf("$%.2f", payroll[curStub].otPay); // Display OT Pay
- addSpaces(16, payroll[curStub].otPay);
- printf("$%.2f", payroll[curStub].grossPay); // Display Gross Pay
- addSpaces(13, payroll[curStub].grossPay);
- printf("$%.2f", payroll[curStub].fedTax); // Display Fed Tax
- addSpaces(15, payroll[curStub].fedTax);
- printf("$%.2f", payroll[curStub].otherTax); // Display Other Tax
- addSpaces(13, payroll[curStub].otherTax);
- printf("$%.2f", payroll[curStub].netPay); // Display Net Pay
- }
- printf("\n");
- pause;
- }
- void addSpaces(int inputSpaces, int inputNum) /* Function that adds spaces based on the length of each value in the table
- I had to make this function instead of using /t or %10.2f in my printf because:
- If the values were too large than the tabs would not space properly
- and %10.2 would space the number 10 times between the number and the $ which I did not want. */
- {
- int tempLength = floor(log10(abs(inputNum))) + 1; /* Outputs the digits from inputNum (the values from the table). Example would be inputNum = 1000, tempLength = 4.
- I tried using other methods, but they weren't working so I decided to do some googling on how to do it.
- Found this off of stackoverflow, from what I can understand the purpose of each command is:
- - the floor rounds the value to lowest whole number
- - log10 specifies that it is in base 10
- - the abs takes the absolute value of the inputNum
- - + 1 because the log 10 is (n-1), so you have to fix that with a + 1
- All of this gives how many digits there are in inputNum, which is needed to calculate how many spaces are required.
- This does not account for decimals and it does not need to because at most each value will go to 2 decimal places.
- https://stackoverflow.com/questions/3068397/finding-the-length-of-an-integer-in-c
- https://stackoverflow.com/questions/24176789/how-does-using-log10-correctly-calculate-the-length-of-a-integer
- */
- if (inputNum == 0) // Run this code if inputNum is 0, because if it is than tempLength is a large value that breaks everything
- {
- for (int spaces = 0; spaces < inputSpaces - 1; spaces++)
- {
- printf(" ");
- }
- }
- else // Run this code if inputNum is not 0 since tempLength will be a proper value then
- {
- for (int spaces = 0; spaces < (inputSpaces - tempLength); spaces++)
- {
- printf(" ");
- }
- }
- }
- void calcPayroll(PAYRECORD* payroll, int inputStub)
- {
- calcRegPay(payroll, inputStub);
- calcOtPay(payroll, inputStub);
- calcGrossPay(payroll, inputStub);
- calcFedTax(payroll, inputStub);
- calcOtherTax(payroll, inputStub);
- calcNetPay(payroll, inputStub);
- }
- void calcRegPay(PAYRECORD* payroll, int inputStub) // Function that calculates regPay and points it back to the variable that called it
- {
- if (payroll[inputStub].hoursWorked <= 40)
- {
- payroll[inputStub].regPay = payroll[inputStub].hoursWorked * payroll[inputStub].hourlyRate;
- }
- else if (payroll[inputStub].hoursWorked > 40)
- {
- payroll[inputStub].regPay = 40 * payroll[inputStub].hourlyRate;
- }
- }
- void calcOtPay(PAYRECORD* payroll, int inputStub) // Function that calculates otPay and points it back to the variable that called it
- {
- if (payroll[inputStub].hoursWorked <= 40)
- {
- payroll[inputStub].otPay = 0;
- return;
- }
- else
- {
- payroll[inputStub].otPay = (payroll[inputStub].hoursWorked - 40) * (payroll[inputStub].hourlyRate * 1.5);
- }
- }
- void calcGrossPay(PAYRECORD* payroll, int inputStub)// Function that calculates grossPay and points it back to the variable that called it
- {
- payroll[inputStub].grossPay = payroll[inputStub].regPay + payroll[inputStub].otPay;
- }
- void calcFedTax(PAYRECORD* payroll, int inputStub) // Function that calculates fedTax and points it back to the variable that called it
- {
- payroll[inputStub].fedTax = payroll[inputStub].grossPay * FED_TAX;
- }
- void calcOtherTax(PAYRECORD* payroll, int inputStub) // Function that calculates otherTax and points it back to the variable that called it
- {
- payroll[inputStub].otherTax = payroll[inputStub].grossPay * OTH_TAX;
- }
- void calcNetPay(PAYRECORD* payroll, int inputStub) // Function that calculates netPay and points it back to the variable that called it
- {
- payroll[inputStub].netPay = payroll[inputStub].grossPay - payroll[inputStub].fedTax - payroll[inputStub].otherTax;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement