user2108147
user2108147

Reputation: 21

Declaring my member function parameters/arguments

class Seller
{
private:
   float salestotal;     // run total of sales in dollars
   int lapTopSold;       // running total of lap top computers sold
   int deskTopSold;      // running total of desk top computers sold
   int tabletSold;       // running total of tablet computers sold
   string name;          // name of the seller
Seller::Seller(string newname)
{
   name = newname;
   salestotal = 0.0;
   lapTopSold = 0;
   deskTopSold = 0;
   tabletSold = 0;
}

bool Seller::SellerHasName ( string nameToSearch )
{
   if(name == nameToSearch)
      return true;
   else
      return false;
}
class SellerList
{
private:
   int num;  // current number of salespeople in the list
   Seller salespeople[MAX_SELLERS];
public:
   // default constructor to make an empty list 
   SellerList()
   {
      num = 0;
   }
   // member functions 

// If a salesperson with thisname is in the SellerList, this 
// function returns the associated index; otherwise, return NOT_FOUND. 
// Params: in
int Find ( string thisName );

void Add(string sellerName);

void Output(string sellerName);
};

int SellerList::Find(string thisName)
{
   for(int i = 0; i < MAX_SELLERS; i++)
      if(salespeople[i].SellerHasName(thisName))
         return i;
   return NOT_FOUND;
}

// Add a salesperson to the salespeople list IF the list is not full
// and if the list doesn't already contain the same name. 
void SellerList::Add(string sellerName)
{           
   Seller(sellerName);
   num++;
}

I have some issues with the parameters in my functions in my SellerList class. I want to add someone to the salespeople array so I have a record of all my sellers... Bob, Pam, Tim, etc... My constructor Seller(sellerName) creates a Seller with name sellerName.

How do I add this Seller to the Salespeople array and have capability of a way to pull the data back out and use it in more functions such as a Update function, or an output function?

MAX_SELLERS = 10.... I guess my issue is not knowing whether to use parameters of only Add(string) or Add(Seller, string). Any help would be appreciated.

Upvotes: 0

Views: 60

Answers (3)

Manu343726
Manu343726

Reputation: 14174

Not reinvent the wheel. Choose the container appropiate to your problem. In this case, because you are referencing/searching Sellers by a std::string, I suggest you to use a hash table like std::unordered_map (Or std::map search tree if you don't have access to C++11):

int main()
{
    std::unordered_map<Seller> sellers;

    //Add example:
    sellers["seller name string here"] = /* put a seller here */;

    //Search example:
    std::unordered_map<Seller>::iterator it_result = sellers.find( "seller name string here" );

    if( it_result != std::end( sellers ) )
        std::cout << "Seller found!" << std::endl;
    else
        std::cout << "Seller not found :(" << std::endl;
}

Upvotes: 2

Sorin
Sorin

Reputation: 11968

How about using STD vector inside of SellerList instead of the array.

vector<Seller> x;

you can do x.push_back(Seller(...)) or x[0].SellerHasName() and x.size() will give you the number of sellers.

Upvotes: 0

CaldasGSM
CaldasGSM

Reputation: 3062

maybe something like this?

// Add a salesperson to the salespeople list IF the list is not full
// and if the list doesn't already contain the same name. 
void SellerList::Add(string sellerName)
{           
   if(num < MAX_SELLERS)
       salespeople[num++] = new Seller(sellerName);
}

Upvotes: -1

Related Questions