CS50 in Arabic Session 2 Project: Employee Salary Calculator
Problem We Want to Solve
A small shop wants to automate salary calculation for employees.
Right now, they calculate salaries manually, which is slow and can lead to mistakes.
We will build a program that:
- Takes employee information
- Calculates salary based on working hours
- Supports overtime pay
- Prints a clean, formatted salary report
What You Will Build
A simple Salary Calculator System in C that behaves like a small HR tool.
Step-by-Step Development (Phases)
Phase 1: Input System
Goal:
Read employee data correctly.
Requirements:
- Ask for employee name
- Ask for working hours
- Ask for hourly rate
Expected Output Example:
Enter employee name: Ahmed
Enter working hours: 45
Enter hourly rate: 10
Phase 2: Basic Salary Calculation
Goal:
Calculate basic salary without overtime.
Rule:
salary = hours × rate
Expected Output Example:
Salary: 450
Phase 3: Overtime Pay
Rule:
- First 40 hours = normal rate
- Extra hours = 1.5 × hourly rate
Logic:
- If hours ≤ 40 → normal calculation
- If hours > 40 → split into normal + overtime
(40 * 10) + (5 * 10 * 1.5) = 400 + 75 = 475
Expected Output Example:
Salary: 475
Final Feature: Formatted Salary Report
Instead of printing random lines, format output like a real report:
Example Output:
===============================
SALARY REPORT
===============================
Employee Name : Ahmed
Working Hours : 45
Hourly Rate : 10
Basic Salary : 450.00
Overtime Pay : 25.00
-------------------------------
Final Salary : 475.00
===============================
Learning Goals
After this assignment, you should understand:
- How real systems are structured
- How to break a big problem into smaller phases
- How to collect data from the user using input
- How to store data inside suitable variables
- How to use data types such as
int,float, andstring - How to use conditions (
if / else) - How to review your code and debug errors
Bonus Challenge (Optional Extras)
If you want to go further:
- Add tax rule: If the salary is greater than 1000, deduct 10% tax, otherwise no tax applies.
- Add performance bonus: If the employee performs excellently or reaches the target, add extra money to the salary.
- Add attendance bonus: If the employee attends all workdays without absence or lateness, add an extra bonus.
Debugging Cheat Sheet
1. Missing CS50 Library
Problem: Program does not recognize get_string, get_float
Fix:
#include <cs50.h>
Check: If error says “implicit declaration”, this header is missing.
2. Wrong Data Types
Problem: Decimal values are incorrect or rounded
Fix:
float hours;
float rate;
Check: Salary involves decimals → use float, not int.
3. Overtime Logic Error
Problem: Overtime not calculated correctly
Correct Logic:
- First 40 hours = normal rate
- Extra hours = 1.5 × rate
Test Case:
- 45 hours, rate = 10
- Expected: (40 10) + (5 10 * 1.5) = 400 + 75 = 475
4. Missing else Case
Problem: Salary wrong for hours ≤ 40
Fix:
if (hours > 40)
else
Check: Always test with hours = 30 or 40.
5. Tax Mistake
Problem: Wrong or fixed tax value
Fix:
tax = salary * 0.10;
Check: Tax must depend on salary.
6. Forgetting Final Salary Calculation
Problem: Tax is calculated but not applied
Fix:
final_salary = salary - tax;
Check: Final salary must be lower than salary when tax applies.
7. Wrong Input Order
Problem: Inputs mixed up (hours, rate, name)
Correct Order:
- Name
- Hours
- Rate
8. Assignment vs Comparison Error
Problem:
if (hours = 40)
Fix:
if (hours == 40)
9. Print Statement Error
Problem: Values not shown
Fix:
printf("Salary: %.2f\n", salary);
Check: Always include format specifiers.
10. Floating Point Output
Problem: Long or messy decimals
Fix:
%.2f
Note
- Start with small inputs
- Calculate manually
- Compare with program output
- Isolate the wrong step
Session Recording
If you need to review any part of the explanation while working on your project, you can watch the full recording here: Watch the Session on YouTube
Remember: "Programming isn't just about writing lines of code; it's about the art of solving problems." Can't wait to see what you build!
Member discussion