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> 
using namespace 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; 
}

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?

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.

#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; 
}

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.

#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; 
}
#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

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.

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.

#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 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.

// 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 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 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 '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).


//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;
}

Thanks Guys

It actually makes sense now.

Dumb question- I am just starting out with programming. Was C++ the best place to start?

I think Python is a good language to start with.

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.