unable to "declare" a value

I have no idea how to represent a letter "h" as variable such as "hourly_employee".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include<iostream>
#include <string>
using namespace std;
int main( )
{

   string employee_type;
   employee_type = hourly;
   employee_type = management;
   
   
   cout << "Enter an h for hourly or m for managment employee:";
   cin >> employee_type;
   
   return 0;


I'm trying to allow "h" to represent an hourly employee. later on I would need to distingquish between the two classes of employees (hourly vs managament)
Last edited on
Seems you are having trouble with one of the most fundamental aspects of programming which is declaring variables.

There are certain data types which can hold different types of data.

int - holds a non decimal integer
double - holds any real number
float -same as double but uses now a negligible amount of less memory than a double data type

string - holds an array of characters

Some datatypes are declared differently such as.

string str = "this is an array of characters";

str can be named anything but make sure you be explicit when naming your variables. Do not pick arbitrary names for them.

to declare a number

1
2
3
int    num1 = 5; 
float  num2 = 5.5;
double num3 = 5.4;



When declaring variable names you must name them differently because how is the program supposed to know which variable you are talking about.

So for your program....

You are creating a string variable with the name of employee_type;

string = employee_type;

then setting this to hourly and management.

You should look up youtube videos on declaring variables.

How you would create this little program is like this:

1
2
3
4
5
string employee_name = "jake35"

double employee_wage = 20.50;

string employee_management = "whatever"



or if you want to create some input go like this

1) create a set of variables for user input. Don't initialize them, have the user do so. Make sure you use the right datatypes...for names use a string. for pay use float or double.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

string employee_name;

double employee_wage;

string employee_management;

cout << "Enter employee name " << endl;
cin >> employee _name;   // this is employee_name = user input (STRING)

cout << "Enter employee wage " << endl;
cin >> employee_wage;   // this is employee_wage = user input (DOUBLE)

cout << "Enter employee Management " << endl;
cin >> employee_management;   // this is employee_wage = user input (STRING)



hope this helps...let me know :)

UPDATE:

So you want to create an if statement

1
2
3
cout << "Options" << endl;
cout << "h for hourly" << endl;
cout << "m for management" << endl;

if (input is h)
{
do something
}
else if ( input is m)
{
do something
}




