Salary Range Calculator help please!! =(

This is a discussion on Salary Range Calculator help please!! =( within the C++ Programming forums, part of the General Programming Boards category; I do not know what I am doing wrong, but I can not figure this out for the life of ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    Salary Range Calculator help please!! =(

    I do not know what I am doing wrong, but I can not figure this out for the life of me. Anyone know what is wrong with my program? I am trying to get it to input the sales for the week, then calculate 9% of that plus 200 dollars for each person, and output it on the chart, but for some reason the chart sticks all the people in the over 1000 dollar category. Im so confused =(

    Code:
    int main()    //start program
    {
    
    //declare and initialize array
    int rangeSales[9] = {0};
    
    //declare and initialize variables
    int salary = 200;
    int grossSales = 0;
    int commission = 0;
    
       //Enter the gross sales for each salesperson
       cout << "Enter the gross sales, enter -1 to stop: ";
           cin >> grossSales;
       cout << endl;
    
       while(grossSales != -1)
       {
           commission = grossSales * .09;
           salary = salary + commission;
    
       if(salary >= 200 && salary <= 299)
           rangeSales[0]++;
       if(salary >= 300 && salary <= 399)
           rangeSales[1]++;
       if(salary >= 400 && salary <= 499)
           rangeSales[2]++;
       if(salary >= 500 && salary <= 599)
           rangeSales[3]++;
       if(salary >= 600 && salary <= 699)
           rangeSales[4]++;
       if(salary >= 700 && salary <= 799)
           rangeSales[5]++;
       if(salary >= 800 && salary <= 899)
           rangeSales[6]++;
       if(salary >= 900 && salary <= 999)
           rangeSales[7]++;
       if(salary >= 1000)
           rangeSales[8]++;
    
    
       //Enter the gross sales for each salesperson
       cout << "Enter the gross sales, enter -1 to stop: ";
           cin >> grossSales;
       cout << endl;
    
       } //end while
    
    
    
    //display the count for each salary range
    cout << "The count for $200-$299 range is: " << rangeSales[0] << endl;
    cout << "The count for $300-$399 range is: " << rangeSales[1] << endl;
    cout << "The count for $400-$499 range is: " << rangeSales[2] << endl;
    cout << "The count for $500-$599 range is: " << rangeSales[3] << endl;
    cout << "The count for $600-$699 range is: " << rangeSales[4] << endl;
    cout << "The count for $700-$799 range is: " << rangeSales[5] << endl;
    cout << "The count for $800-$899 range is: " << rangeSales[6] << endl;
    cout << "The count for $900-$999 range is: " << rangeSales[7] << endl;
    cout << "The count for over $1000 range is: " << rangeSales[8] << endl;
    
    
    }

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Hmm, can't get the behavior you describe, but a few notes:
    * Make sure you've included iostream and have "using namespace std;"

    Why do you have "salary = salary + commission;" ? First, it's redundant: "salary += commission", but furthermore, the salary for a salesperson will depend on the order I enter them, which makes no sense. Why does the sales of one salesperson get added to the last salesperson? (If so, I wanna be the last one entered in!) What I think you want is:
    Code:
    salary = 200 + commission;
    But I could be wrong.

    Finally, your if()s can be greatly simplified. Only one of those is going to prove true, so take advantage of the keyword else:
    Code:
    if(salary < 200) cout << "Negative commission?" << endl;
    else if(salary < 300) rangeSales[0]++;
    else if(salary < 400) rangeSales[1]++;
    //etc, etc.
    else rangeSales[8]++;
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    The problem seems to be that you are constantly adding to your salary variable without resetting it for each employee - Unless the company awards money to the next employee based on the previous employee's sales, you probably want a constant which records the basic salary, to be used when determining the total pay.

    Also - this is a style issue, but the name rangeSales[] is very confusing - you're recording the salary range, not the sales range.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2
    Quote Originally Posted by Cactus_Hugger
    Why do you have "salary = salary + commission;" ? First, it's redundant: "salary += commission", but furthermore, the salary for a salesperson will depend on the order I enter them, which makes no sense. Why does the sales of one salesperson get added to the last salesperson? (If so, I wanna be the last one entered in!) What I think you want is:
    Code:
    salary = 200 + commission;
    But I could be wrong.
    OMG how could I have been so stupid

    the reason i did that was because I set salary to 200 in the begining, but I didnt realize that I used the variable twice!!!

    THANKS SO MUCH!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program for finding gross salary
    By newbcore in forum C Programming
    Replies: 7
    Last Post: 12-05-2008, 06:23 PM
  2. Find max profit among input salary
    By hlam in forum C Programming
    Replies: 8
    Last Post: 11-16-2008, 10:30 AM
  3. Srand () w/ range
    By xsbinary in forum C Programming
    Replies: 9
    Last Post: 10-21-2007, 04:24 PM
  4. Need help with small program to calculate a salary
    By Guti14 in forum C++ Programming
    Replies: 13
    Last Post: 01-11-2004, 05:54 PM
  5. salary program heeeeeeeelp....
    By juan in forum C Programming
    Replies: 1
    Last Post: 02-09-2002, 12:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21