I really love working with structs, and I get how they work and everything. And I know my issue right now is the math (I'm horrible at math!) But my problem: I need to create a program that reads from a .txt file of employees (let's say 5) an they're quarterly sales. (check, got that working). I need the cout to display the last name and sales of each (check) and then add up the quarterly sales totals (check). Then, this is where I get stuck, is I gotta find the highest and lowest total quarterly sales & assign a bonus to the highest, nothing to the lowest, and a bonus to everyone else.

So far, I have

quarterSales.quarterOne + quarterSales.quarterTwo + quarterSales.quarterThree + quarterSales.quarterFour

for the total of the sales. Then I was starting a for loop to determine the highest and lowest.

for (int x = 0; x < 5; x++)
{
if (quarterSales[x] > highest)
highest = quarterSales[x]
}

Then the same goes for the lowest (changing the operator in the if statement accordingly). I guess I don't know how to go about incorporating the struct. If the total quarterly sales is something that is calculated from the information in the .txt file and not something directly from the file, how would I even declare a quarterlySales.totalSales variable?

Because I keep thinking that I need to do

quarterlySales.totalSales = quarterlySales.totalSales + highBonus
quarterlySales.totalSales = quarterlySales.totalSales + regularBonus

or something like that, since I know that's not right.

Am I close in my thought process? I feel like I'm incredibly wrong.

Recommended Answers

All 4 Replies

If I could declare a quarterSales.totalSales, then I'm thinking it'd be like

for (int x = 0; x < 5; x++)
{
if (quarterSales.totalSales[x] > highestSales)
highestSales = quarterSales.totalSales[x]

quarterSales.totalSales[x] + highBonus/regularBonus
}

or, something along those lines. My thought process might be out of whack (I'm horrible at pseudocode).

What you could do is place all of the quatersales structs into a vector object like so: vector<quaterSalesStruct> qSales;

Then you create the variables :

int highest = 0;
int lowest = 0;

Then to find the highest and lowest you can do the following:

 for (int x = 0; x < qSales.size(); x++)
{
   if (quarterSales[x].sale > highest) //compare employees with curr highest
      highest = x; //save the position in the vector where highest is   
}

Then when you are assigning bonuses you can set up a loop and when the lowest index is encountered you could skip over it for example:

for(int index = 0; index < qSales.size(); index++)
{
    if(index == lowest)
        //no bonus

    else if(index == highest)
     qSales[highest].sale += the bonus for highest

     else
      qSales[index] += bonus for everyone else
}

hope this helps.

 for (int x = 0; x < qSales.size(); x++)
{
   if (qSales[x].sale > highest) //compare employees with curr highest
      highest = x; //save the position in the vector where highest is   
}

correct loop

In case you have not yet encountered using the C++ STL vector container, (which is really just an easily re-usable and easily expandable dynamic array), you might like to see this demo ... that just uses an array with the max size pre-fixed at compile time ...

Note the little 'trick' of using/setting the first element value in the array to the min and max values, to get started ...

and then, to begin traversing the values, testing if lesser ... or greater ... for the rest of the array elements ... if any exist.

// aryStructSalesLowHigh.cpp //

// demo ...
// take in an array of struct ...
// and then find low and high values ...
// and output processed results

// note:
// we start by setting first element (if exists) in array to
// lowest and highest ...
// then change from there ... (while more elements exist)


// http://developers-heaven.net/forum/index.php/topic,46.0.html

#include <iostream>
#include <sstream> // re. stringstream objects ...
#include <string>
#include <cctype> // re. tolower ...

using namespace std;

const int TEAM_SIZE = 5;
const int BONUS = 1000;

struct Sales
{
    string name;
    int dollars;

    // ctor with default values ...
    Sales( const string& n="", int d=0 ) : name(n), dollars(d) {}
} ;

// def'n of overloaded operator << for Sales objects ...
ostream& operator << ( ostream& os, const Sales& s )
{
    return os << s.name <<", $" << s.dollars;
}

// some utility functions to ease program code ...
char takeInChr( const std::string& msg = "" )
{
    std::cout << msg << std::flush;
    std::string reply;
    getline( std::cin, reply );
    if( reply.size() )
        return reply[0];
    // else ...
    return 0;
}
bool more()
{
    if( tolower( takeInChr( "More (y/n) ? " )) == 'n' )
        return false;
    // else ...
    return true;
}

// take in a Sales object AND return by (pass by) reference
bool takeIn( Sales& s, int n )
{
    cout << "Enter name of team member " << n << ": " << flush;
    getline( cin, s.name );

    cout << "Enter total sales for this quarter: " << flush;
    string salesStr;
    getline( cin, salesStr );

    // NOW ... convert string to int ...
    istringstream iss( salesStr );
    iss >> s.dollars;

    cout << "You entered " << s << endl;
    if( takeInChr( "Is that ok to accept (y/n) ? ") == 'y')
    {
        cout << "\nOk ... accepted ...\n";
        return true;
    }

    // else ... if reach hear ...
    cout << "\nOk ... NOT accepted ...\n";
    return false;
}



int main()
{
    Sales myTeam[TEAM_SIZE]; // reserve memory for this many struct's

    int size = 0;

    for( ; ; ) // loop forever until 'break'
    {
        Sales member;
        bool okToAccept = takeIn( member, size+1 );
        if( okToAccept )
        {
            myTeam[size] = member;
            ++size;
        }

        if( size < TEAM_SIZE )
        {
            if( !more() )
                break; // break out of forever loop right now
        }
        else
        {
            cout << "\nYou have reached the max team size here.\n";
            break;
        }
    }

    // ok ... now find min and max INDEX and award each bonus
    if( size )
    {
        int iMin = 0, iMax = 0;
        int dollarsMin, dollarsMax;
        // start here ...
        dollarsMin = dollarsMax = myTeam[0].dollars;
        // now start with next in ... (if exists) ...
        for( int i = 1; i < size; ++ i )
        {
            if( myTeam[i].dollars < dollarsMin )
            {
                // update ...
                dollarsMin =  myTeam[i].dollars;
                iMin = i;
            }
            else if( myTeam[i].dollars > dollarsMax )
            {
                // update ...
                dollarsMax =  myTeam[i].dollars;
                iMax = i;
            }
        }

        // Ok ... now can award each bonus ... or not to Min
        // Max gets double bonus ...

        for( int i = 0; i < size; ++ i )
        {
            cout << myTeam[i];
            if( i != iMin )
            {
                if( i != iMax )
                    cout << " has bonus = " << BONUS;
                else
                    cout << " has bonus = " << 2*BONUS;
            }
            else
                cout << " has no bonus this quarter";
            cout << endl;
        }
    }
    else
        cout << "\nNo team members were entered yet ...\n";
}
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.