Hi all

I need assistance with the below code snippit. It calculates how many of salaries entered exceeds R100 000 or how many is below. With the below entries the lower count totals to 4 instead of 5. What am I doing wrong? Still new to this so please excude if the code is everywhere.

Entries:
Enter a yearly salary: R1000
Are there any more values to be input? ('Y' or 'N') y
Enter a yearly salary: R2000
Are there any more values to be input? ('Y' or 'N') y
Enter a yearly salary: R3000
Are there any more values to be input? ('Y' or 'N') y
Enter a yearly salary: R150000
Are there any more values to be input? ('Y' or 'N') y
Enter a yearly salary: R300000
Are there any more values to be input? ('Y' or 'N') n
There are 4 salaries that do not exceed R100 000.
There are 2 salaries that exceeds R100 000.

``````#include <iostream>
using namespace std;

int main()
{
int count, lcount;
float yearlySalary, totalSalary = 0, totalSalaryB = 0, grandTotal;
const int LIMIT = 100000;

count = 0;
lcount = 0;

cout << "Enter a yearly salary: R";
cin >> yearlySalary;
cout << "Are there any more values to be input? ('Y' or 'N') ";

do
{
cout << "Enter a yearly salary: R";
cin >> yearlySalary;
if (yearlySalary > LIMIT)
{
totalSalary += yearlySalary;
count++;
}
else if (yearlySalary <= LIMIT)
totalSalaryB += yearlySalary;
lcount++;

cout << "Are there any more values to be input? ('Y' or 'N') ";
} while (answer == 'Y' || answer == 'y');

cout << "There are " << lcount << " salaries that do not exceed R100 000." << endl;
cout << "There are " << count << " salaries that exceeds R100 000." << endl;

return 0;
}``````

## All 2 Replies

Not familiar with the language you are using but looking at the code my guess would be that it runs through one last time with an undefined input. ie. in your example you give it 5 values then you exit and it runs the "do" code one last time when you exit, making the extra less than 100,000 salary - making it say there are 4 less than 100,000 instead of 3.

Several things:

1. Remove lines 14-17. You are prompting for and reading input that you never use.
2. You don't have brackets around your two statements after the `else if` so only the first is part of the block.
3. Why are you summing the salaries? That wasn't part of the spec.
4. You need to pick better counter names than count and lcount.
5. Your spec doesn't match your code.

For the last point, your spec (incorrectly, I presume) says to count how many are over and how many are under. According to the spec you should ignore those that are equal. Try

``````int over    = 0;
int notover = 0;

do {

cout << "Enter a yearly salary: R";
cin  >> yearlySalary;

if (yearlySalary > LIMIT)
++over;
else
++notover;

cout << "Are there any more values to be input? ('Y' or 'N') ";