Logic error (letting me put >50)

Write your question here.

It shouldn't be letting me put more than 40, but it is. I'm not sure what's causing it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//Ashton Dreiling
//Payroll program exercise
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

//function prototypes
void calculationForGrossPay (double &grossPay, double hourlyPayRate, double numberOfHoursWorked);
bool inputValidationCheckOne (double hourlyPayRate);
bool inputValidationCheckTwo (double numbersOfHoursWorked);

//global constants
const double RANGED_NUMBER_FOR_PAY_RATE_1=7.50;
const double RANGED_NUMBER_FOR_PAY_RATE_2=18.25;
const int RANGED_NUMBER_FOR_HOURS_WORKED_1=0;
const int RANGED_NUMBER_FOR_HOURS_WORKED_2=40;
int main()
{
    //some local variables
    double hourlyPayRate=0;
    double numbersOfHoursWorked=0;
    double grossPay=0;
    bool status;
    bool status2;
    string doAgain;


    cout << "Hello, today we will be calculating your employees' payroll." << endl;

    do
    {


        do
        {

            cout << "Please enter an employee's hourly pay rate." << endl;
            cin >> hourlyPayRate;
            cout << "You put the your employee's hourly pay rate is $" << hourlyPayRate << endl;

            //function to calculate if the hourlyPayRate is in range
            status=inputValidationCheckOne(hourlyPayRate);

            //if-then statement to tell user if they entered a valid input
            if (status==true)
                cout << "You put in a valid hourly pay." << endl;
            else
                cout << "You did not enter a valid hourly pay. It must range between $7.50 through $18.25." << endl;
        }//end of do while loop to confirm hourly pay
        while (status==false);

        do
        {
            cout << "Please enter the number of hours your employee worked." << endl;
            cin >> numbersOfHoursWorked;
            cout << "You put that your employee works " << numbersOfHoursWorked << endl;

            //function to see if numbersOfHoursWorked is in valid range
            status2=inputValidationCheckTwo(numbersOfHoursWorked);

            //if-then statement to tell user if their input was valid
            if (status2==true)
                cout << "You put in a valid number of hours worked." << endl;
            else
                cout << "You didn't put in a valid number of hours worked. It must range between 0 through 40." << endl;
        }//end of do while loop to confirm if hours worked is in range
        while (status2==false);


        //grossPay function that is executed after the hours worked and hourly pay rate is in valid range
        calculationForGrossPay(grossPay, hourlyPayRate, numbersOfHoursWorked);

        cout << "Your employee's gross pay is $" << grossPay << "." << endl;

        cout << "Would you like to enter another employee's payroll? If so, please type yes." << endl;
        cin >> doAgain;
    }//end of do while  loop to enter another employee's payroll
    while(doAgain=="yes" || "Yes" || "YES");




    system("Pause");
    return 0;
}//end main

//function to calculate if hourlyPayRate is in range
bool inputValidationCheckOne (double hourlyPayRate)
{

    if (hourlyPayRate>=RANGED_NUMBER_FOR_PAY_RATE_1 && hourlyPayRate<=RANGED_NUMBER_FOR_PAY_RATE_2)
        return true;
    else
        return false;

}//end inputValidationCheckOne

//function to calculate if numbersOfHoursWorked is in range
bool inputValidationCheckTwo(double numbersOfHoursWorked)
{
    if (numbersOfHoursWorked<RANGED_NUMBER_FOR_HOURS_WORKED_1 && numbersOfHoursWorked>RANGED_NUMBER_FOR_HOURS_WORKED_2)
        return false;
    else
        return true;
}//end inputValidationCheckTwo

//grossPay function to calculate employee's grossPay
void calculationForGrossPay(double &grossPay, double hourlyPayRate, double numbersOfHoursWorked)
{
    grossPay=(hourlyPayRate*numbersOfHoursWorked);
}//end calculationForGrossPay 
Last edited on
A quick logic error :
if (numbersOfHoursWorked<RANGED_NUMBER_FOR_HOURS_WORKED_1 && numbersOfHoursWorked>RANGED_NUMBER_FOR_HOURS_WORKED_2)

This should be OR (||) operator, not AND (&&) operator

if (numbersOfHoursWorked<RANGED_NUMBER_FOR_HOURS_WORKED_1 || numbersOfHoursWorked>RANGED_NUMBER_FOR_HOURS_WORKED_2)
Does that help you? :)
Yes! Does everything else look good after that?
Last edited on
if (status==true)

This can be written:

1
2
3
if (status) {

}


The false version looks like this:

1
2
3
if (!status) {

}


Try to avoid global variables, they seem easy now, but they lead to bad code with hard to find errors. Send each function the variables it needs. Btw I think the names you have for them are too long, consider these:

1
2
3
4
5
6
7
8
const double RANGED_NUMBER_FOR_PAY_RATE_1=7.50;
const double PayLowerLimit= 7.50;
const double RANGED_NUMBER_FOR_PAY_RATE_2=18.25;
const double  PayUpperLimit= 18.25;
const int RANGED_NUMBER_FOR_HOURS_WORKED_1=0;
const double HoursLowerLimit = 0.0; // made these double so as to not mix the types
const int RANGED_NUMBER_FOR_HOURS_WORKED_2=40;
const double HoursUpperLimit = 0.0;


If using do loops, put the while part on the same line as the closing brace, so it doesn't look like a while loop

1
2
//end of do while loop to confirm hourly pay
}  while (!status);


This part doesn't do what you think it does:

1
2
  }//end of do while  loop to enter another employee's payroll
    while(doAgain=="yes" || "Yes" || "YES");


Rather than trying to account for every combination of spelling and mixing of upper and lower case, just get input as a single char and convert to uppercase with the toupper function. Then you can compare to one thing: 'Y'

At the moment the form of the conditional is wrong, one has to do this:

1
2
3
  
//end of do while  loop to enter another employee's payroll
} while(doAgain=="yes" || doAgain ==  "Yes" || doAgain ==  "YES");


It would still fail on input of "YEs" plus all the other combinations not mentioned. But as I say above, you can avoid that.

This:

1
2
3
4
5
6
7
8
9
bool inputValidationCheckOne (double hourlyPayRate)
{

    if (hourlyPayRate>=RANGED_NUMBER_FOR_PAY_RATE_1 && hourlyPayRate<=RANGED_NUMBER_FOR_PAY_RATE_2)
        return true;
    else
        return false;

}//end inputValidationCheckOne 


Can be written:

1
2
3
4
5
6
bool ValidateHourlyPayRate (const double hourlyPayRate, 
                                     const double PayLowerLimit,
                                     const double PayUpperLimit
                                      )
{ return hourlyPayRate>=PayLowerLimit && hourlyPayRate<=PayUpperLimit;
}


I changed the name of the function to something more meaningful, made the parameters const, one return statement is sufficient - the condition evaluates to true or false, and used the variables names I mentioned earlier.

Hope this helps a bit :+)
Last edited on
Topic archived. No new replies allowed.