Answer to Question #349991 in C++ for ali

Question #349991

The program will accept some values from the user and will print out the following

1]                 Details of the user

•      Worker ID – Alphanumeric (2 letters and three digits e.g. EF101)     Full Name of the worker.

2]                 Gross Pay (GP)

•      GP = Rate * Hours Worked o Rate is a constant of 11.25 ($11.25 per hour) o Hours Worked to be entered by the user.

3]                 Deduction

•      Union fee o Constant of 50 ($50 per member) o Retirement is 10% of GP

4]                 Take Home Pay (THP)

•      THP = GP – Deduction 


1
Expert's answer
2022-06-13T08:29:26-0400
#include <iostream>
#include <iomanip>

void check(char* id);
void check(double& hours);
void retype(const char* error_str, char* x);

int main() {
    char workerID[1024];
    double GP, hours, Deduction, THP;
    const float Rate = 11.25;

    std::cout << "Please enter worker ID: ";
    std::cin >> workerID;
    check(workerID);
    std::cout << "Please enter hours worked: ";
    std::cin >> hours;
    check(hours);

    GP = Rate * hours;
    Deduction = 50 + ((GP - 50) * 0.1);
    THP = GP - Deduction;

    std::cout << std::fixed;
    std::cout << std::setprecision(2);
    std::cout << "\nWorker ID: " << workerID << "\n";
    std::cout << "Gross Pay (GP): " << GP << "\n";
    std::cout << "Deduction: " << Deduction << "\n";
    std::cout << "Take Home Pay (THP): " << THP << std::endl;
    return 0;
}

void check(char* id) {
    for (int i = 0; i < 5; i++) {
        if (i < 2) {
            if (isalpha(id[i]) && strlen(id) < 6 ) {
                continue;
            } else {
                retype("Wrong ID! Re-enter please: ", id);
                i = 0;
            }
        } else {
            if (isdigit(id[i]) && strlen(id) < 6) {
                continue;
            } else {
                retype("Wrong worker ID! Re-enter please: ", id);
                i = 0;
            }
        }
    }
}

void check(double& hours) {
    while (!std::cin || hours <= 0) {
        std::cout << "Wrong hours! Re-enter please: ";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cin >> hours;
    }
}

void retype(const char* error_str, char* x) {
    std::cout << error_str;
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin >> x;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS