943,809 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 776
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 19th, 2009
0

I need some help...

Expand Post »
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:
cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using std::cout;
  3. using std::cin;
  4. using std::endl;
  5.  
  6. #include <iomanip>
  7.  
  8. void displayResults( );
  9.  
  10. int main()
  11. {
  12. int sales;
  13. double salary;
  14. int counter[ 9 ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  15.  
  16.  
  17.  
  18.  
  19. cout << "Enter a sales amount: (negative to end)";
  20. cin >> sales;
  21.  
  22.  
  23. while (sales > 0){
  24. salary = (sales * .09) + 200;
  25.  
  26. if ( (salary >= 200) && (salary < 300) )
  27. ++counter[ 0 ];
  28. else if ( (salary >= 300) && (salary < 400) )
  29. ++counter[ 1 ];
  30. else if ( (salary >= 400) && (salary < 500) )
  31. ++counter[ 2 ];
  32. else if ( (salary >= 500) && (salary < 600) )
  33. ++counter[ 3 ];
  34. else if ( (salary >= 600) && (salary < 700) )
  35. ++counter[ 4 ];
  36. else if ( (salary >= 700) && (salary < 800) )
  37. ++counter[ 5 ];
  38. else if ( (salary >= 800) && (salary < 900) )
  39. ++counter[ 6 ];
  40. else if ( (salary >= 900) && (salary < 1000) )
  41. ++counter[ 7 ];
  42. else
  43. ++counter[ 8 ];
  44. cout << "Enter a sales amount: (negative to end)";
  45. cin >> sales;
  46. }
  47.  
  48. displayResults();
  49.  
  50.  
  51. system("pause");
  52. return 0;
  53. }
  54.  
  55. void displayResults( )
  56. {
  57. int counter[ 9 ];
  58.  
  59. cout << " Range Number" << endl;
  60. cout << "$200 - $299\t " << counter[ 0 ] << endl;
  61. cout << "$300 - $399\t " << counter[ 1 ] << endl;
  62. cout << "$400 - $499\t " << counter[ 2 ] << endl;
  63. cout << "$500 - $599\t " << counter[ 3 ] << endl;
  64. cout << "$600 - $699\t " << counter[ 4 ] << endl;
  65. cout << "$700 - $799\t " << counter[ 5 ] << endl;
  66. cout << "$800 - $899\t " << counter[ 6 ] << endl;
  67. cout << "$900 - $999\t " << counter[ 7 ] << endl;
  68. cout << "Over $1000 \t " << counter[ 8 ] << endl;
  69. }



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
Last edited by Tekmaven; Jun 19th, 2009 at 7:25 pm. Reason: Code Tags
Reputation Points: 46
Solved Threads: 0
Newbie Poster
TheSilverFox is offline Offline
13 posts
since Jun 2009
Jun 19th, 2009
0

Re: I need some help...

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

C++ Syntax (Toggle Plain Text)
  1.  
  2. int counter[ 9 ];
  3.  
  4. i = counter[ n ] ; is the value so...
  5. int *pCnt = &counter[ n ];
  6. Gives you a pointer to the integer located at cell [n].
  7. Or try this.
  8.  
  9. int *pCnt = counter;
  10. while( n-- )
  11. {
  12. int i = *pCnt++;
  13.  
  14. }
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jun 19th, 2009
0

Re: I need some help...

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...
Reputation Points: 46
Solved Threads: 0
Newbie Poster
TheSilverFox is offline Offline
13 posts
since Jun 2009
Jun 19th, 2009
0

Re: I need some help...

First problem that array is empty (not initialized).

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

C++ Syntax (Toggle Plain Text)
  1.  
  2. // YOU need t fill counter
  3. // and initialize n like in a loop reference or something.
  4.  
  5. //i = counter[ n ] ;
  6. int *pCnt = &counter[ n ];
  7. for (n=0; n < 9; n++)
  8. {
  9. counter[n] = n;
  10. }
  11.  
  12.  
  13.  
  14. cout << " Range Number" << endl;
  15. cout << "$200 - $299\t " << counter[ 0 ] << endl;
  16. cout << "$300 - $399\t " << counter[ 1 ] << endl;
  17. cout << "$400 - $499\t " << counter[ 2 ] << endl;
  18. cout << "$500 - $599\t " << counter[ 3 ] << endl;
  19. cout << "$600 - $699\t " << counter[ 4 ] << endl;
  20. cout << "$700 - $799\t " << counter[ 5 ] << endl;
  21. cout << "$800 - $899\t " << counter[ 6 ] << endl;
  22. cout << "$900 - $999\t " << &counter[ 7 ] << endl;
  23. cout << "Over $1000 \t " << counter[ 8 ] << endl;
  24. }
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jun 19th, 2009
0

