| | |
Coding Question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2006
Posts: 5
Reputation:
Solved Threads: 0
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.
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
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main() { // 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 ) { bonusAmount[i] = totalSales[i] * 0.05; } else { bonusAmount[i] = 0; // this is unnecessary actually } } // Display as you want. I stop here. return 0; }
バルサミコ酢やっぱいらへんで
•
•
Join Date: Jun 2006
Posts: 5
Reputation:
Solved Threads: 0
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.
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by WolfPack; Jun 19th, 2006 at 6:11 pm.
#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;
} C++ Syntax (Toggle Plain Text)
int integer_array[ 5] = {10, 22, 31, 48, 12};
integer_array to the screen? When you figure out the way to answer that question you will realize what you are doing wrong. バルサミコ酢やっぱいらへんで
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main(int argc, char* argv[]) { // declare variables string firstName[5]; string lastName[5]; double sales[5]; double totalSales = 0.0; double bonus[ 5 ] = { 0 }; double avgsales = 0.0; // 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 >> sales[i]; totalSales += sales[i]; } avgsales = totalSales / 5.0; for (int i=0; i < 5; i++) { if ( sales[i] > avgsales ) { bonus[i] = sales[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"; for (int j = 0; j < 5; ++j) { cout.width(15); cout << firstName[j]; cout.width(10); cout << lastName[j]; cout.width(10); cout << sales[j]; cout.width(20); cout << bonus[j]; cout << endl; } return 0; }
I don't accept change; I don't deserve to live.
•
•
Join Date: Jun 2006
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
// 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; }
C++ Syntax (Toggle Plain Text)
//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 (int i=0; i < 5; i++) { cout.width(15); cout << firstName[i]; cout.width(10); cout << lastName[i]; cout.width(10); cout << totalSales[i]; cout.width(20); cout << bonus[i]; cout << endl; }
![]() |
Similar Threads
- CODING QUESTION: What is the MOST importanting thing to do when...... (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: Read/write to same file > once, Help
- Next Thread: Getting started programming Windows
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






