Help with C++ problem ! how do i code this

an application for a company that wants a breakdown of payroll by department. Input includes each employee’s last name, first name, department number, hourly salary, and number of hours worked. The output is a list of the seven departments in the company (numbered 1 through 7) and the total gross payroll (rate times hours) for each department.

I have used a for statement for the input, I know it wrong,but I cant figure out how to do it right
Here is what I have so far:
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
#include <iostream>
#include<string>
using namespace std;

void main()
{
	string lastname,firstname;
	double depnumber[7]={1,2,3,4,5,6,7}, rate, hours, totalgross[7],totalrate[7], totalhours[7];
	int i;

for (i=0;i<7;i++)

{   cout<<"enter department number"<<endl;
	cin>>i;
	
	cout<<"enter first name"<<endl;
	cin>>firstname;

	cout<<"enter last name"<<endl;
	cin>>lastname;

	cout<<"enter hourly salary"<<endl;
	cin>>rate;

	cout<<"enter hours worked"<<endl;
	cin>>hours;

	totalrate[i]=totalrate[i]+rate;
	totalhours[i]=totalhours[i]+hours;

}
for (i=0;i<7;i++)
	totalgross[i]=totalrate[i]*totalhours[i];
	cout<<"the total gross payroll for department"<<i+1<<"is"<<totalgross[i]<<endl;



system ("pause");

}
Last edited on
Use code block when posting code.
Use code block when posting code.


Sorry I didnt know. But can you help me with this problem ?

thanks
depnumber isn't used at all, so I would suggest using or removing it. Your last for loop has not brackets so it will only run the first line 7 times. You never declare totalrate or total hours to have only zeroes in them, so they will contain random numbers when you execute that line of code. Use getline(cin, str) for strings where str is the name of the string. Use cin.ignore(1, '\n') or cin.ignore() after each cin so that you can get rid of the newline character that will screw up every other input after it.

That's all I believe.
Last edited on
Thanks !
Do you think that the for loop is good for the input ? I'm not sure about the position or i whether it should be a while loop.
Last edited on
I dont really understand what u have all theese wierd arrays and extra variables and stuff.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
while(true){
cout<<"enter department number"<<endl;
	cin>>depnumber;

	cout<<"enter first name"<<endl;
	cin>>firstname;

	cout<<"enter last name"<<endl;
	cin>>lastname;

	cout<<"enter hourly salary"<<endl;
	cin>>rate;

	cout<<"enter hours worked"<<endl;
	cin>>hours;

	cout<<"the total gross payroll for department "<<depnumber<<" is: "<<rate*hours<<endl;

}


I dont know if this is what u want cause i dont understand what u are trying to do. So if this is not how u want it. Tell me :P
A while loop will be better. It will allow the user to decide when they are done inputting data.
Ok got it ! thanks :)
Topic archived. No new replies allowed.