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

Dani AI

Generated

Replying to : the immediate bug and the design problem are separate. The bug that makes every line print the same item comes from resetting the counters and reassigning the arrays inside each getter — each call sets x, y, z back to zero, writes the arrays, increments once and always returns the first element. Beyond that, a RetailItem object should represent a single product. Storing three items inside one RetailItem instance (arrays + counters) is the wrong abstraction.

Practical steps to fix the program:

  • Make RetailItem hold only three members: description (string), unitsOnHand (integer type), and price (double). Provide a constructor that initializes those members, and getDescription(), getUnitsOnHand(), getPrice() as const accessors. Remove p, d, oh, and x,y,z from the class entirely.
  • Populate the inventory outside the class. Use a fixed container (std::array) or std::vector<RetailItem> in main() and create three RetailItem objects with their data. Loop over that container and print "Item #n" plus the fields for each element.
  • For output use iomanip (setw, fixed, setprecision(2)) and consider implementing operator<< or a small print() member to centralize formatting.

Other notes and best practices: don’t put using namespace std; in headers, add include guards or #pragma once, pass strings by const&, mark getters const, initialize members with an initializer list, index arrays from 0 if you use them, and avoid system("PAUSE"). ’s recommendation to use an array/vector of RetailItem objects and iterate to print each item is the right direction. Use that design and the checklist above to get the desired output.

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';
}
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.