Coding Question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2006
Posts: 5
Reputation: jbuzz120 is an unknown quantity at this point 
Solved Threads: 0
jbuzz120 jbuzz120 is offline Offline
Newbie Poster

Coding Question

 
0
  #1
Jun 18th, 2006
Hey Guys and Gals

I am new to this site and new to programming. C++ is the first one I am trying to learn. I ran into a problem on one of my assignements. I believe my problem is in the actual calculations. The Bonus part of the program is way off. Could you guys give it a look and see if you can point me in the right direction. I will attach the problem and my source code. I really appreciate this. I look forward to learning from some people who have been doing this a long time.

Create an algorithm and a C++ program for the following problem.

TechMed International has five salesmen on their staff. Bonuses are provided each week if a salesman’s total sales are greater than the average sales for the week. Your program should input the first and last name of each salesman along with his or her total sales for the week. It should then compute the average sales for the five salesmen. Finally, it should print each salesperson’s name, his or her sales amount, and the bonus amount. The bonus amount will be zero if the sales amount is less than the average. It will be 5 percent of the salesman’s sales amount if it is greater than the average. Use parallel arrays to solve this problem.

To test your program, it should display the following report, assuming that the sales amount figures were provided as input to the program.

Sales Report
Name
Total Sales
Bonus
Stan Jones
2500
0.00
Bill Smith
3325
166.25
Abe Locker
4155
207.75
Ace Hill
2120
0.00
Bud Wiser
1850
0.00


Store your project in a folder called Exam2. You do not need to write any external documentation but you should include internal documentation in your program.

Extra Special Information

We haven’t dealt with strings of characters yet in this class for C++. There is more than one type of string in C++ but the string class in the std namespace is the easiest to use, because you can normally treat it like other data types. To use the string class, you need to add the following line at the top.
#include <string>


Above the line that contains “using namespace std;”

To declare a single string variable called “firstName”, use a declaration similar to
string firstName;

To declare an array of strings that has five elements, use a declaration similar to:

string firstNames[5];


To input a value into a string variable called firstName, use: cin >> firstName;

To input a value into element “j” of the array “firstNames”, use cin >> firstNames[j];

To output a value from element “j” of the array “firstNames”, use cout << firstNames[j];

Your program will need to implement the following general steps to work successfully.
1. Input the first name, last name and total sales amount for each employee, storing the information in parallel arrays.
2. Calculate the average sales amount for all employees.
3. Calculate the bonus for each sales person, storing the bonus for each in an array.
4. Print the first name, last name, total sales and bonus for each employee in the arrays.

/ Program: Demo9.cpp : Sales Report
// Author: ME 
// Date Written: 6-16-06
// Purpose: To compute total sales and bonuses for the salesmen

// include libraries 
#include <iostream> 
#include "stdafx.h"
#include <string> 
usingnamespace std; 
int main(int argc, char* argv[]) 
{ 
// declare variables 
string firstName[100]; 
string lastName[100];

double totalSales[100], avgSales[100], bonusAmount[100]; 
int index, count; 
// Read in the Stock names, current price and former price. 
index = 0; 
cout << "Enter the Sales Persons First Name or end to quit "; 
cin >> firstName[index]; 
cout << "Enter the Sales Person Last Name ";
cin >> lastName[index];
while (firstName[index] != "end") 
{ 
cout << "Enter the total sales for the week "; 
cin >> totalSales[index]; 
index++; 
cout << "\n Enter the Sales Persons First Name 'end' to quit: "; 
cin >> firstName[index]; 
cout << "\n Enter the Sales Persons Last Name 'end' to quit: "; 
cin >> lastName[index];

} 
count = index; 
// Calculate the Sales average and bonus amount. 
for (index=0; index<count; index++) 
{ 
avgSales[index] = totalSales[index] /count; 
bonusAmount[index] = totalSales[index] * .5; 
if (avgSales[index]>totalSales[index]) 
bonusAmount[index]; 

} 
// Display 
cout.setf(ios::fixed); 
cout.setf(ios::showpoint); 
cout.precision(2); 
cout << "\n\n Sales Report\n\n"; 
cout << " First Last Total Sales Bonus \n"; 
for (index=0; index<count; index++) 
{ 
cout.width(15);
cout << firstName[index];
cout.width(10);
cout << lastName[index];
cout.width(10);
cout << totalSales[index];
cout.width(20);
cout << bonusAmount[index];

cout << endl;
} 
return 0; 
}
Last edited by Rashakil Fol; Jun 18th, 2006 at 7:06 pm. Reason: added [code][/code] tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 5
Reputation: jbuzz120 is an unknown quantity at this point 
Solved Threads: 0
jbuzz120 jbuzz120 is offline Offline
Newbie Poster

