This is what I need to do:

Use a single-subscripted array to solve the following problem: A company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9% of $5000 or a total of $650. Write a program (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson’s salary is truncated to an integer amount):
a) $200-$299
b) $300-$399
c) $400-$499
d) $500-$599
e) $600-$699
f) $700-$799
g) $800-$899
h) $900-$999
i) $1000 and over
Summarize the results in tabular format

This is what I've got:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>

void displayResults( );

int main()
{
    int sales;
    double salary;
    int counter[ 9 ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    
    
    
    
    cout << "Enter a sales amount: (negative to end)";
    cin >> sales;
    
    
    while (sales > 0){
         salary = (sales * .09) + 200;
    
         if ( (salary >= 200) && (salary < 300) )
              ++counter[ 0 ];
         else if ( (salary >= 300) && (salary < 400) )
              ++counter[ 1 ];
         else if ( (salary >= 400) && (salary < 500) )
              ++counter[ 2 ];
         else if ( (salary >= 500) && (salary < 600) )
              ++counter[ 3 ];
         else if ( (salary >= 600) && (salary < 700) )
              ++counter[ 4 ];
         else if ( (salary >= 700) && (salary < 800) )
              ++counter[ 5 ];
         else if ( (salary >= 800) && (salary < 900) )
              ++counter[ 6 ];
         else if ( (salary >= 900) && (salary < 1000) )
              ++counter[ 7 ];
         else
              ++counter[ 8 ];
    cout << "Enter a sales amount: (negative to end)";
    cin >> sales;
    }        
    
    displayResults();

 
 system("pause");
 return 0;
}

void displayResults( )
{
     int counter[ 9 ];
     
     cout << " Range        Number" << endl;
     cout << "$200 - $299\t  " << counter[ 0 ] << endl;
     cout << "$300 - $399\t  " << counter[ 1 ] << endl;
     cout << "$400 - $499\t  " << counter[ 2 ] << endl;
     cout << "$500 - $599\t  " << counter[ 3 ] << endl;
     cout << "$600 - $699\t  " << counter[ 4 ] << endl;
     cout << "$700 - $799\t  " << counter[ 5 ] << endl;
     cout << "$800 - $899\t  " << counter[ 6 ] << endl;
     cout << "$900 - $999\t  " << counter[ 7 ] << endl;
     cout << "Over $1000 \t  " << counter[ 8 ] << endl;
}

I am fairly new to C++, and am not sure how to fix it. I have used QBasic and C before, but not extensively. The program sort of works...but it doesn't display the proper results in the function. If I put a regular cout << counter[whatever]; into the main function, it displays the proper results...any ideas how I can transfer the correct results into the function? I have read a lot about passing arrays into functions, but can't figure out what exactly to add to my program to make it do what it is supposed to. Any help would be greatly appreciated, thank you! =)

~TheSilverFox

The slot determination is easy, below range, range, over range.

int counter[ 9 ];

i = counter[ n ] ; is the value so...
int *pCnt = &counter[ n ];
Gives you a pointer to the integer located at cell [n].
Or try this.

int *pCnt = counter;
while( n-- )
{
          int i = *pCnt++;

}

I tried adding that into my code:

void displayResults( )
{

int counter[ 9 ];
int n;
int i;

i = counter[ n ] ;
int *pCnt = &counter[ n ];

cout << " Range Number" << endl;
cout << "$200 - $299\t " << &counter[ 0 ] << endl;
cout << "$300 - $399\t " << &counter[ 1 ] << endl;
cout << "$400 - $499\t " << &counter[ 2 ] << endl;
cout << "$500 - $599\t " << &counter[ 3 ] << endl;
cout << "$600 - $699\t " << &counter[ 4 ] << endl;
cout << "$700 - $799\t " << &counter[ 5 ] << endl;
cout << "$800 - $899\t " << &counter[ 6 ] << endl;
cout << "$900 - $999\t " << &counter[ 7 ] << endl;
cout << "Over $1000 \t " << &counter[ 8 ] << endl;
}

and it just caused my .exe to crash. I wasn't 100% sure how to add it in properly though. Do I need to declare n better? Where do I use i? Thank you in advance...

First problem that array is empty (not initialized).

Second, you only want the value, not the address for printing!

// YOU need t fill counter
// and initialize n like in a loop reference or something.

//i = counter[ n ] ; 
int *pCnt = &counter[ n ];
for (n=0; n < 9; n++)
{
    counter[n] = n;
}



cout << " Range Number" << endl;
cout << "$200 - $299\t " << counter[ 0 ] << endl;
cout << "$300 - $399\t " << counter[ 1 ] << endl;
cout << "$400 - $499\t " << counter[ 2 ] << endl;
cout << "$500 - $599\t " << counter[ 3 ] << endl;
cout << "$600 - $699\t " << counter[ 4 ] << endl;
cout << "$700 - $799\t " << counter[ 5 ] << endl;
cout << "$800 - $899\t " << counter[ 6 ] << endl;
cout << "$900 - $999\t " << &counter[ 7 ] << endl;
cout << "Over $1000 \t " << counter[ 8 ] << endl;
}

void displayResults( );

change this to:

void displayResults(int *, int);

Change declaration of counter to this:
const int SIZE = 9;
int counter;

call displayResults() like this:
dipsplayResults(counter, SIZE);

Try to define it on your own.

Ok. This is what the function reads now:

