C++ Integer and Calculation error

hi all, am having a bit of problem with my codes.

Lst Question: My error is at the Employees numbers. By right i should have the out put of Employee 1, Employee 2, Employee 3 etc. But in my codes My Employee only show up to 9. Any one can help me rectify this problem for Employee Salary and Bonus.

2nd Question: my total number of employees with salary and bonus more than and less than 1000 has gone mess up. Appreciate if anyone could help me fix this.

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
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	char name [100];
	
	cout<<"This is an Calculator to calculate number of Employees having total salary and bonus more than 1000" << endl;
	cout<<"What is your name?" << endl;
	cin.getline (name,100);
	cout<<"Hello, " << name << "." << endl;

	int Salary[10], Bonus[10], lCount=0, gCount=0, i=1, j=1;
	cout<<"Please Enter Employees Salary (Max 10 Employees)" << endl;

	for (i=1; i<10; i++)
{
	cout<<"Enter Employee " << i << " Salary : " ;
	cin>>Salary[i];
	cin.ignore();
}

	for (j=1; j<10; j++)
{
        cout<<"Enter Employee " << j << " Bonus : ";
        cin>>Bonus[j];
        cin.ignore();
}

	for (i=1, j=1; j<10, i<10; i++, j++)
{
	if (Salary[i]+Bonus[j]<1000)
	{lCount++;}
	else 
	{gCount++;}
}

	cout<<"There are " << lCount << " Employees with Salary and Bonus more then 1000\n";
	cout<<"There are " << gCount << " Employees with Salary and Bonus less then 1000\n";

	cin.get();
}
1. for (i=1; i<10; i++) this will loop 9 times only. 1, 2, 3, 4, 5, 6, 7, 8 and 9. Start form 0. And then cout << i+1 instead of 1. Arrays start form 0, not 1.

2.
1
2
3
4
5
6
7
for (i=1, j=1; j<10, i<10; i++, j++)
{
	if (Salary[i]+Bonus[j]<1000)
	{lCount++;}
	else 
	{gCount++;}
}
here i always == j. You don't need both. Just write Salary[i]+Bonus[i] Also, you lCount is the number of employees with less than 1000. Make it > on line 33.
thanks hamsterman, fixed the lst question.
as for 2nd question, i have edit my code base on your recommendation but i get an error when running it. Am i doing the correct editing or ?

1
2
3
4
5
6
7
8
9
10
11
12
13
for (i=1, j=1; j<10, i>10; i++, j++)
{
	if (Salary[i]+Bonus[i]<1000)
	{lCount++;}
	else 
	{gCount++;}
}

	cout<<"There are " << lCount << " Employees with Salary and Bonus more then 1000\n";
	cout<<"There are " << gCount << " Employees with Salary and Bonus less then 1000\n";

	cin.get();
}
This for loop is ill formed.

1) What's the point of treating i and j seperately if i==j? Notice that both vars start at 1 and increment by 1 each iteration, so they'll always be exactly the same. Why not just have one counting variable?

2) the comma operator does not work like you think for the condition:

for (i=1, j=1; j<10, i<10; i++, j++)

If you want to combine multiple checks into the loop condition, the comma operator won't do it. You need to use either the AND operator (&&) or the OR operator (||). && will loop as long as both conditions are true. || will loop as long as either condition is true:

1
2
3
4
for (i=1, j=1; j<10 && i<10; i++, j++)
// or
for (i=1, j=1; j<10 || i<10; i++, j++)
// depending on what you want 


But again you should only need one condition here. I say just get rid of j and use i. Also +1 to what hamsterman said. Start at 0... not 1:

1
2
3
4
5
6
7
for (i=0; i < 10; ++i)
{
	if (Salary[i]+Bonus[i]<1000)
	{lCount++;}
	else 
	{gCount++;}
}
Last edited on
thanks disch for your help. manage to get it done. Was thinking whether will it be able to match employee salary + bonus = total ? Eg, i have 5 employees that has salary AND bonus more than 1000, the final output should be 'there are 5 employees that has salary AND bonus more than 1000' and 'there are 5 employees that has salary AND bonus less than 1000' ?

My code is as below.
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
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	char name [100];
	
	cout<<"This is an Calculator to calculate number of Employees having total salary and bonus more than 1000" << endl;
	cout<<"What is your name?" << endl;
	cin.getline (name,100);
	cout<<"Hello, " << name << "." << endl;

	int Salary[10], Bonus[10], lCount=0, gCount=0, i=0, j=0;
	cout<<"Please Enter Employees Salary (Max 10 Employees)" << endl;

	for (i=0; i<10; i++)
{
	cout<<"Enter Employee " << i+1 << " Salary : " ;
	cin>>Salary[i];
	cin.ignore();
}

	for (j=0; j<10; j++)
{
    cout<<"Enter Employee " << j+1 << " Bonus : ";
    cin>>Bonus[j];
    cin.ignore();
}

	for (i=0; i < 10; ++i)
{
	if (Salary[i]+Bonus[i]>1001)
	{lCount++;}
	else 
	{gCount++;}
}

	cout<<"There are " << lCount << " Employees with Salary and Bonus more then 1000\n";
	cout<<"There are " << gCount << " Employees with Salary and Bonus less then 1000\n";

	cin.get();
}
Topic archived. No new replies allowed.