bidders.cpp


#include <string>
#include <fstream>
#include <iostream>
//
//  Bidders Registered for auction
//

#include "bidders.h"

using namespace std;



/**
 * Create a collection capable of holding the indicated number of bidders
 */
void initialize (BidderCollection& collection, int MaxSize)
{
  collection.MaxSize = MaxSize;
  collection.size = 0;
  collection.elements = new Bidder[MaxSize];
}


/**
 * Read all bidders from the indicated file
 */
void readBidders (BidderCollection& bidders, std::string fileName)
{
    ifstream in (fileName.c_str());
    in >> bidders.size;
    for (int i = 0; i < bidders.size && i < bidders.MaxSize; ++i)
    {
      Bidder bidder;
      in >> bidder.name >> bidder.balance;
      bidders.elements[i] = bidder;
    }
}


/**
 * Find the index of the bidder with the given name. If no such bidder exists,
 * return nBidders.
 */
int findBidder (BidderCollection& collection, std::string name)
{
  int found = collection.size;
  for (int i = 0; i < collection.size && found == collection.size; ++i)
    {
      if (name == collection.elements[i].name)
	found = i;
    }
  return found;
}

Discuss This Page:

(no threads at this time)