Last edited on
Thank you for your reply. I read over the info you provided and that got me this far. I'm till not gettting the nuances of declaring though. I'm trying to set the employee_type by the user input and then run the appropriate if/else statement. I also seem to get an extra line to the input when asked for the pay period(weekly, bi-week, montly.) Regardless of what is enter it does not direct to the right if/else statement. Thank you again in advance for you time and help!

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
#include <iostream>
#include <string>
using namespace std;
int main( )
{

   string employee_type;
         
      cout << "Enter an h for hourly or m for managment employee:";
      cin >> employee_type;
      
      if (employee_type(m))  
         {
         cout << "You have entered the Management employee" << endl;
         int hours;
         double gross_salary, fica, medicare, health_insurance, net_salary;
         string weekly, bi_weekly, monthly;
                 
         cout << "Enter the gross salary amount: $";
         cin >> gross_salary;
         cout << "Enter W for weekly B for bi-weekly or M for pay period length,";
         weekly = "W", bi_weekly = "B", monthly = "M";
         cin >> weekly, bi_weekly, monthly;
             
             if (cin >> weekly)
             {
               gross_salary = gross_salary/52;
             }
             else if (cin >> bi_weekly)
             {
               gross_salary = gross_salary/26;
             }  
             else if (cin >> monthly)
             {
               gross_salary = gross_salary/12;
             }
                 
             fica = gross_salary*0.07;
             medicare = gross_salary*0.02;
             health_insurance = gross_salary*0.1;
             net_salary = gross_salary-(fica+medicare+health_insurance);
               
             cout.setf(ios::fixed);
             cout.setf(ios::showpoint);
             cout.precision(2);
             cout << "Salary for pay period = " << gross_salary << endl;
             cout << "FICA deduction =-$" <<fica << endl;
             cout << "Health Insurance deduction =-$" << health_insurance << endl;
             cout << "Medicare deduction =-$" <<medicare << endl;
             cout << "Net pay is $" << net_salary << endl;
         }
         
         else if (employee_type(h))
         {
            int hours;
             double gross_pay, rate, fica, medicare, health_insurance, net_pay;
         
             cout << "Enter the hourly rate of pay: $";
             cin >> rate;
             cout << "Enter the number of hours worked,\n";
             cin >> hours;
         
             if (hours > 40)
                 gross_pay = rate*40 + 1.5*rate*(hours - 40);
             else
                 gross_pay = rate*hours;
                 
             fica = gross_pay*0.07;
             medicare = gross_pay*0.02;
             health_insurance = gross_pay*0.1;
             net_pay = gross_pay-(fica+medicare+health_insurance);
               
             cout.setf(ios::fixed);
             cout.setf(ios::showpoint);
             cout.precision(2);
             cout << "Hours = " << hours << endl;
             cout << "Hourly pay rate = $" << rate << endl;
             cout << "Gross pay = $" << gross_pay << endl;
             cout << "FICA deduction =-$" <<fica << endl;
             cout << "Health Insurance deduction =-$" << health_insurance << endl;
             cout << "Medicare deduction =-$" <<medicare << endl;
             cout << "Net pay is $" << net_pay << endl;
         }
 
    
    return 0;   
}
OK so I think i seem to have fixed the declaration errors but, not I can't get the if/else selection to work. It seems to only take the first 'if statement' regardless of what is enter.

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
#include <iostream>
#include <string>
using namespace std;
int main( )
{

   char employee_type;
         
      cout << "Enter an h for hourly or m for managment employee:";
      cin >> employee_type;
      
      if ((employee_type = 'm') || (employee_type = 'M'))  
         {
         cout << "You have entered the Management employee" << endl;
         int hours;
         double gross_salary, fica, medicare, health_insurance, net_salary;
         char pay_period;
                 
         cout << "Enter the gross salary amount: $";
         cin >> gross_salary;
         cout << "Enter W for weekly B for bi-weekly or M for pay period length,";
         cin >> pay_period;
             
             if ((pay_period = 'W') || (pay_period = 'w'))
             {
               gross_salary = gross_salary/52;
             }
             else if ((pay_period = 'B') || (pay_period = 'b'))
             {
               gross_salary = gross_salary/26;
             }  
             else if ((pay_period = 'M') || (pay_period = 'm'))
             {
               gross_salary = gross_salary/12;
             }
                 
             fica = gross_salary*0.07;
             medicare = gross_salary*0.02;
             health_insurance = gross_salary*0.1;
             net_salary = gross_salary-(fica+medicare+health_insurance);
               
             cout.setf(ios::fixed);
             cout.setf(ios::showpoint);
             cout.precision(2);
             cout << "Salary for pay period = " << gross_salary << endl;
             cout << "FICA deduction =-$" <<fica << endl;
             cout << "Health Insurance deduction =-$" << health_insurance << endl;
             cout << "Medicare deduction =-$" <<medicare << endl;
             cout << "Net pay is $" << net_salary << endl;
         }
         
      else if ((employee_type = 'h') || (employee_type = 'H'))
            {
             cout << "You have entered the Hourly employee" << endl;
             int hours;
             double gross_pay, rate, fica, medicare, health_insurance, net_pay;
         
             cout << "Enter the hourly rate of pay: $";
             cin >> rate;
             cout << "Enter the number of hours worked,\n";
             cin >> hours;
         
             if (hours > 40)
                 gross_pay = rate*40 + 1.5*rate*(hours - 40);
             else
                 gross_pay = rate*hours;
                 
             fica = gross_pay*0.07;
             medicare = gross_pay*0.02;
             health_insurance = gross_pay*0.1;
             net_pay = gross_pay-(fica+medicare+health_insurance);
               
             cout.setf(ios::fixed);
             cout.setf(ios::showpoint);
             cout.precision(2);
             cout << "Hours = " << hours << endl;
             cout << "Hourly pay rate = $" << rate << endl;
             cout << "Gross pay = $" << gross_pay << endl;
             cout << "FICA deduction =-$" <<fica << endl;
             cout << "Health Insurance deduction =-$" << health_insurance << endl;
             cout << "Medicare deduction =-$" <<medicare << endl;
             cout << "Net pay is $" << net_pay << endl;
         }
 
    
    return 0;   
}
Lines 12 and 52: You're using the assignment operator (=), not the equality operator (==).
I think I have gotten everything working now. If anyone would like to give me some feedback if I missed anything that would be appreciated. I have run every option that I could and believe it is working. Thanks

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
#include <iostream>
#include <string>
using namespace std;
int main( )
{

      char employee_type;
         
      cout << "Enter the letter 'H' for hourly or 'M' for managment employee:";
      cin >> employee_type;
      
         if ((employee_type == 'm') || (employee_type == 'M'))
               {
               int hours;
               double gross_salary, fica, medicare, health_insurance, net_salary;
               char pay_period;       
                                 
               cout << "You have entered the option for Management employee." << endl;
               cout << "Now enter the gross salary amount: $";
               cin >> gross_salary;
               cout << "Enter the letter 'W' for weekly 'B' for bi-weekly or 'M'for monthly to select the desired pay period length.";
               cin >> pay_period;                   
                     {
                     if ((pay_period == 'W') || (pay_period == 'w'))
                        cout << "You have selected the Weekly pay period." << endl;
             
                     else if ((pay_period == 'B') || (pay_period == 'b'))
                        cout << "You have selected the Bi-Weekly pay period." << endl;
             
                     else if ((pay_period == 'M') || (pay_period == 'm'))
                        cout << "You have selected the Monthly pay period." << endl;
                        
                     else if (!(pay_period == 'W') || !(pay_period == 'w'))
                        {cout << "You have not entered a valid selection, please restart the program.";
                        return 0;}
             
                     else if (!(pay_period == 'B') || !(pay_period == 'b'))
                        {cout << "You have not entered a valid selection, please restart the program.";
                        return 0;}
             
                     else if (!(pay_period == 'M') || !(pay_period == 'm'))
                        {cout << "You have not entered a valid selection, please restart the program.";
                        return 0;}

                     } 
                           
             if ((pay_period == 'W') || (pay_period == 'w'))
               gross_salary = gross_salary/52;
             
             else if ((pay_period == 'B') || (pay_period == 'b'))
               gross_salary = gross_salary/26;
             
             else if ((pay_period == 'M') || (pay_period == 'm'))
               gross_salary = gross_salary/12;
                              
             fica = gross_salary*0.07;
             medicare = gross_salary*0.02;
             health_insurance = gross_salary*0.1;
             net_salary = gross_salary-(fica+medicare+health_insurance);
               
             cout.setf(ios::fixed);
             cout.setf(ios::showpoint);
             cout.precision(2);
             cout << "Gross salary for the pay period: = " << gross_salary << endl;
             cout << "FICA deduction = -$" <<fica << endl;
             cout << "Health Insurance deduction = -$" << health_insurance << endl;
             cout << "Medicare deduction = -$" <<medicare << endl;
             cout << "Net pay is $" << net_salary << endl;
             }
         
         else if ((employee_type == 'h') || (employee_type == 'H'))
            {
             cout << "You have entered the selection for Hourly employee" << endl;
             int hours;
             double gross_pay, rate, fica, medicare, health_insurance, net_pay;
         
             cout << "Enter the hourly rate of pay: $";
             cin >> rate;
             cout << "Enter the number of hours worked:";
             cin >> hours;
         
             if (hours > 40)
                 gross_pay = rate*40 + 1.5*rate*(hours - 40);
             else
                 gross_pay = rate*hours;
                 
             fica = gross_pay*0.07;
             medicare = gross_pay*0.02;
             health_insurance = gross_pay*0.1;
             net_pay = gross_pay-(fica+medicare+health_insurance);
               
             cout.setf(ios::fixed);
             cout.setf(ios::showpoint);
             cout.precision(2);
             cout << "Hours = " << hours << endl;
             cout << "Hourly pay rate = $" << rate << endl;
             cout << "Gross pay = $" << gross_pay << endl;
             cout << "FICA deduction = -$" <<fica << endl;
             cout << "Health Insurance deduction = -$" << health_insurance << endl;
             cout << "Medicare deduction = -$" <<medicare << endl;
             cout << "Net pay is $" << net_pay << endl;
         }
         else if (!(employee_type == 'h') || !(employee_type == 'H'))
         {
            cout << "You have not entered a valid selection, please restart the program.";
         }   
         else if (!(employee_type == 'm') || !(employee_type == 'M'))
         {
            cout << "You have not entered a valid selection, please restart the program.";
         }
    
    return 0;   
}

Lines 56-56 and 87-101 are very similar and you might consider moving them to a common function. Or consider creating two functions. One for management and one for hourly.

Your if statements on lines 105 and 107 are extraneous. All you need is the else at 103. You've already determined employee type is not H,h,M or m through lines 12 and 71.
Last edited on
Topic archived. No new replies allowed.