Salary coding

closed account (ETA9216C)
I've started to learn about adding "prototype" into my code in my Computer Science coarse. I was wondering if i did it right.

I also need help with a piece of code that after the user for my code decides to say no to "are there any more employees" question, it displays how many people have logged their info to find about their weekly salary.

This program is suppose to calculate a persons weeks salary.
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
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;

double weeklypay(double hourlyrate,double hours);


int main()
{
  double hourlyrate,hours,exemptions;
	char flag= 'y'||'Y';

 while(flag=='y'||'Y');
	cout<<"Hourly Salary: ";
	cin>> hourlyrate;
	cout<<"Hours: ";
	cin>> hours;
	cout<<"Number of Exemptions: ";
	cin>> exemptions;

	weeklypay(hourlyrate,hours);

	system("pause");
	return 0;

	}


 double weeklypay(double hourlyrate,double hours)
{
	if(hours>40)
	{
	   salary=hours*hourlyrate*1.5;

	}
	else if(hours<=40)
	{
		salary=hours*hourlyrate;


	}

	  return(salary);
}



	  withheld=.20*(salary-600-exemption*100);


		cout<<"your salary is $"<<salary;
		cout<<"The withheld is $"<<withheld;
		cout<<"Are there other employees?y,Y for yes, anything else is no";
		cin>>flag;



	system("pause");
	return 0;

}
Last edited on
I'm not sure what you mean by prototyping in this situation, but you definitely have some problems.

First of all, in line 12 you declare a variable flag and assign it the value 'y'||'Y'. When you "or" the character values 'y' and 'Y' together, you get the value 'true', which is 1. Instead, you probably want to read in the value for flag with cin or some such.

In line 14 you have an empty loop (semi-colon instead of a statement or compound statement) that executes as long as flag is 'y' or the value 'Y' (which will evaluate to non-zero, so true). So, you have an infinite loop doing nothing. Instead, you probably want the line to be:
while(flag=='y'|| flag=='Y'){

Lines 30 - 45 appear to be a function defined in the middle of main(). You can't do that. You should define weeklypay outside of main() and call it where you need to.
Last edited on
closed account (ETA9216C)
i meant adding prototype before the main sorry.

for line 30 - 45, i think i have it defined on line 6. My professor checked it out and he deemed it as an okay.

Last edited on
closed account (ETA9216C)
i've added this a line here:on line 62-65. Does that line make any sense? What it suppose to do is if they do not wish to continue, it's tells them how many people have used the the program to check their salary. And i can't get this piece to work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

	  withheld=.20*(salary-600-exemption*100);


		cout<<"your salary is $"<<salary;
		cout<<"The withheld is $"<<withheld;
		cout<<"Are there other employees?y,Y for yes, anything else is no";
		cin>>flag;
		i=1;
		i++;
    if (flag=='y'!||flag=='Y'!)
    cout<<"the number of employees are "<<i;

	system("pause");
	return 0;

}
Last edited on
Lines 30-45, by themselves, are fine*, and do match the declaration, or prototype, in line 6. However, lines 46-62 are apparently the completion of the main() function. You cannot stick the definition of a function (weeklypay() in this case) inside the definition of another function (main() in this case). Move weeklypay() to after line 62.

In your latest snippet, this line should not compile:
if (flag=='y'!||flag=='Y'!)

The exclamation points are extraneous and semantically incorrect.

You also haven't shown how you are populating the flag value.

*When you calculate overtime pay, time-and-a-half is generally calculated only on the hours above 40, not the entire time worked during the week. Also, you can replace line 37 with a simple else statement rather than anelse if.

When you've made changes, post your entire code again.
Last edited on
closed account (ETA9216C)
like this? from line 22 if that's what you are talking about.
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
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;

double weeklypay(double hourlyrate,double hours); //prototype



