Arrays

Hey so I am in a class where we are using Microsoft C++ and right now we are working on a program using arrays. We are given a problem where we have to sort people's salaries into categories of how much money they earn. So if they make $200-$299 they are in a category and if the earn $300-$399 they are in a different category and so on. I need to add to the previous amounts. So if the earned salary is $290 it will go into the $200-$299 category and then you will ask to calculate another salary. How do i keep the 1 in the category and still do another salary. So in essence, i need to make it remember everything it calculates and allow it to continue to calculate more salaries.

#include <iostream>
#include <conio.h>

using namespace std;

int main(){

char again='y';
while(again == 'y')
{
system("CLS");

int salary[9] = {0}, grosssales, earnedsalary;

cout << "Enter the salesperson's gross sales: $";

cin >> grosssales;

earnedsalary = 200 + grosssales*.09;

cout << "The earned salary for this salesperson is: $" << earnedsalary << endl;



if(200 <= earnedsalary && earnedsalary <= 299)
{
salary[0]++;
}
//cout << "\nSalaries in the range of $200-$299: " << salary[0];


if(300 <= earnedsalary && earnedsalary <= 399)
{
salary[1]++;
}
//cout << "\nSalaries in the range of $300-$399: " << salary[1];


if(400 <= earnedsalary && earnedsalary <= 499)
{
salary[2]++;
}
//cout << "\nSalaries in the range of $400-$499: " << salary[2];


if(500 <= earnedsalary && earnedsalary <= 599)
{
salary[3]++;
}
//cout << "\nSalaries in the range of $500-$599: " << salary[3];


if(600 <= earnedsalary && earnedsalary <= 699)
{
salary[4]++;
}
//cout << "\nSalaries in the range of $600-$699: " << salary[4];


if(700 <= earnedsalary && earnedsalary <= 799)
{
salary[5]++;
}
//cout << "\nSalaries in the range of $700-$799: " << salary[5];


if(800 <= earnedsalary && earnedsalary <= 899)
{
salary[6]++;
}
//cout << "\nSalaries in the range of $800-$899: " << salary[6];


if(900 <= earnedsalary && earnedsalary <= 999)
{
salary[7]++;
}
//cout << "\nSalaries in the range of $900-$999: " << salary[7];


if(earnedsalary >= 1000)
{
salary[8]++;
}
//cout << "\nSalaries greater than $1000: " << salary[8];



cout << "\nSalaries in the range of $200-$299: " << salary[0];

cout << "\nSalaries in the range of $300-$399: " << salary[1];

cout << "\nSalaries in the range of $400-$499: " << salary[2];

cout << "\nSalaries in the range of $500-$599: " << salary[3];

cout << "\nSalaries in the range of $600-$699: " << salary[4];

cout << "\nSalaries in the range of $700-$799: " << salary[5];

cout << "\nSalaries in the range of $800-$899: " << salary[6];

cout << "\nSalaries in the range of $900-$999: " << salary[7];

cout << "\nSalaries greater than $1000: " << salary[8];

cout << "\n\nWould you like to calculate another saleperson's wage? y/n" << endl;
again = 'y';

getch();
}
}
You have a couple of basic errors that are going to stop you until you fix them.

First of all, you reinitialize your salary array every time your loop runs -- so put the declaration before your while loop.

Second of all in your calculations you will add the earned salary to too many arrays (if you were doing that -- All you are doing is counting the number of salaries in a range.)

To fix your comparisons, you are going to have to use "else-ifs".
#include <iostream>
#include <conio.h>

using namespace std;

int main(){

char again='y';
while(again == 'y')

{
int salary[9], grosssales, earnedsalary;

system("CLS");

cout << "Enter the salesperson's gross sales: $";

cin >> grosssales;

earnedsalary = 200 + grosssales*.09;

cout << "The earned salary for this salesperson is: $" << earnedsalary << endl;


if(200 <= earnedsalary && earnedsalary <= 299)
{
salary[0]++;
}
//cout << "\nSalaries in the range of $200-$299: " << salary[0];


else if(300 <= earnedsalary && earnedsalary <= 399)
{
salary[1]++;
}
//cout << "\nSalaries in the range of $300-$399: " << salary[1];


else if(400 <= earnedsalary && earnedsalary <= 499)
{
salary[2]++;
}
//cout << "\nSalaries in the range of $400-$499: " << salary[2];


else if(500 <= earnedsalary && earnedsalary <= 599)
{
salary[3]++;
}
//cout << "\nSalaries in the range of $500-$599: " << salary[3];


else if(600 <= earnedsalary && earnedsalary <= 699)
{
salary[4]++;
}
//cout << "\nSalaries in the range of $600-$699: " << salary[4];


else if(700 <= earnedsalary && earnedsalary <= 799)
{
salary[5]++;
}
//cout << "\nSalaries in the range of $700-$799: " << salary[5];


else if(800 <= earnedsalary && earnedsalary <= 899)
{
salary[6]++;
}
//cout << "\nSalaries in the range of $800-$899: " << salary[6];


else if(900 <= earnedsalary && earnedsalary <= 999)
{
salary[7]++;
}
//cout << "\nSalaries in the range of $900-$999: " << salary[7];


else if(earnedsalary >= 1000)
{
salary[8]++;
}
//cout << "\nSalaries greater than $1000: " << salary[8];



cout << "\nSalaries in the range of $200-$299: " << salary[0];

cout << "\nSalaries in the range of $300-$399: " << salary[1];

cout << "\nSalaries in the range of $400-$499: " << salary[2];

cout << "\nSalaries in the range of $500-$599: " << salary[3];

cout << "\nSalaries in the range of $600-$699: " << salary[4];

cout << "\nSalaries in the range of $700-$799: " << salary[5];

cout << "\nSalaries in the range of $800-$899: " << salary[6];

cout << "\nSalaries in the range of $900-$999: " << salary[7];

cout << "\nSalaries greater than $1000: " << salary[8];

cout << "\n\nWould you like to calculate another saleperson's earned salary? y/n" << endl;
again = 'y';


getch();
}
}
is this better??
Topic archived. No new replies allowed.