Re: Coding Question

 
0
  #2
Jun 18th, 2006
it's mainly the whole how to calculate the bonus thing that has me scratching my head. Everything else seems to work on it. Well i need to line up my print out but that is no biggie.

Guys have any advice?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 5
Reputation: jbuzz120 is an unknown quantity at this point 
Solved Threads: 0
jbuzz120 jbuzz120 is offline Offline
Newbie Poster

Re: Coding Question

 
0
  #3
Jun 19th, 2006
Hey guys

I need to get this done soon and I am at a loss for what I am doing wrong. The whole bonus thing is screwed up. It isn't doing the right calc at all.



Hoping someone can help me out.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Coding Question

 
0
  #4
Jun 19th, 2006
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. // declare variables
  7. string firstName[5];
  8. string lastName[5];
  9. double totalSales[5];
  10. double TotalSalesByAll = 0.0;
  11. double bonus[ 5 ] = { 0 };
  12. double avgsales,
  13.  
  14. // Read in the Names, and weekly sales
  15. for ( int i = 0 ; i < 5 ; i++)
  16. {
  17. cout << "Enter the First Name";
  18. cin >> firstName[i];
  19.  
  20. cout << "Enter the Last Name";
  21. cin >> lastName[i];
  22.  
  23. cout << "Enter the Weekly Sales ";
  24. cin >> totalSales[i];
  25.  
  26. TotalSalesByAll += totalSales[i];
  27. }
  28. // Calculate the Sales average
  29. avgsales = TotalSalesByAll / 5;
  30. // Calculate the bonus amount.
  31. for (int i=0; i < 5; i++)
  32. {
  33. if ( totalSales[i] > avgsales )
  34. {
  35. bonusAmount[i] = totalSales[i] * 0.05;
  36. }
  37. else
  38. {
  39. bonusAmount[i] = 0; // this is unnecessary actually
  40. }
  41. }
  42. // Display as you want. I stop here.
  43. return 0;
  44. }
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 5
Reputation: jbuzz120 is an unknown quantity at this point 
Solved Threads: 0
jbuzz120 jbuzz120 is offline Offline
Newbie Poster

Re: Coding Question

 
0
  #5
