C++ Salary Array

  • Thread starter James Strickland
  • Start date
J

James Strickland

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
 
M

Melzzzzz

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...
 
J

James Strickland

On Monday, 8 October 2012 14:34:45 UTC-5, Melzzzzz wrote:
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.
 
R

Rui Maciel

James said:
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
 
R

Rui Maciel

James said:
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
 
J

James Strickland

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.
 
I

Ian Collins

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!
 
I

Ike Naar

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.
 
S

Single Stage to Orbit

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.
 
J

James Strickland

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?
 
I

Ian Collins

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.
 
I

Ike Naar

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)
/* ... */
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top