Like Tree3Likes
  • 1 Post By Salem
  • 1 Post By laserlight
  • 1 Post By msh

Sales department salary range generator

This is a discussion on Sales department salary range generator within the C++ Programming forums, part of the General Programming Boards category; I am writing this program for my class and am having a problem getting my array to post to the ...

  1. #1
    Registered User erm3r's Avatar
    Join Date
    Sep 2011
    Location
    MIA
    Posts
    5

    Sales department salary range generator

    I am writing this program for my class and am having a problem getting my array to post to the proper ranges. I've been staring at it for hours. Can anyone offer a hint or tip?
    Code:
    //headers 
    #include <cstdlib> 
    #include <iostream> 
    #include <conio.h> 
     
    using namespace std; 
     
    //Start of main 
    int main() 
    { 
         
    //Array chart initilization 
    int salesArray[9]  = {0}; 
     
    //variables and initialization 
    int salary = 0; 
    int grossSales = 0; 
    int commission = 0; 
     
     
     
    //Program introduction and instruction 
    cout << "Department: Sales" << endl; 
    cout << "Salary Range List Generator" << endl; 
    cout << "--------------------------------------" << endl; 
    cout << "Directions: "; 
    cout << "This program will allow you to enter gross sales for each employee." << endl; 
    cout << "Once you have finished entering their sales press -1 for a compiled list of ranges" << endl << endl; 
    cout << "Gross Sales (or -1): "; 
    cin >> grossSales; 
    cout << endl; 
     
    // while loop to update salary array chart 
     
    while (grossSales != -1) 
    { 
           
          salary = 200 + commission; 
          commission = grossSales * .09; 
           
          if (salary < 200)  
          cout << "invalid entry" << endl; 
           
          else if (salary >= 200 && salary <= 299) 
          salesArray[0]++; 
           
          else if (salary >= 300 && salary <= 399) 
          salesArray[1]++; 
           
          else if (salary >= 400 && salary <= 499) 
          salesArray[2]++; 
           
          else if (salary >= 500 && salary <= 599) 
          salesArray[3]++; 
           
          else if (salary >= 600 && salary <= 699) 
          salesArray[4]++; 
           
          else if (salary >= 700 && salary <= 799) 
          salesArray[5]++; 
           
          else if (salary >= 800 && salary <= 899) 
          salesArray[6]++; 
           
          else if (salary >= 900 && salary <= 999) 
          salesArray[7]++; 
           
          else salesArray[8]++; 
     
          cout << "Enter Gross Sales (or press -1):"; 
          cin >> grossSales; 
          cout << endl; 
          }//end while 
           
          //salary range list generator 
          cout << "------------------------------------" << endl; 
          cout << "Salary ranges are listed below: " << endl; 
          cout << "------------------------------------" << endl; 
          cout << "$200-299: " << salesArray[0] << endl; 
          cout << "$300-399: " << salesArray[1] << endl; 
          cout << "$400-499: " << salesArray[2] << endl; 
          cout << "$500-599: " << salesArray[3] << endl; 
          cout << "$600-699: " << salesArray[4] << endl; 
          cout << "$700-799: " << salesArray[5] << endl; 
          cout << "$800-899: " << salesArray[6] << endl; 
          cout << "$900-999: " << salesArray[7] << endl; 
          cout << "$1000 +: " << salesArray[8] << endl; 
          cout << "End of List " << endl; 
           
          getch(); 
          return 0; 
           
          }//end main
    Attached Files Attached Files
    Last edited by Salem; 09-08-2011 at 09:17 AM. Reason: Inlined code for easier life

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    What numbers do you type in?

    Also, swap these two lines over, otherwise salary isn't what you think.
    salary = 200 + commission;
    commission = grossSales * .09;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User erm3r's Avatar
    Join Date
    Sep 2011
    Location
    MIA
    Posts
    5
    I run the program and enter the following gross pay: 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 and -1

    the chart of ranges post 9 entries to the 200-299 category.

    Also, thanks so much for noticing the commission mishap. I just noticed it a minute ago as well and changed it accordingly.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    Well even 1000 * .09 is only 90, which doesn't get you out of the 200 to 299 band.

    Try bigger numbers, or a different scale factor.
    erm3r likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User erm3r's Avatar
    Join Date
    Sep 2011
    Location
    MIA
    Posts
    5
    Quote Originally Posted by Salem View Post
    Well even 1000 * .09 is only 90, which doesn't get you out of the 200 to 299 band.

    Try bigger numbers, or a different scale factor.
    Wow... you can tell my brain is fried. I shouldn't try to do so many things at once. Right now I'm doing accounts receivables at my job, studying for a java test and testing my code for tonight's c++ class... I think I need to take a moment to myself so my brain can refresh lol.

    Thanks so much for all of your help Salem! I think I got everything working the way I need to.

  6. #6
    Registered User erm3r's Avatar
    Join Date
    Sep 2011
    Location
    MIA
    Posts
    5
    I changed the code to vector and changed a couple of arbitrary things. The final product works well.

    Code:
    //headers
    #include <cstdlib>
    #include <iostream>
    #include <conio.h>
    #include <vector>
    
    using namespace std;
    
    //Start of main
    int main()
    {
        
    //Array chart initilization
    vector <int> salesVector(9);
    
    //variables and initialization
    int salary = 0;
    int grossSales = 0;
    int commission = 0;
    
    
    
    //Program introduction and instruction
    cout << "Department: Sales" << endl;
    cout << "Salary Range List Generator" << endl;
    cout << "--------------------------------------" << endl;
    cout << "Directions: ";
    cout << "This program will allow you to enter gross sales for each employee." << endl;
    cout << "Once you have finished entering their sales press -1 for a compiled list of ranges" << endl << endl;
    cout << "Gross Sales (or -1): ";
    cin >> grossSales;
    cout << endl;
    
    // while loop to update salary array chart
    
    while (grossSales != -1)
    {
          commission = (int) (grossSales * .09);
          salary = 200 + commission;
          
          if (salary < 200) 
          cout << "invalid entry" << endl;
          
          else if (salary >= 200 && salary <= 299)
          salesVector[0]++;
          
          else if (salary >= 300 && salary <= 399)
          salesVector[1]++;
          
          else if (salary >= 400 && salary <= 499)
          salesVector[2]++;
          
          else if (salary >= 500 && salary <= 599)
          salesVector[3]++;
          
          else if (salary >= 600 && salary <= 699)
          salesVector[4]++;
          
          else if (salary >= 700 && salary <= 799)
          salesVector[5]++;
          
          else if (salary >= 800 && salary <= 899)
          salesVector[6]++;
          
          else if (salary >= 900 && salary <= 999)
          salesVector[7]++;
          
          else salesVector[8]++;
    
          cout << "Enter Gross Sales (or press -1):";
          cin >> grossSales;
          cout << endl;
          }//end while
          
          //salary range list generator
          cout << "------------------------------------" << endl;
          cout << "Salary ranges are listed below: " << endl;
          cout << "------------------------------------" << endl <<endl;
          cout << "$200-299: " << salesVector[0] << endl;
           cout << "------------------------------------" << endl;
          cout << "$300-399: " << salesVector[1] << endl;
           cout << "------------------------------------" << endl;
          cout << "$400-499: " << salesVector[2] << endl;
           cout << "------------------------------------" << endl;
          cout << "$500-599: " << salesVector[3] << endl;
           cout << "------------------------------------" << endl;
          cout << "$600-699: " << salesVector[4] << endl;
           cout << "------------------------------------" << endl;
          cout << "$700-799: " << salesVector[5] << endl;
           cout << "------------------------------------" << endl;
          cout << "$800-899: " << salesVector[6] << endl;
           cout << "------------------------------------" << endl;
          cout << "$900-999: " << salesVector[7] << endl;
           cout << "------------------------------------" << endl;
          cout << "$1000 + : " << salesVector[8] << endl;
           cout << "------------------------------------" << endl;
          cout << "End of List. Thank you! " << endl;
          
          getch();
          return 0;
          
          }//end main
    Last edited by erm3r; 09-08-2011 at 11:20 AM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Here we go again...

    All of this...
    Code:
         if (salary < 200)  
          cout << "invalid entry" << endl; 
           
          else if (salary >= 200 && salary <= 299) 
          salesArray[0]++; 
           
          else if (salary >= 300 && salary <= 399) 
          salesArray[1]++; 
           
          else if (salary >= 400 && salary <= 499) 
          salesArray[2]++; 
           
          else if (salary >= 500 && salary <= 599) 
          salesArray[3]++; 
           
          else if (salary >= 600 && salary <= 699) 
          salesArray[4]++; 
           
          else if (salary >= 700 && salary <= 799) 
          salesArray[5]++; 
           
          else if (salary >= 800 && salary <= 899) 
          salesArray[6]++; 
           
          else if (salary >= 900 && salary <= 999) 
          salesArray[7]++; 
           
          else salesArray[8]++;
    ... can be replaced with....

    Code:
        if (salary < 200)
          cout << "invalid";
        if (salary > 999)
          salary =  1000;
    
       salesvector[(salary / 100) - 2]++;
    Last edited by CommonTater; 09-08-2011 at 11:41 AM. Reason: Semicolon... semicolon, everywhere a semicolon;

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by CommonTater
    All of this can be replaced with....
    Note that CommonTater's example gives the general idea. You should check to see if it is correct instead of using it blindly.
    CommonTater likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Note that CommonTater's example gives the general idea. You should check to see if it is correct instead of using it blindly.
    Absolutely... it's a little nudge in a simpler direction, definately not intended to be used scoop and poop fashion...
    In fact anyone who would use code from other sources (especailly a stranger on the internet) without vetting and testing is just begging for disaster.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by CommonTater View Post
    In fact anyone who would use code from other sources (especailly a stranger on the internet) without vetting and testing is just begging for disaster.
    Especially your code......
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Especially your code......
    Yep... I've never said my code did anything more than look pretty on a screen. (and some even debate that!)

  12. #12
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Code:
    //headers 
    #include <cstdlib>    // unnecessary
    #include <iostream> 
    #include <conio.h>    // unnecessary
    Please don't include headers blindly. You're not using <cstdlib> and neither should you in a C++ program, and <conio.h> would have gone the way of the dodo if not for the instructors who consistently fail to keep themselves and their tools up-to-date. If you want to keep the console open just use `cin.get();`.
    CommonTater likes this.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  13. #13
    Registered User erm3r's Avatar
    Join Date
    Sep 2011
    Location
    MIA
    Posts
    5
    Wow. Thanks so much for all of your replies! I'm slowly starting to get it all sorted out.

    commontater:
    Thanks so much for helping to simplify my code. I will mess around with it and see what sticks. I'm still in the learning phases and its really nice to know that there are great people like you out there helping us simple folk =P.

    Msh: Our instructor told us to use the conio header but I do agree with you and have done research which has pointed me in the cin.get(); direction. I'll try not to make it a habit.

    Hope everyone has a great weekend!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sales tax
    By Mark_Guy in forum C Programming
    Replies: 0
    Last Post: 10-22-2008, 08:47 AM
  2. Salary Range Calculator help please!! =(
    By bambino in forum C++ Programming
    Replies: 3
    Last Post: 04-20-2006, 06:17 PM
  3. Two-dimensional array Sales
    By mikeprogram in forum C++ Programming
    Replies: 7
    Last Post: 12-04-2005, 09:32 PM
  4. Replies: 0
    Last Post: 05-21-2005, 09:42 PM
  5. donut sales take a dive
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 07-03-2004, 01:34 PM

Tags for this Thread


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