Jun 19th, 2006
I added some stuff and it blows up after I enter the 5th person. Sorry to be nagging but I am just lost. I have been going through the tutorials today and still a little lost. I appreciate the help I have got so far though. Not sure it's my display that is blowing it up or if it's the program.

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main(int argc, char* argv[])
  5. {
  6. // declare variables
  7. string firstName[5];
  8. string lastName[5];
  9. double totalSales[5];
  10. double TotalSalesByAll = 0.0;
  11. double bonus[ 5 ] = { 0 };
  12. double avgsales;
  13.  
  14.  
  15. // Read in the Names, and weekly sales
  16. for ( int i = 0 ; i < 5 ; i++)
  17. {
  18. cout << "Enter the First Name ";
  19. cin >> firstName[i];
  20.  
  21. cout << "Enter the Last Name ";
  22. cin >> lastName[i];
  23.  
  24. cout << "Enter the Weekly Sales ";
  25. cin >> totalSales[i];
  26.  
  27. TotalSalesByAll += totalSales[i];
  28. }
  29. // Calculate the Sales average
  30. avgsales = TotalSalesByAll / 5;
  31. // Calculate the bonus amount.
  32. for (int i=0; i < 5; i++)
  33. {
  34. if ( totalSales[i] > avgsales )
  35. {
  36. bonus[i] = totalSales[i] * 0.05;
  37. }
  38. else
  39. {
  40. bonus[i] = 0;
  41. }
  42. }
  43.  
  44. // Display
  45. cout.setf(ios::fixed);
  46. cout.setf(ios::showpoint);
  47. cout.precision(2);
  48. cout << "\n\n Sales Report\n\n";
  49. cout << " First Last Total Sales Bonus \n";
  50. cout.width(15);
  51. cout << firstName[i];
  52. cout.width(10);
  53. cout << lastName[5];
  54. cout.width(10);
  55. cout << totalSales[5];
  56. cout.width(20);
  57. cout << bonus[5];
  58. cout << endl;
  59. return 0;
  60. }
Last edited by WolfPack; Jun 19th, 2006 at 6:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Coding Question

 
0
  #6
Jun 19th, 2006
#include <iostream> 
#include <string> 
using namespace std; 
int main(int argc, char* argv[]) 
{ 
    // declare variables 
    string firstName[5]; 
    string lastName[5];
    double totalSales[5];
    double TotalSalesByAll = 0.0;
    double bonus[ 5 ] = { 0 };
    double avgsales;
   
    
    // Read in the Names, and weekly sales 
    for ( int i = 0 ; i < 5 ; i++)
    { 
        cout << "Enter the First Name   "; 
        cin >> firstName[i]; 
        
        cout << "Enter the Last Name    "; 
        cin >> lastName[i]; 
        
        cout << "Enter the Weekly Sales "; 
        cin >> totalSales[i]; 
        
        TotalSalesByAll += totalSales[i];
    } 
    // Calculate the Sales average
    avgsales = TotalSalesByAll / 5; 
    // Calculate the bonus amount. 
    for (int i=0; i < 5; i++) 
    { 
        if ( totalSales[i] > avgsales )
        {
            bonus[i] = totalSales[i] * 0.05;
        }
        else
        {
            bonus[i] = 0; 
        }
} 
   
    // Display 
cout.setf(ios::fixed); 
cout.setf(ios::showpoint); 
cout.precision(2); 
cout << "\n\n Sales Report\n\n"; 
cout << " First Last Total Sales Bonus \n"; 
cout.width(15);
cout << firstName[i];
cout.width(10);
cout << lastName[5];
cout.width(10);
cout << totalSales[5];
cout.width(20);
cout << bonus[5];
cout << endl;
return 0; 
}
The lines marked in Red are definitely wrong. The line marked in Blue is neither right nor wrong. Refer the tutorials in Arrays a bit more. If you define an array called
  1. int integer_array[ 5] = {10, 22, 31, 48, 12};
how do you print the 5 elements in integer_array to the screen? When you figure out the way to answer that question you will realize what you are doing wrong.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Coding Question

 
0
  #7
Jun 19th, 2006
Originally Posted by jbuzz120
I added some stuff and it blows up after I enter the 5th person. Sorry to be nagging but I am just lost. I have been going through the tutorials today and still a little lost. I appreciate the help I have got so far though. Not sure it's my display that is blowing it up or if it's the program.
Here is the workin prog. I hope it helps.

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main(int argc, char* argv[]) {
  5. // declare variables
  6. string firstName[5];
  7. string lastName[5];
  8. double sales[5];
  9. double totalSales = 0.0;
  10. double bonus[ 5 ] = { 0 };
  11. double avgsales = 0.0;
  12. // Read in the Names, and weekly sales
  13. for ( int i = 0 ; i < 5 ; i++) {
  14. cout << "Enter the First Name ";
  15. cin >> firstName[i];
  16. cout << "Enter the Last Name ";
  17. cin >> lastName[i];
  18. cout << "Enter the Weekly Sales ";
  19. cin >> sales[i];
  20. totalSales += sales[i];
  21. }
  22.  
  23. avgsales = totalSales / 5.0;
  24. for (int i=0; i < 5; i++) {
  25. if ( sales[i] > avgsales ) {
  26. bonus[i] = sales[i] * 0.05;
  27. }
  28. else {
  29. bonus[i] = 0;
  30. }
  31. }
  32. // Display
  33. cout.setf(ios::fixed);
  34. cout.setf(ios::showpoint);
  35. cout.precision(2);
  36. cout << "\n\n Sales Report\n\n";
  37. cout << " First Last Total Sales Bonus \n";
  38. for (int j = 0; j < 5; ++j) {
  39. cout.width(15);
  40. cout << firstName[j];
  41. cout.width(10);
  42. cout << lastName[j];
  43. cout.width(10);
  44. cout << sales[j];
  45. cout.width(20);
  46. cout << bonus[j];
  47. cout << endl;
  48. }
  49. return 0;
  50. }
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1
Reputation: jeph is an unknown quantity at this point 
Solved Threads: 0
jeph jeph is offline Offline
Newbie Poster

Re: Coding Question

 
0
  #8
Jun 19th, 2006
Originally Posted by jbuzz120
I added some stuff and it blows up after I enter the 5th person. Sorry to be nagging but I am just lost. I have been going through the tutorials today and still a little lost. I appreciate the help I have got so far though. Not sure it's my display that is blowing it up or if it's the program.
  1. // Display
  2. cout.setf(ios::fixed);
  3. cout.setf(ios::showpoint);
  4. cout.precision(2);
  5. cout << "\n\n Sales Report\n\n";
  6. cout << " First Last Total Sales Bonus \n";
  7. cout.width(15);
  8. cout << firstName[i];
  9. cout.width(10);
  10. cout << lastName[5];
  11. cout.width(10);
  12. cout << totalSales[5];
  13. cout.width(20);
  14. cout << bonus[5];
  15. cout << endl;
  16. return 0;
  17. }
The problem is you are referencing memory outside of your vector when you call "lastName[5]". Your vector spans from lastName[0] to lastName[4] (5 total). I don't know exactly what you were trying to do with firstName[i] and no for loop, but I assume you meant to put one in. I think your best bet would be to put everything from "cout.width(15)" to "cout << endl;" in a for loop and change the [5]'s to [i]'s. You may also want to think about setting individual widths for First, Last, Total Sales and Bonus so that they line up properly with the numbers (just use the same width as below).

  1. //Display
  2. cout.setf(ios::fixed);
  3. cout.setf(ios::showpoint);
  4. cout.precision(2);
  5. cout << "\n\n Sales Report\n\n";
  6. cout << " First Last Total Sales Bonus \n";
  7.  
  8. for (int i=0; i < 5; i++)
  9. {
  10. cout.width(15);
  11. cout << firstName[i];
  12. cout.width(10);
  13. cout << lastName[i];
  14. cout.width(10);
  15. cout << totalSales[i];
  16. cout.width(20);
  17. cout << bonus[i];
  18. cout << endl;
  19. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 5
Reputation: jbuzz120 is an unknown quantity at this point 
Solved Threads: 0
jbuzz120 jbuzz120 is offline Offline
Newbie Poster

Re: Coding Question

 
0
  #9
Jun 19th, 2006
Thanks Guys

It actually makes sense now.

Dumb question- I am just starting out with programming. Was C++ the best place to start?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Coding Question

 
0
  #10
Jun 19th, 2006
I think Python is a good language to start with.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC