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 3. 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;
char answer;

count = 0;
lcount = 0;

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

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') ";
cin >> answer;
} 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;
}``````

## Recommended Answers

You need to put brace brackets around your two statements. Indenting is not enough.

``````    else if (yearlySalary <= LIMIT) {
totalSalaryB += yearlySalary;
lcount++;
}``````

But your problems extend beyond that. If the code, according to your spec, is only supposed to calculate the number of …

## All 3 Replies

You need to put brace brackets around your two statements. Indenting is not enough.

``````    else if (yearlySalary <= LIMIT) {
totalSalaryB += yearlySalary;
lcount++;
}``````

But your problems extend beyond that. If the code, according to your spec, is only supposed to calculate the number of salaries below and above a limit then why are you totalling the actual salaries? And why are you bothering to do prompting and input above the loop? And while I am on it, count and lcount are poor variaible names.

Because this is so close to your actual code I don't mind posting this.

``````int main()
{
float yearlySalary;
const int LIMIT = 100000;
char answer;
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') ";
cin  >> answer;
} while (answer == 'Y' || answer == 'y');

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

return 0;
}``````

By the way, `Code Snippet` is to be used only for fully debugged and working (and fully commented/documented) code snippets. It is not to be used for posting buggy code. You should be posting as `Discussion/Question`.

One final comment. Your spec said to calculate the number of salaries over a limit and how many below a limit. Ac cording to the stated spec you should ignore anything that equals the limit. You'll have to adjust either the spec or the code.

commented: Great Scott! Code to the spec and wait for the client to tell you that's not what they meant. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.