0

The program is very simple. Prompt for an employee's payroll, hours worked, pay code etc.

// Project Name: Lab Assignment 3
// Programmer Name: Trenton Covieo
// Date Written: 9/18/2017
// Description: This program will calculate employee pay based on  paycode and hours provided by the employee
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//  Begin main Function Definition    
    int main()    
    {
        //intialization
        char paycode;
        string employee_number;
        int hours;
        const double min_wage = 8.90;
        double pay_rate;
        double calc_pay;
        char again;
        int employee_num = 0;

        //Diplay identifcation on screen        
        cout << "Lab Assignment 3"<< endl; 
        cout << "programmed by Trenton Covieo" << endl << endl;

        //Prompts for exmployee number      
        do{
            cout << "Enter employee number: ";
            cin >> employee_number;
            // prompts for/ determines paycode and payrate

            cout <<"Enter Paycode: ";
            cin >> paycode;
            switch (paycode)
            {
                case 'M':
                pay_rate = min_wage;
                break;

                case 'O':
                pay_rate = min_wage + 1;
                break;

                case 'T':
                pay_rate = min_wage + 2; 
                break;

                case 'm':
                pay_rate = min_wage;
                break;

                case 'o':
                pay_rate = min_wage + 1;
                break;

                case 't':
                pay_rate = min_wage + 2;
                break;

                // An incorrect code was entered
                default: 
                pay_rate = 0;
                cout << "You did not enter a valid paycode!" << endl;
                break;
            }
            // Prompts for hours worked

            cout << "Enter hours worked: ";
            cin >> hours;

            //calculates pay based upon hours entered including overtime
            if (hours <=40)  
                calc_pay = pay_rate * hours;
            else if (hours > 40)
                calc_pay = (pay_rate* 40) + pay_rate * (hours-40) * 1.5;

            //outputs information entered       
            cout << "Employee#: " << employee_number << endl;
            cout << "Pay Rate $: " << fixed
                 << setprecision(2)<< pay_rate  << endl;
            cout << "Hours Worked: "
                 << fixed << setprecision(2) << hours <<  endl;
            cout << "Total Pay: " << fixed << setprecision(2) << calc_pay 
                 << endl;

            // prompt for another employeee
            cout<< "Do you want to enter another employee? (Y/N) ";
            cin >> again;
        }while (again =='Y' || again =='y') ;

        /* This is the part that I can't seem to figure out.
           I have anoter Do-While loop that says
           whenever the paycode = 'm' it will add 1 to employee_num.
           It works fine when i have one condition,
           but if i try two (both 'M' and 'm') the cout won't work. */
        do
        {    employee_num++;
        }while (paycode == 'm'|| paycode =='M') ;
        cout <<"Number of Employees Processed: " << employee_num << endl;
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • 2
    I recommend to start treating your whitespace, newline and StackOverflow readers with respect, by adopting a (any) code formatting style. Also, curlies `{}`can be your friend. – Yunnosch Sep 28 '17 at 07:18
  • Also, a correct [mcve] of this would have contained hardly anything apart from the last four lines. And some more lines for getting your code compilable. Please read [tour], especially [ask]. – Yunnosch Sep 28 '17 at 07:20
  • You might have found the problem yourself with either of the following: Format cleanly (I noticed the answer while doing that), https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ or https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb – Yunnosch Sep 28 '17 at 07:23
  • My apologies, I'm not sure how to use an any code formatting style. – Trenton Covieo Sep 28 '17 at 07:36
  • While you write your question, there is a huge, highlighted box titled "How to format" to the right of where you are typing. Did you read that? Or anything in the help pages? I can tell that you (still) did not take the [tour]. – Yunnosch Sep 28 '17 at 07:40

2 Answers2

0

Ya that's because the do while loop is in infinite loop

Use an if statement instead of do while

if (paycode == 'm'|| paycode =='M') {
      employee_num++;
}

cout <<"Number of Employees Processed: " << employee_num << endl;
Samuel Robert
  • 10,106
  • 7
  • 39
  • 60
0

Your problem is caused by the endless loop caused by the fact that you do not change paycode within the second loop. It will continue for ever, once it has detected 'm' or 'M'.

Simply chaning the:

   do
    {    employee_num++;
    }while (paycode == 'm'|| paycode =='M') ;

to

    if (paycode == 'm'|| paycode =='M') 
    {    employee_num++;
    }

will fix that immediate problem.

However, I get the impression that you intend to count the employees with paycode 'mM'. That can be achieved more elegantly by doing the counting inside the previous loop.

    do{
        /* ... */
        switch (paycode)
        {
            case 'M':
            case 'm':
                employee_num++;
                pay_rate = min_wage;
                break;

        /* ... combining O&o, T&t */
            default:
                /* ... */
        }

        // prompt for another employeee
        cout<< "Do you want to enter another employee? (Y/N) ";
        cin >> again;
    } while (again =='Y' || again =='y');

    cout <<"Number of Employees Processed: " << employee_num << endl;

    /* ... */

Or, if you want to count ALL employees,
as indicated by the naming of the variable and the output after the loop,
do it unconditionally inside the first loop.

    do{
        /* ... */
        switch (paycode)
        {
                /* ... */
        }
        employee_num++;

        // prompt for another employeee
        cout<< "Do you want to enter another employee? (Y/N) ";
        cin >> again;
    } while (again =='Y' || again =='y');

    cout <<"Number of Employees Processed: " << employee_num << endl;

    /* ... */
Yunnosch
  • 26,130
  • 9
  • 42
  • 54