Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ Salary Array

Reply
Thread Tools

C++ Salary Array

 
 
James Strickland
Guest
Posts: n/a
 
      10-08-2012
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
 
Reply With Quote
 
 
 
 
Melzzzzz
Guest
Posts: n/a
 
      10-08-2012
On Mon, 8 Oct 2012 12:26:10 -0700 (PDT)
James Strickland <(E-Mail Removed)> 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...


 
Reply With Quote
 
 
 
 
James Strickland
Guest
Posts: n/a
 
      10-08-2012
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.
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      10-08-2012
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
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      10-08-2012
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
 
Reply With Quote
 
James Strickland
Guest
Posts: n/a
 
      10-08-2012
<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.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-08-2012
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
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      10-08-2012
On 2012-10-08, James Strickland <(E-Mail Removed)> 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.
 
Reply With Quote
 
Single Stage to Orbit
Guest
Posts: n/a
 
      10-08-2012
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

 
Reply With Quote
 
James Strickland
Guest
Posts: n/a
 
      10-08-2012
<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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Salary Question Teddy MCSD 8 02-17-2005 01:55 AM
Juniper Salary Michael Cisco 3 10-19-2004 05:21 AM
Salary Survey for MCSD David MCSD 15 01-23-2004 10:00 PM
Salary Question?? JO MCSE 15 01-22-2004 12:27 AM
Re: Salary Question?? pheonix1t MCSE 2 11-23-2003 05:02 PM



Advertisments