Introduction

In this introductory lesson on C++, we will use a conditional statement to validate a condition and perform some simple calculations.

When you are creating a console or terminal application in C++, you can call some of the C functions to perform operations that would be difficult or problematic in C++. Some of those functions are used to format values for data display. In this exercise, we will call the printf() function to control the width to display a value and the number of decimal digits on a floating-point number.

Practical LearningPractical Learning: Creating the Application

  1. Start Microsoft Visual Studio
  2. In the Visual Studio 2022 dialog box, click Create a New Project
  3. In the Create a New Project dialog box, in the Languages combo box, select C++
  4. In the list of projects templates, click Console App
  5. Click Next
  6. In the Configure Your New Project dialog box, change the Project Name to PayrollEvaluation and set the Location of your choice
  7. Click Create
  8. Change the document as follows:
    #include <iostream>
    
    int main()
    {
        std::cout << "Payroll Evaluation\n";
        std::cout << "------------------------------------------";
        std::cout << "\nEnter the following pieces of information";
        std::cout << "\n------------------------------------------";
        std::cout << "\nEmployee Information";
        std::cout << "\n------------------------------------------";
        std::cout << "\nFirst Name:    ";
        char firstName[20];
        std::cin >> firstName;
        std::cout << "";
        std::cout << "Last Name:     ";
        char lastName[20];
        std::cin >> lastName;
        std::cout << "Hourly Salary: ";
        double hSalary;
        std::cin >> hSalary;
        std::cout << "------------------------------------------";
        std::cout << "\nTime worked";
        std::cout << "\n------------------------------------------\n";
    
        std::cout << "Monday:        ";
        double mon;
        std::cin >> mon;
    
        std::cout << "Tuesday:       ";
        double tue;
        std::cin >> tue;
    
        std::cout << "Wednesday:     ";
        double wed;
        std::cin >> wed;
    
        std::cout << "Thursday:      ";
        double thu;
        std::cin >> thu;
    
        std::cout << "Friday:        ";
        double fri;
        std::cin >> fri;
    
        double timeWorked = mon + tue + wed + thu + fri;
    
        double regTime = timeWorked;
        double overtime = 0.00;
        double overPay = 0.00;
        double regPay = hSalary * timeWorked;
    
        if (timeWorked > 40.00)
        {
            regTime = 40.00;
            regPay = hSalary * 40.00;
            overtime = timeWorked - 40.00;
            overPay = hSalary * 1.50 * overtime;
        }
    
        double netPay = regPay + overPay;
    
        std::cout << "\n==========================================";
        std::cout << "\nPayroll Evaluation";
        std::cout << "\n==========================================";
        std::cout << "\nEmployee Information";
        std::cout << "\n------------------------------------------";
        std::cout << "\nFull Name:             " << firstName << " " << lastName;
        printf("\nHourly Salary:         %.2f", hSalary);
        std::cout << "\n==========================================";
        std::cout << "\nTime Worked Summary";
        std::cout << "\n------------------------------------------";
        printf("\n\tMonday         %.2f", mon);
        printf("\n\tTuesday        %.2f", tue);
        printf("\n\tWednesday      %.2f", wed);
        printf("\n\tThursday       %.2f", thu);
        printf("\n\tFriday         %.2f", fri);
        std::cout << "\n==========================================";
        std::cout << "\nPay Summary";
        std::cout << "\n==========================================";
        printf("\n\tRegular Time:  %.2f", regTime);
        printf("\n\tRegular Pay:   %.2f", regPay);
        printf("\n\tOver Time:     %.2f", overtime);
        printf("\n\tOvertime Pay:  %.2f", overPay);
        std::cout << "\n------------------------------------------";
        printf("\n\tNet Pay:       %.2f", netPay);
        std::cout << "\n==========================================";
    }
  9. To execute the project, on the main menu, click Debug -> Start Without Debugging
  10. When requested, type each of the following values and press Enter each time:
    First Name: Michael
    Last Name: Carlock
    Hourly Salary: 28.25
    Monday 7
    Tuesday: 8
    Wednesday: 6.5
    Thursday: 8.5
    Friday: 6.5
    Hello World!
    Payroll Evaluation
    ------------------------------------------
    Enter the following pieces of information
    ------------------------------------------
    Employee Information
    ------------------------------------------
    First Name:    Michael
    Last Name:     Carlock
    Hourly Salary: 28.46
    ------------------------------------------
    Time worked
    ------------------------------------------
    Monday:        7
    Tuesday:       9
    Wednesday:     6.5
    Thursday:      8.5
    Friday:        6.5
    
    ==========================================
    Payroll Evaluation
    ==========================================
    Employee Information
    ------------------------------------------
    Full Name:             Michael Carlock
    Hourly Salary:         28.46
    ==========================================
    Time Worked Summary
    ------------------------------------------
            Monday         7.00
            Tuesday        9.00
            Wednesday      6.50
            Thursday       8.50
            Friday         6.50
    ==========================================
    Pay Summary
    ==========================================
            Regular Time:  37.50
            Regular Pay:   1067.25
            Over Time:     0.00
            Overtime Pay:  0.00
    ------------------------------------------
            Net Pay:       1067.25
    ==========================================
    
    Press any key to close this window . . .
  11. Press C to close the window and return to your programming environment

Using an Opposite Conditional Statement

In the previous section, we use a Greater Than operator to check a condition. That operator has an opposite, which is the Less Than Or Equal operator. We will uase apply as an example.

Practical LearningPractical Learning: Using an Opposite Conditional Statement

  1. Change the document as follows:
    #include <iostream>
    
    int main()
    {
        std::cout << "Payroll Evaluation\n";
        std::cout << "------------------------------------------";
        std::cout << "\nEnter the following pieces of information";
        std::cout << "\n------------------------------------------";
        std::cout << "\nEmployee Information";
        std::cout << "\n------------------------------------------";
        std::cout << "\nFirst Name:    ";
        char firstName[20];
        std::cin >> firstName;
        std::cout << "";
        std::cout << "Last Name:     ";
        char lastName[20];
        std::cin >> lastName;
        std::cout << "Hourly Salary: ";
        double hSalary;
        std::cin >> hSalary;
        std::cout << "------------------------------------------";
        std::cout << "\nTime worked";
        std::cout << "\n------------------------------------------\n";
    
        std::cout << "Monday:        ";
        double mon;
        std::cin >> mon;
    
        std::cout << "Tuesday:       ";
        double tue;
        std::cin >> tue;
    
        std::cout << "Wednesday:     ";
        double wed;
        std::cin >> wed;
    
        std::cout << "Thursday:      ";
        double thu;
        std::cin >> thu;
    
        std::cout << "Friday:        ";
        double fri;
        std::cin >> fri;
    
        double timeWorked = mon + tue + wed + thu + fri;
    
        double regTime = 40.00;
        double regPay = hSalary * 40.00;
        double overtime = timeWorked - 40.00;
        double overPay = hSalary * 1.50 * overtime;
    
        if (timeWorked <= 40.00)
        {
            regTime = timeWorked;
            overtime = 0.00;
            regPay = hSalary * timeWorked;
            overPay = 0.00;
        }
    
        double netPay = regPay + overPay;
    
        std::cout << "\n==========================================";
        std::cout << "\nPayroll Evaluation";
        std::cout << "\n==========================================";
        std::cout << "\nEmployee Information";
        std::cout << "\n------------------------------------------";
        std::cout << "\nFull Name:             " << firstName << " " << lastName;
        printf("\nHourly Salary:         %.2f", hSalary);
        std::cout << "\n==========================================";
        std::cout << "\nTime Worked Summary";
        std::cout << "\n------------------------------------------";
        printf("\n\tMonday         %.2f", mon);
        printf("\n\tTuesday        %.2f", tue);
        printf("\n\tWednesday      %.2f", wed);
        printf("\n\tThursday       %.2f", thu);
        printf("\n\tFriday         %.2f", fri);
        std::cout << "\n==========================================";
        std::cout << "\nPay Summary";
        std::cout << "\n==========================================";
        printf("\n\tRegular Time:  %.2f", regTime);
        printf("\n\tRegular Pay:   %.2f", regPay);
        printf("\n\tOver Time:     %.2f", overtime);
        printf("\n\tOvertime Pay:  %.2f", overPay);
        std::cout << "\n------------------------------------------";
        printf("\n\tNet Pay:       %.2f", netPay);
        std::cout << "\n==========================================";
    0.00;
    }
  2. To execute the project again, on the main menu, click Debug -> Start Without Debugging
  3. When requested, type each of the following values and press Enter each time:
    First Name: Catherine
    Last Name: Busbey
    Hourly Salary: 24.37
    Monday 9.50
    Tuesday: 8
    Wednesday: 10.50
    Thursday: 9
    Friday: 10.50
    Hello World!
    Payroll Evaluation
    ------------------------------------------
    Enter the following pieces of information
    ------------------------------------------
    Employee Information
    ------------------------------------------
    First Name:    Catherine
    Last Name:     Busbey
    Hourly Salary: 24.37
    ------------------------------------------
    Time worked
    ------------------------------------------
    Monday:        9.5
    Tuesday:       8
    Wednesday:     10.5
    Thursday:      9
    Friday:        10.5
    
    ==========================================
    Payroll Evaluation
    ==========================================
    Employee Information
    ------------------------------------------
    Full Name:             Catherine Busbey
    Hourly Salary:         24.37
    ==========================================
    Time Worked Summary
    ------------------------------------------
            Monday         9.50
            Tuesday        8.00
            Wednesday      10.50
            Thursday       9.00
            Friday         10.50
    ==========================================
    Pay Summary
    ==========================================
            Regular Time:  40.00
            Regular Pay:   974.80
            Over Time:     7.50
            Overtime Pay:  274.16
    ------------------------------------------
            Net Pay:       1248.96
    ==========================================
    
    Press any key to close this window . . .
  4. Press T to close the window and return to your programming environment

YouTube Version


Home Copyright © 2001-2025, FunctionX Saturday 14 December 2024, 18:55 Home