C++ Salary Array

Discussion in 'C++' started by James Strickland, Oct 8, 2012.

  1. Greetings all,

    I'm having some issues running this code. http://pastebin.com/8FtC23DF

    it seems to stop running right after user inputs grossSales.

    Can someone please help me out with where I'm going wrong.

    THanks,
    James
    James Strickland, Oct 8, 2012
    #1
    1. Advertising

  2. James Strickland

    Melzzzzz Guest

    On Mon, 8 Oct 2012 12:26:10 -0700 (PDT)
    James Strickland <> wrote:

    > Greetings all,
    >
    > I'm having some issues running this code. http://pastebin.com/8FtC23DF
    >
    > it seems to stop running right after user inputs grossSales.
    >
    > Can someone please help me out with where I'm going wrong.
    >
    > THanks,
    > James


    Problem is here:

    // Initialize array
    int rangeSales[8] = {0}; // 0 to 9

    That array index goes from 0 to 7 , but you are indexing it
    till 8.
    So increase array size...
    Melzzzzz, Oct 8, 2012
    #2
    1. Advertising

  3. On Monday, 8 October 2012 14:34:45 UTC-5, Melzzzzz wrote:
    <SNIP>
    >
    > Problem is here:
    >
    >
    >
    > // Initialize array
    >
    > int rangeSales[8] = {0}; // 0 to 9
    >
    >
    >
    > That array index goes from 0 to 7 , but you are indexing it
    >
    > till 8.
    >
    > So increase array size...


    Increasing the array size didn't work. rangeSales[8] is correct to declare an array for 9 items. (200,300,400,500,600,700,800,900,1000+).

    The program is not looping... Not sure where the hang up is.
    James Strickland, Oct 8, 2012
    #3
  4. James Strickland

    Rui Maciel Guest

    James Strickland wrote:

    > Increasing the array size didn't work. rangeSales[8] is correct to
    > declare an array for 9 items.


    No, it's wrong. int rangeSales[8] is a declaration of an array with 8
    elements, numbered 0 to 7.


    Rui Maciel
    Rui Maciel, Oct 8, 2012
    #4
  5. James Strickland

    Rui Maciel Guest

    James Strickland wrote:

    > Greetings all,
    >
    > I'm having some issues running this code. http://pastebin.com/8FtC23DF
    >
    > it seems to stop running right after user inputs grossSales.


    Quite the opposite. You've defined an endless loop. Your code boils down
    to the following:

    <code>

    int main(void)
    {
    // snip
    int grossSales = 0;

    // snip

    cin >> grossSales;

    // snip

    while(grossSales != -1)
    {
    //snip
    }

    return 0;
    }
    </code>


    Rui Maciel
    Rui Maciel, Oct 8, 2012
    #5
  6. <SNIP>


    > No, it's not. The number in brackets is not the largest index, but the
    >
    > total number of objects, so rangeSales holds only 8 objects. Such errors
    >
    > are typically not found by the compiler, but the program will go crazy
    >
    > in all possible ways, including that it's working by chance.
    >
    >
    >
    > About the loop: Why dou you think the while condition should ever
    >
    > change? I assume that you want to have input in every iteration.....
    >
    >
    >
    > Christian


    OK, so I made a few changes and now it loops. I changed rangeSales[8] to rangeSales[9] and put an input in my loop. It now loops, but the math is not working right.

    When you input numbers, let's say 800, it counts it in the "$200-300" range.
    James Strickland, Oct 8, 2012
    #6
  7. James Strickland

    Ian Collins Guest

    On 10/09/12 09:36, James Strickland wrote:

    Please wrap your lines!

    For such a small code example, posting here is much better than posting
    link many won't bother following.

    > OK, so I made a few changes and now it loops. I changed rangeSales[8] to rangeSales[9] and put an input in my loop. It now loops, but the math is not working right.
    >
    > When you input numbers, let's say 800, it counts it in the "$200-300" range.


    Which is what the specification states: 200 + 9% of 800 -s 272!

    --
    Ian Collins
    Ian Collins, Oct 8, 2012
    #7
  8. James Strickland

    Ike Naar Guest

    On 2012-10-08, James Strickland <> wrote:
    > OK, so I made a few changes and now it loops.
    > I changed rangeSales[8] to rangeSales[9] and put an input in my loop.
    > It now loops, but the math is not working right.
    > When you input numbers, let's say 800, it counts it in the "$200-300" range.


    Can you make the new, updated code available?
    By the way, shouldn't thie

    if (salary >= 200 && salary <= 300)

    be

    if (salary >= 200 && salary <= 299)

    That said, the source of your problem is that the salary is
    accumulated in the the loop while it should not.
    Your loop looks like this:

    salaray = 200;
    cin >> grossSales;
    while (grossSales != -1)
    {
    commission = grossSales * 0.09;
    salary = salary + commission;

    This works correctly only in the first iteration, when salary still
    equals 200; the salary computed in, say, the second iteration,
    will be the sum of the first salary and the second commission,
    while it should be the sum of 200 and the second commission.
    Ike Naar, Oct 8, 2012
    #8
  9. On Mon, 2012-10-08 at 12:26 -0700, James Strickland wrote:
    > Greetings all,
    >
    > I'm having some issues running this code. http://pastebin.com/8FtC23DF
    >
    > it seems to stop running right after user inputs grossSales.
    >
    > Can someone please help me out with where I'm going wrong.


    You're looping without asking for further input! It will loop forever
    unless you move the code that asks for the sales details within the
    loop.
    --
    Tactical Nuclear Kittens
    Single Stage to Orbit, Oct 8, 2012
    #9
  10. <SNIP>

    > This works correctly only in the first iteration, when salary still
    >
    > equals 200; the salary computed in, say, the second iteration,
    >
    > will be the sum of the first salary and the second commission,
    >
    > while it should be the sum of 200 and the second commission.


    Ok, I'm not sure how to tackle that... should salary be a constant, would that fix it?
    James Strickland, Oct 8, 2012
    #10
  11. James Strickland

    Ian Collins Guest

    On 10/09/12 10:09, James Strickland wrote:
    > <SNIP>
    >
    >> This works correctly only in the first iteration, when salary still
    >>
    >> equals 200; the salary computed in, say, the second iteration,
    >>
    >> will be the sum of the first salary and the second commission,
    >>
    >> while it should be the sum of 200 and the second commission.

    >
    > Ok, I'm not sure how to tackle that... should salary be a constant, would that fix it?


    Something like

    outside the loop:

    const int baseSalary = 200;

    inside the loop:

    int commission = grossSales * .09;
    int salary = baseSalary + commission;

    Note the variables are declared where they belong inside the loop.

    --
    Ian Collins
    Ian Collins, Oct 8, 2012
    #11
  12. James Strickland

    Ike Naar Guest

    On 2012-10-08, James Strickland <> wrote:
    ><SNIP>
    >
    >> This works correctly only in the first iteration, when salary still
    >>
    >> equals 200; the salary computed in, say, the second iteration,
    >>
    >> will be the sum of the first salary and the second commission,
    >>
    >> while it should be the sum of 200 and the second commission.

    >
    > Ok, I'm not sure how to tackle that... should salary be a constant, would that fix it?


    If salary is a constant, it cannot be changed after initialization.
    The simplest way is probaby to use the proper values in the
    computation of salary inside the loop:

    int salary = 200;
    int commission = 0;
    /* ... */
    while (grossSales != -1)
    {
    commission = grossSales * 0.09;
    salary = 200 + commission;
    if (salary >= 200 && salary <= 299)
    /* ... */
    }

    If you want to use a named constant for the $200 base salary,
    which would probably be a good idea, you could do something like

    const int baseSalary = 200;
    int salary = 200;
    int commission = 0;
    /* ... */
    while (grossSales != -1)
    {
    commission = grossSales * 0.09;
    salary = baseSalary + commission;
    if (salary >= 200 && salary <= 299)
    /* ... */
    }

    Note that commission and salary are never used outside the loop,
    except for initializing them with some meaningless value.
    You could move them inside the loop, and initialize them
    with meaningful values:

    const int baseSalary = 200;
    /* ... */
    while (grossSales != -1)
    {
    int commission = grossSales * 0.09;
    int salary = baseSalary + commission;
    if (salary >= 200 && salary <= 299)
    /* ... */
    }

    and now they could even be made constant:

    const int baseSalary = 200;
    /* ... */
    while (grossSales != -1)
    {
    const int commission = grossSales * 0.09;
    const int salary = baseSalary + commission;
    if (salary >= 200 && salary <= 299)
    /* ... */
    }
    Ike Naar, Oct 8, 2012
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. The job lady
    Replies:
    0
    Views:
    548
    The job lady
    Jan 13, 2006
  2. Simon Harris

    Salary Expectations (UK)

    Simon Harris, Oct 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,620
    Mark Rae
    Oct 16, 2004
  3. Lucas Tam
    Replies:
    5
    Views:
    9,535
    Karl Seguin
    May 13, 2005
  4. Balu
    Replies:
    1
    Views:
    345
    Juan T. Llibre
    Feb 24, 2006
  5. Balu
    Replies:
    1
    Views:
    339
    darrel
    Feb 25, 2006
Loading...

Share This Page