### 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:
 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````#include #include 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"<>i; cout<<"enter first name"<>firstname; cout<<"enter last name"<>lastname; cout<<"enter hourly salary"<>rate; cout<<"enter hours worked"<>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"<
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.

 ``1234567891011121314151617181920`` ``````while(true){ cout<<"enter department number"<>depnumber; cout<<"enter first name"<>firstname; cout<<"enter last name"<>lastname; cout<<"enter hourly salary"<>rate; cout<<"enter hours worked"<>hours; cout<<"the total gross payroll for department "<

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.