int main()
{
  double hourlyrate,hours,exemptions,withheld;
	char flag= 'y';

 while(flag=='y');
	cout<<"Hourly Salary: ";
	cin>> hourlyrate;
	cout<<"Hours: ";
	cin>> hours;
	cout<<"Number of Exemptions: ";
	cin>> exemptions;


	return 0;

	}


 double weeklypay(double hourlyrate,double hours)
{
	double salary;
	if(hours>40)
	{
	   salary=hours*hourlyrate*1.5;

	}
	else (hours<=40);
	{
		salary=hours*hourlyrate;


	}

	  return (salary);
}



	  withheld=.20*(salary-600-exemption*100);


		cout<<"your salary is $"<<salary;
		cout<<"The withheld is $"<<withheld;
		cout<<"Are there other employees? y for yes, anything else is no";
		cin>>flag;
		i=1;
		i++;

        if (flag=='y'!)
        {
        cout<<"the number of employees are "<<i;
        }
	system("pause");
	return 0;

}
weeklypay(hourlyrate,hours);
Last edited on
Upon closer inspection, it looks like you intend for main() to end in line 26. If that's the case, what are line 49 - 67? I think what you wanted to do was make 49 - 67 part of main. If that's the case, then 29 - 45 (THE WHOLE THING) needs to move to line 67. You also need to remove line 24 so main() doesn't return early.

If you really do want main to end in 26, then you need to figure out what you're trying to do with lines 49 - 67. They don't make sense to me.

Your while loop in line 15 is still an infinite loop. The semi-colon at the end or the statement is an empty statement that will be executed over and over and over again because flag is never being set to anything other than 'y'. You need to replace the ';' with a '{' like I said in my first post.

There is just too much wrong with your code right now to itemize everything. What you need to do is design your main() function in psuedo code. I think it will be something like this:
1
2
3
4
5
6
7
8
9
10
Initialize flag = 'y'
Initialize counter = 0
Begin Loop While flag == 'y'
   Input salary information
   Call weekly pay
   Print out results
   Increment Counter
   Ask for additional employees
End Loop
Print out counter


Go from there to writing the code.
Last edited on
closed account (ETA9216C)
line 49 is needed for this assignment, it's to calculate the money withheld from salary.
should i add another prototype or declaration?
Last edited on
while(flag=='y');

This code is the same as
1
2
 while(flag=='y')
     ;


When you use a loop, only the first statement following the loop is governed by the loop condition. If you want more than one statement to be governed by the loop condition, you must use a compound statement (a block of statements delimited by curly brackets {}.) But, you should note the loop condition will always be true, currently, even with the addition of the curly brackets. flag never changes.

1
2
3
4
5
6
    while(flag=='y')
    {
        cout << "Hourly salary: " ;
        cin >> hourlyrate ;
        // ...
    }


None of the code at or past line 49 occurs within a function. The only place such code can appear is within a function.
Last edited on
closed account (ETA9216C)
This is the result of what i encountered:
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
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;

double weeklypay(double hourlyrate,double hours); //prototype

double amount(double withheld,double exemption);

int main()
{



  double hourlyrate,hours,exemption,withheld,salary;
	char flag= 'y';

 while(flag=='y');
 {
	cout<<"Hourly Salary: ";
	cin>> hourlyrate;
	cout<<"Hours: ";
	cin>> hours;
	cout<<"Number of Exemptions: ";
	cin>> exemption;


weeklypay(hourlyrate,hours);
amount(withheld,exemption);

withheld =0;
salary=0;

	return 0;
 }



 double weeklypay(double hourlyrate,double hours);
{
double salary;
	if(hours>40)
	{
	   salary=hours*hourlyrate*1.5;

	}
	else
	{
		salary=hours*hourlyrate;

	}

    return salary;
}
double amount(double withheld,double exemption);
{
    double salary,i;
	    withheld =.20*(salary-600-exemption*100);


		cout<<"your salary is $"<<salary;
		cout<<"The withheld is $"<<withheld;
		cout<<"Are there other employees? y for yes, anything else is no";
		cin>>flag;
		i=1;
		i++;

        if (flag==!'y')

        cout<<"the number of employees are "<<i;

	system("pause");
	return 0;

}
}


i can't get it to compile. I get " undefined reference to weekpay(double,double)" "undefined reference to amount(double,double)". For the last function, i don't what to return to. Do i return "amount"?
Last edited on
Topic archived. No new replies allowed.