Re: I need some help...

void displayResults( );

change this to:

void displayResults(int *, int);

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

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

Try to define it on your own.
Last edited by Lerner; Jun 19th, 2009 at 5:57 pm.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Jun 19th, 2009
0

Re: I need some help...

Ok. This is what the function reads now:
cpp Syntax (Toggle Plain Text)
  1. void displayResults(int*, int)
  2. {
  3. int n;
  4. const int SIZE = 9;
  5. int counter[SIZE];
  6. int *pCnt = &counter[ n ];
  7. for (n=0; n < 9; n++)
  8. {
  9. counter[n] = n;
  10. }
  11.  
  12.  
  13. cout << " Range Number" << endl;
  14. cout << "$200 - $299\t " << counter[ 0 ] << endl;
  15. cout << "$300 - $399\t " << counter[ 1 ] << endl;
  16. cout << "$400 - $499\t " << counter[ 2 ] << endl;
  17. cout << "$500 - $599\t " << counter[ 3 ] << endl;
  18. cout << "$600 - $699\t " << counter[ 4 ] << endl;
  19. cout << "$700 - $799\t " << counter[ 5 ] << endl;
  20. cout << "$800 - $899\t " << counter[ 6 ] << endl;
  21. cout << "$900 - $999\t " << counter[ 7 ] << endl;
  22. cout << "Over $1000 \t " << counter[ 8 ] << endl;
  23. }
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!!
Last edited by Tekmaven; Jun 19th, 2009 at 7:26 pm. Reason: Code Tags
Reputation Points: 46
Solved Threads: 0
Newbie Poster
TheSilverFox is offline Offline
13 posts
since Jun 2009
Jun 19th, 2009
0

Re: I need some help...

Sorry I think I confused you

C++ Syntax (Toggle Plain Text)
  1. void displayResults(int*pCnt, int , nCnt)
  2. {
  3. cout << " Range Number" << endl;
  4. cout << "$200 - $299\t " << *(pCnt+0) << endl;
  5. cout << "$300 - $399\t " << *(pCnt+1) << endl;
  6.  
  7. etc.
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jun 19th, 2009
0

Re: I need some help...

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.
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jun 19th, 2009
0

Re: I need some help...

In this program it's probably easier to write out the display as opposed to using a loop so you could follow this outline:
C++ Syntax (Toggle Plain Text)
  1. void display(int *);
  2.  
  3. int main()
  4. {
  5. const int SIZE = 9;
  6. int counter[SIZE] = {0};
  7.  
  8. //do dah
  9.  
  10. display(counter);
  11.  
  12. //finish up
  13. }
  14.  
  15. void display(int * counter)
  16. {
  17. cout << " Range Number" << endl;
  18. cout << "$200 - $299\t " << counter[0] << endl;
  19. cout << "$300 - $399\t " << counter[1] << endl;
  20. //etc
  21. }

You want to pass counter as an argument to display(), not declare it in display().
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Jun 19th, 2009
0

Re: I need some help...

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?
Last edited by TheSilverFox; Jun 19th, 2009 at 6:32 pm.
Reputation Points: 46
Solved Threads: 0
Newbie Poster
TheSilverFox is offline Offline
13 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: c++ "error C4716: 'F_Ite' : must return a value"
Next Thread in C++ Forum Timeline: Debug Error Run-Time Check Failure #2





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC