
C plus plus free project for Beginners.
Learn how to create a C plus plus projects from scratch with this step-by-step tutorial. We cover everything you need to know, from setting up a development environment to writing and debugging code. Perfect for beginners and experienced C++ developers alike.
- Simple Calculator
- Salary Slip
- project 3
- project 4
Simple Calculator:
In this project we perform simple command-line calculator does mathematical operations on two operands. It starts by including several standard library headers and then defines the main function, which takes two arguments: the number of command line arguments and an array of strings containing the command line arguments. The code checks that there are 4 arguments (1 for the operator and 1 each for the two operands). If this is not the case, it prints a usage message and returns 1 to indicate an error. If the operator is none of these, the code prints an error message and returns 1. Finally, the code returns 0 to indicate that the program ran successfully.
#include <iostream>
#include <string>
#include <cmath>
int main(int argc, char* argv[])
{
// Check the command line arguments
if (argc != 4)
{
std::cout << "Usage: calc [operator] operand1 operand2" << std::endl;
return 1;
}
// Parse the operands
double operand1 = std::stod(argv[2]);
double operand2 = std::stod(argv[3]);
// Perform the specified operation
std::string operator_ = argv[1];
if (operator_ == "+")
{
std::cout << operand1 + operand2 << std::endl;
}
else if (operator_ == "-")
{
std::cout << operand1 - operand2 << std::endl;
}
else if (operator_ == "*")
{
std::cout << operand1 * operand2 << std::endl;
}
else if (operator_ == "/")
{
if (operand2 == 0)
{
std::cout << "Error: Division by zero" << std::endl;
return 1;
}
std::cout << operand1 / operand2 << std::endl;
}
else if (operator_ == "^")
{
std::cout << std::pow(operand1, operand2) << std::endl;
}
else
{
std::cout << "Error: Invalid operator" << std::endl;
return 1;
}
return 0;
}
Simple Salary Slip project:
This code is a program that calculates and prints a salary slip for an employee. It starts by including several standard library headers and defining a struct type called Employee to represent an employee’s data. The Employee struct has several fields. The main function then reads the employee data from the standard input using cin and getline. Sets the employee’s daily_rate, pf, travel_allowance, insurance, and incentive fields using hard-coded values. Calculates the gross salary by multiplying the days_present and daily_rate fields. Then calculates the net salary by adding the gross salary, travel_allowance, and incentive fields and subtracting the total deductions. Finally, it prints the salary slip using the cout stream and the setprecision and fixed manipulators to format the numbers with 2 decimal places. The program returns 0 to indicate that it ran successfully.
#include <iostream>
#include <string>
#include <iomanip>
struct Employee
{
std::string name;
int id;
int days_present;
double daily_rate;
double pf;
double travel_allowance;
double insurance;
double incentive;
};
int main()
{
// Read the employee data from the standard input
Employee employee;
std::cout << "Enter employee name: ";
std::getline(std::cin, employee.name);
std::cout << "Enter employee ID: ";
std::cin >> employee.id;
std::cout << "Enter number of days present in the month: ";
std::cin >> employee.days_present;
// Set the employee data
employee.daily_rate = 1000;
employee.pf = 0.1;
employee.travel_allowance = 500;
employee.insurance = 0.2;
employee.incentive = 1000;
// Calculate the gross salary and deductions
double gross_salary = employee.days_present * employee.daily_rate;
double pf_deduction = gross_salary * employee.pf;
double insurance_deduction = gross_salary * employee.insurance;
double total_deductions = pf_deduction + insurance_deduction;
// Calculate the net salary
double net_salary = gross_salary + employee.travel_allowance + employee.incentive - total_deductions;
// Print the salary slip
std::cout << std::endl;
std::cout << "Salary Slip" << std::endl;
std::cout << "------------" << std::endl;
std::cout << "Employee Name: " << employee.name << std::endl;
std::cout << "Employee ID: " << employee.id << std::endl;
std::cout << "Days Present: " << employee.days_present << std::endl;
std::cout << "Gross Salary: $" << std::setprecision(2) << std::fixed << gross_salary << std::endl;
std::cout << "PF Deduction: $" << std::setprecision(2) << std::fixed << pf_deduction << std::endl;
std::cout << "Insurance Deduction: $" << std::setprecision(2) << std::fixed << insurance_deduction << std::endl;
std::cout << "Total Deductions: $" << std::setprecision(2) << std::fixed << total_deductions << std::endl;
std::cout << "Travel Allowance: $" << std::setprecision(2) << std::fixed << employee.travel_allowance << std::endl;
std::cout << "Incentive: $" << std::setprecision(2) << std::fixed << employee.incentive << std::endl;
std::cout << "Net Salary: $" << std::setprecision(2) << std::fixed << net_salary << std::endl;
return 0;
}
Do subscribe on YouTube for the tutorials on C plus plus projects.