In the retailitem.cpp I need to beable to increment x,y and z. So that is displays the next item. Heres what the code should output

Descriptio   Units On Hand	Unit Price ($)
Item #1	Jacket	        12	                     59.95
Item #2	Jeans	        40	                     34.95
Item #3	Shirt	        20	                     24.95

Heres the code I have...

main.cpp

#include <cstdlib>
#include <iostream>
#include "RetailItem.h"
#include <iomanip>
using namespace std;
const unsigned W = 20;

int main(int argc, char *argv[])
{
    RetailItem r;
 
cout<<setw(W)<<"Description"<<setw(W)<<"Units on Hand"<<setw(W)<<"Price ($)"<<endl;
cout<<endl;
cout<<"Item #1";
cout<<setw(10)<<r.getDescription();
cout<<setw(15)<<r.getUnitsOnHand();
cout<<setw(25)<<r.getPrice()<<endl;
cout<<"Item #2";
cout<<setw(10)<<r.getDescription();
cout<<setw(15)<<r.getUnitsOnHand();
cout<<setw(25)<<r.getPrice()<<endl;
cout<<"Item #3";
cout<<setw(10)<<r.getDescription();
cout<<setw(15)<<r.getUnitsOnHand();
cout<<setw(25)<<r.getPrice()<<endl;
    
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

retailitem.h

#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <stdlib.h>

using namespace std;

class RetailItem   

{
      
              
      public:
             static const unsigned SIZE = 100;
             static const unsigned SIZE1 = 100;
             static const unsigned SIZE2 = 100;
             RetailItem();
             void setDescription();
             void setUnitsOnHand();
             void setPrice();
             string getDescription();
             double getUnitsOnHand();
             double getPrice();
             
             
      private:

              string description;
              unsigned unitsOnHand;
              double price;
              double x,y,z;
              double p[SIZE];
              string d[SIZE1];
              double oh[SIZE2];
              
              
              
};

retailitem.cpp

#include <cstdlib>
#include <iostream>
#include <string>
#include "RetailItem.h"

using namespace std;


RetailItem::RetailItem()
{
}

double RetailItem::getPrice()
{
     x = 0;
     p[1] = 59.95;
     p[2] = 34.95;
     p[3] = 24.95;
     x++;
     return p[x];
     
}

string RetailItem::getDescription()
{
       y=0;
       d[1] = "Jacket";
       d[2] = "Jeans";
       d[3] = "Shirt";
       y++;
       return d[y];
}


double RetailItem::getUnitsOnHand()
{
       z=0;
       oh[1] = 12;
       oh[2] = 40;
       oh[3] = 20;
       z++;
       return oh[z];
}

I don't think you have a good design. RetailItem should only specify one item. To get more than one item, you use an array of RetailItems:

#include <iomanip>
#include <iostream>
#include <ostream>
#include <string>

class RetailItem {
  std::string _description;
  double _price;
  int _on_hand;
public:
  RetailItem ( std::string description, double price, int on_hand )
    : _description ( description )
    , _price ( price )
    , _on_hand ( on_hand )
  {}

  void ReStock ( int units ) { _on_hand += units; }
  void Sold ( int units ) { _on_hand -= units; }

  friend std::ostream& operator<< ( std::ostream& out, const RetailItem& item )
  {
    using std::setw;
    using std::left;
    using std::fixed;
    using std::setprecision;

    return out<< setw ( 16 ) << left << item._description
      << setw ( 16 ) << item._on_hand
      << fixed << setprecision ( 2 ) << item._price;
  }
};

int main()
{
  RetailItem inventory[] = {
    RetailItem ( "Jacket", 59.95, 12 ),
    RetailItem ( "Jeans", 34.95, 40 ),
    RetailItem ( "Shirt", 24.95, 20 )
  };

  std::cout<<"\tDescription\tUnits On Hand\tPrice\n";

  for ( int i = 0; i < 3; i++ )
    std::cout<<"Item #"<< i + 1 <<' '<< inventory[i] <<'\n';
}
This article has been dead for over six months. Start a new discussion instead.