Hey there ans good morning! I have a problem when I try to enter my second statement for my second employee. After I enter my first statement for my first employee, the output says "Press any key to continue" And it closes. I've tried using getche() or system(pause), but didn't helped. Is there any other way to fix this? I would be happy if this can be fixed so I will be able to make a code where I can calculate the total salary of all employees. Thank you.
Thank you for helping and I've understood my problem, I was supposed to use for loop. Everything worked fine but I am not getting the total salary including the 5%. Did I did something wrong in here? Thank you.
1 2 3 4 5
cout<<"\n Name: " << name[Choice - 1] << '\n';
Amount = Amount * 5 /100;
cout<<" \n Pension Contribution 5% of Basic Salary: "<< Amount << endl;
Total = Amount + Amount * 5 /100;
cout<<" \n The Total Salary Including the Basic Salary and Pension Contribution is: "<< Total << endl;
I did Amount and Total to type float and I've re-written the integers to decimals. But I am still not getting my Total salary right. I am sorry if I am consuming your time, I am trying to get it right.
Your maths are wrong. At line 29, the user inputs the salary. But at line 34, you change that salary to be 5% of what it previously was.
Then, at line 36, you set that total to be that value (which is 5% of the original) plus 5% of that 5%. In other words, Total becomes 0.0525 times the original salary, when really you want it to be 1.05 times the original salary.
The first code before the loop, every thing was well, even amount + amount was working fine but in loop things are different... I tried doing variable rather than amount but I get warnings and the same problem. :(
Also - it isn't significant here, but I recommend to use double rather than float as your default choice. Only switch to float if you need to conserve space, for example in a huge array containing millions of values.
// Line 12: Create a variable to hold the grand total of all the employees
double grand_total = 0;
// After line 36: Add the employee's total to the grand total
grand_total += total;
// After line 44: Display the grand total
cout << "Grand total: " << grand_total << endl;
how I can display the total salary of all employees?
This is where it would probably be more useful to use a for-loop rather than a while-loop when entering the values. The difficulty I anticipate here is that there is no guarantee whether or not all the employee data will be entered unless there is some methodical approach such as a for-loop, rather than depending on the whim of the user.
However, that doesn't answer the question. You need to add another variable, call it something like grand_total, and give it an initial value of zero. Then each time you calculate the salary for an employee, add it to that grand total. At the end, print it out.