"Press any key to continue" Help.

closed account (G2hvpfjN)
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.

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
#include <iostream>
using namespace std;
int main()
{
int NumberofEmployees = 3;
	cout<<"Number of Employees = " << NumberofEmployees << endl;
	cout<<"---------------------------" <<endl;
	cout<<"List of Employees," 	    <<endl;
	cout<<"\n-> Frank       ID Number:1"<<endl;
	cout<<"\n-> Sarah       ID Number:2"<<endl;
	cout<<"\n-> John        ID Number:3"<<endl;
	cout<<"---------------------------" <<endl;
//-----------------------------------------------------------------------

int SalaryOfFrank1,SalaryOfSarah2,SalaryOfJohn3,Choice,Amount,Total; 
 
		cout<<"Please enter the employee ID number including the Basic Salary";
		cin >> Choice >> Amount;
		
		
 
	if 	(Choice==1)
	{
 
		SalaryOfFrank1 = Amount;
		cout<<" \n Name: Frank \n\n Basic Salary: "<< SalaryOfFrank1 << endl;
		SalaryOfFrank1 = Amount * 5  /100;
		cout<<" \n Pension Contribution 5% of Basic Salary: "<< SalaryOfFrank1 << endl;
		Total = Amount + Amount * 5 /100;
		cout<<" \n The Total Salary Including the Basic Salary and Pension Contribution is: "<< Total << endl; 
	
	}
	else if (Choice==2)  
	{
		SalaryOfSarah2 = Amount;
		cout<<" \n Name: Sarah \n\n Basic Salary: "<< SalaryOfSarah2 << endl;
		SalaryOfSarah22 = Amount * 5  /100;
		cout<<" \n Pension Contribution 5% of Basic Salary: "<< SalaryOfSarah2 << endl;
		Total = Amount + Amount * 5 /100;
		cout<<" \n The Total Salary Including the Basic Salary and Pension Contribution is: "<< Total << endl;

	}
	else if (Choice==3)
	{
		SalaryOfJohn3 = Amount;
		cout<<" \n Name: John \n\n Basic Salary: "<< SalaryOfJohn3 << endl;
		SalaryOfJohn3 = Amount * 5  /100;
		cout<<" \n Pension Contribution 5% of Basic Salary: "<< SalaryOfJohn3 << endl;
		Total = Amount + Amount * 5 /100;
		cout<<" \n The Total Salary Including the Basic Salary and Pension Contribution is: "<< Total << endl;
		
	}
	else 
	{
		cout<<"ERROR: unknown employee number is entered, please try again.";
	}
	
	return 0;
	
}
Last edited on
> when I try to enter my second statement for my second employee
line 18 is the only place where you take user input
If you want it to repeat you will have to use a loop.
http://www.cplusplus.com/doc/tutorial/control/#loops
closed account (G2hvpfjN)
I basically want my code to be able to let the user enter the salary of Sarah and John after the Frank's. I tried to cin the three employee variables in line 18, but didn't worked :/
Last edited on
It would be useful to make use of arrays as well as loops.

This code isn't complete, consider it as an outline of how things might be done.
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 <iomanip>
#include <string>

using namespace std;

int main()
{
    const int NumberofEmployees = 3;
    string name[NumberofEmployees] =  { "Frank", "Sarah", "John" };
        
    cout << "Number of Employees = " << NumberofEmployees << endl;
    cout << "---------------------------\n\n";
    
    for (int i=0; i<NumberofEmployees; i++)
    {
        cout << "  " << left << setw(12) << name[i] << "ID Number: " << (i+1) << "\n\n";
    }
    cout << "---------------------------\n";


    int Choice = -1;
    
    cout << "Enter 0 to exit program\n";
    while (Choice != 0)
    {
        cout << "\nPlease enter the employee ID number ";
        cin >> Choice;
        
        if (Choice > 0 && Choice <= NumberofEmployees) 
        {
            cout<<"\n Name: " << name[Choice - 1] << '\n';
            // add code to get the salary here
        }
        else if (Choice != 0)
        {
            cout << "ERROR: unknown employee number is entered, please try again.\n";
        }  
    }
}


Though if you want to enter the salary of each employee in turn, then rather than having the user choose, you could just use a for loop and get the salary input for one employee each time.

Arrays tutorial:
http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
closed account (G2hvpfjN)
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; 
Everything worked fine but I am not getting the total salary including the 5%. Did I did something wrong in here?


The problem was noted in an earlier reply,
AbstractionAnon wrote:
Be aware you doing integer arithmetic. You may not get the results you expect.


Here: Amount * 5 /100 all of the values are integers. When dividing an integer by an integer, the result is also an integer. For example 99/100 would give the result 0.

You should make Amount and Total type double. The integers 5 and 100 should be written as 5.0 and 100.0 to ensure that they too are type double.
closed account (G2hvpfjN)
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.



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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	int NumberofEmployees = 3;
	float Total, Amount;
    string name[NumberofEmployees] =  { "Frank", "Sarah", "John" };
        
    cout << "Number of Employees = " << NumberofEmployees << endl;
    cout << "---------------------------\n\n";
    
    for (int i=0; i<NumberofEmployees; i++)
    {
        cout << "  " << left << setw(12) << name[i] << "ID Number: " << (i+1) << "\n\n";
    }
    cout << "---------------------------\n";


    int Choice = -1;
	
	cout << "Enter 0 to exit program\n";
    while (Choice != 0)
    {
        cout << "\nPlease enter the employee ID number ";
        cin >> Choice >> Amount;
        
        if (Choice > 0 && Choice <= NumberofEmployees) 
        {
            cout<<"\n Name: " << name[Choice - 1] << '\n';
			Amount = Amount * 5.0 /100.0;
			cout<<" \n Pension Contribution 5% of Basic Salary: "<< Amount << endl;
			Total = Amount + Amount * 5.0 /100.0;
			cout<<" \n The Total Salary Including the Basic Salary and Pension Contribution is: "<< Total << endl; 
            
        }
        else if (Choice != 0)
        {
            cout << "ERROR: unknown employee number is entered, please try again.\n";
        }  
    }
}
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.
Last edited on
closed account (G2hvpfjN)
At line 29, the user is supposed to enter the Salary. At line 34 the Pension Contribution is right, but my problem is at line 36. Where I must find the Total Salary + the pension contribution.
Last edited on
You need to use a separate variable for the pension contribution, rather than destroying the contents of Amount when the original value is replaced at line 34.

By the way, the array size must be a compile-time constant at line 9:
 
const int NumberofEmployees = 3;

variable-length arrays are not allowed in ISO standard C++ and some compilers will reject your code as an error.
Last edited on
closed account (G2hvpfjN)
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. :(
9
10
	const int NumberofEmployees = 3;
	double Total, Amount, Pension;


34
35
36
			Pension = Amount * 5.0 /100.0;
			cout<<" \n Pension Contribution 5% of Basic Salary: "<< Pension << endl;
			Total = Amount + Pension;



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.
closed account (G2hvpfjN)
After trying your latest reply, it worked. Thank you very much and I am really glad you helped me <3 And yes, sorry for consuming your time and I've learned alot of stuff from you,
cheers.
closed account (G2hvpfjN)
And yes, 1 more thing. Can you give me a hint on how I can display the total salary of all employees?
1
2
3
4
5
6
7
8
//  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.
At line 29, the user is supposed to enter the Salary. At line 34 the Pension Contribution is right, but my problem is at line 36. Where I must find the Total Salary + the pension contribution.

That might be what you think you were doing. My post told you what your code was actually doing.
Topic archived. No new replies allowed.