void displayResults(int*, int)
{
     int n;
     const int SIZE = 9;
     int counter[SIZE];
     int *pCnt = &counter[ n ];
     for (n=0; n < 9; n++)
         {
            counter[n] = n;
         }

     
     cout << " Range        Number" << endl;
     cout << "$200 - $299\t  " << counter[ 0 ] << endl;
     cout << "$300 - $399\t  " << counter[ 1 ] << endl;
     cout << "$400 - $499\t  " << counter[ 2 ] << endl;
     cout << "$500 - $599\t  " << counter[ 3 ] << endl;
     cout << "$600 - $699\t  " << counter[ 4 ] << endl;
     cout << "$700 - $799\t  " << counter[ 5 ] << endl;
     cout << "$800 - $899\t  " << counter[ 6 ] << endl;
     cout << "$900 - $999\t  " << counter[ 7 ] << endl;
     cout << "Over $1000 \t  " << counter[ 8 ] << endl;
}

The program works with no compiler errors, but the results show just 0-8 in sequential order for the Number corresponding to each range. I believe that is because somewhere, a pointer isn't pointing to the original int counter[9]; I created...I'm not sure which parts (if any) should be omitted or edited...

Thank you very much for your quick responses and help! I greatly appreciate it!!

Sorry I think I confused you

void displayResults(int*pCnt, int , nCnt)
{
cout << " Range Number" << endl;
cout << "$200 - $299\t " << *(pCnt+0) << endl;
cout << "$300 - $399\t " << *(pCnt+1) << endl;

etc.

Sorry let's try that again.


void displayResults(int*pCnt, )
{
cout << " Range Number" << endl;
cout << "$200 - $299\t " << *(pCnt+0) << endl;
cout << "$300 - $399\t " << *(pCnt+1) << endl;

etc.

In this program it's probably easier to write out the display as opposed to using a loop so you could follow this outline:

void display(int *);

int main()
{
    const int SIZE = 9;
    int counter[SIZE] = {0};

    //do dah

   display(counter);

   //finish up
}

void display(int * counter)
{
   cout << " Range Number" << endl;
   cout << "$200 - $299\t " << counter[0] << endl;
   cout << "$300 - $399\t " << counter[1] << endl;
   //etc
}

You want to pass counter as an argument to display(), not declare it in display().

Ok, I did that, wildgoose. The compiler is now giving me an error that the line

int *pCnt = &counter[ n ];

shadows a parameter. I can't delete the declaration, because that's what links it to the original array... Also, I assumed that when you typed


void displayResults(int*pCnt, )

it should be


void displayResults(int*pCnt, int )

so that's what I made it. Was that wrong?

Woo! The program works as planned! This is the final structure:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>

void displayResults(int * counter);

int main()
{
    
    int sales;
    double salary;
    const int SIZE = 9;
    int counter[SIZE] = {0};
    
    
    
    
    cout << "Enter a sales amount: (negative to end)";
    cin >> sales;
    
    
    while (sales > 0){
         salary = (sales * .09) + 200;
    
         if ( (salary >= 200) && (salary < 300) )
              ++counter[ 0 ];
         else if ( (salary >= 300) && (salary < 400) )
              ++counter[ 1 ];
         else if ( (salary >= 400) && (salary < 500) )
              ++counter[ 2 ];
         else if ( (salary >= 500) && (salary < 600) )
              ++counter[ 3 ];
         else if ( (salary >= 600) && (salary < 700) )
              ++counter[ 4 ];
         else if ( (salary >= 700) && (salary < 800) )
              ++counter[ 5 ];
         else if ( (salary >= 800) && (salary < 900) )
              ++counter[ 6 ];
         else if ( (salary >= 900) && (salary < 1000) )
              ++counter[ 7 ];
         else
              ++counter[ 8 ];
    cout << "Enter a sales amount: (negative to end)";
    cin >> sales;
    }        
    
    displayResults(counter);

 
 system("pause");
 return 0;
}

void displayResults(int * counter)
{
   
     cout << " Range        Number" << endl;
     cout << "$200 - $299\t  " << counter[0] << endl;
     cout << "$300 - $399\t  " << counter[1] << endl;
     cout << "$400 - $499\t  " << counter[2] << endl;
     cout << "$500 - $599\t  " << counter[3] << endl;
     cout << "$600 - $699\t  " << counter[4] << endl;
     cout << "$700 - $799\t  " << counter[5] << endl;
     cout << "$800 - $899\t  " << counter[6] << endl;
     cout << "$900 - $999\t  " << counter[7] << endl;
     cout << "Over $1000 \t  " << counter[8] << endl;
}

Thanks you SOO much Lerner and wildgoose! You're advice and help helped me realize a lot about what I needed to do, and why. Thanks again!!!

Okay. I had enough!
It had been 11 posts in this thread and I cannot belive no one told OP to use code tags.
To the OP, it doesn't mean you shouldn't be knowing about code-tags. You should be posting the code using code-tags. Information about code-tag is posted all over the website :
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) any place CODE tags were used
6) Even on the background of the box you actually typed your message in

commented: Well said!!!! +35

Wow, ouch, I'm sorry...I'll make sure I do that from now on...

And so far nobody has told you that it's better to avoid using system("pause");

Use cin.get(); as a replacement of system("pause"); , less typing and more portable :) !!

Check out this if you want to know why :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.