student in need of help.. program has 2 classes, one to build object and another to build array of objects from first class.. i put cout<< in the constructor and it shows fine, but the displayAllBins functions displays default values...
here's the class codes:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <math.h>
#include <windows.h>
#include <string>
#include <sstream>
#include <cstdlib>
#include "prog4classesandfuncts.h"
//#include "ConsoleIOManager.h"
usingnamespace std;
//********************************************************************
//CLASSES
//********************************************************************
//********************************************************************
//Class InvBin
//********************************************************************
InvBin::InvBin (string d = "empty", int q = 0)//2-parameter constructor
{description = d; qty = q;} //with default values
// It will also have the following public member functions. They
//will be used by the BinManager class, not the client program.
//void setDescription( string d );
//string getDescription();
//void setQty ( int q );
//int getQty();
//********************************************************************
//Class BinManager
//********************************************************************
BinManager::BinManager() // default constructor
{ numBins = 0;}
BinManager::BinManager(int size, string d[ ], int q[ ])
{
// 3-Parameter constructor:: Recieves number of
//bins in use and parallelarrays of item names and
//quantities. Uses this info. to store values in the
//elements of the bin array. Remember, these elements
//are InvBin objects.
int index;
cout<<"constructor"<<endl;
for( index=0 ; index < size ; index++ )
{
InvBin bin(d[index], q[index]);
cout<<fixed<<left<<index +1<<" ";
cout<<setw(20)<<bin.getDescription();
cout<<" "<<bin.getQty()<<endl;
}
Sleep(2000);
}
// the class will also have the following public member functions:
// string getDescription(int index); //returns name of one item
// int getQuantity(int index) ; // returns qty of one item
// string DisplayAllBins() ; //returns string having one line
//for each item
// bool addParts(int binIndex, int q); //these returns true if the
// bool removeParts(int binIndex, int q); //action was done and false
// if it could not be done-
//see validation information
//******************************************************************
//FUNCTIONS
//******************************************************************
//***************class InvBin***************************
//void InvBin::setDescription ( string d )
//{ description = d ;
//}
string InvBin::getDescription()
{
return description;
}
//void InvBin::setQty(int q)
//{
// qty = q ;
//}
int InvBin::getQty( )
{
return qty;
}
//*************class BinManager******************
/*
string BinManager::getDescription ( int index )
{bin[index].description = d[index]
return bin[index].d;
}
;
int BinManager::getQuantity (int index)
{
for( index=0 ; index < 10 ; index++ )
{
{InvBin::bin[index].q = q[index]};
}
};
*/
string BinManager::DisplayAllBins()
{
int i;
cout<< "Current Inventory: "<<endl;
cout<<"still not working"<<endl;
for (i=0; i<9;i++)
{
cout<<fixed<<left<<i<<" "<<setw(20);
cout<<bin[i].getDescription();
cout<<" "<<bin[i].getQty()<<endl;
}
Sleep(4000);
return 0 ;
};
On line 50 you define InvBin bin(d[index], q[index]);. I see two problems with this:
1. bin is declared in the local scope of the for loop which means that it is destroyed when you reach the end of the for loop.
2. bin is not defined as an array. It is just one object. You refer to an array in DisplayAllBins.
Here is what I'd suggest. In the BinManager declaration add:
1 2 3 4
class BinManager
{
vector<InvBin> bin;
};
Then in BinManager's constructor do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
BinManager::BinManager(int size, string d[ ], int q[ ])
{
int index;
cout<<"constructor"<<endl;
for( index=0 ; index < size ; index++ )
{
bin.push_back(InvBin(d[index],q[index])); // Add a new InvBin (with the constructor) to bin.
cout<<fixed<<left<<index +1<<" ";
cout<<setw(20)<<bin.end()->getDescription();
cout<<" "<<bin.end()->getQty()<<endl;
}
Sleep(2000);
}
You can make DisplayAllBins a little more flexible now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
string BinManager::DisplayAllBins()
{
int i;
cout<< "Current Inventory: "<<endl;
cout<<"still not working"<<endl;
for (i=0; i<bin.size();i++) // Not hard coded to 9 anymore.
{
cout<<fixed<<left<<i<<" "<<setw(20);
cout<<bin[i].getDescription();
cout<<" "<<bin[i].getQty()<<endl;
}
Sleep(4000);
return 0 ;
};
Here's everything togeather (I had to guess a bit at the class headers).
ok i think i see what you did there... sadly pointers are in the next chapter, so we're not supposed to use them, and BinManager specifies an array of InvBin objects instead of vectors.. here's the header file to show..
class InvBin
{
private:
string description; // item name
int qty; // Quantity of items
// in this bin
public:
InvBin (string d , int q );//2-parameter constructor
//with default values
// It will also have the following public member functions. They
//will be used by the BinManager class, not the client program.
void setDescription( string d );
string getDescription();
void setQty ( int q );
int getQty();
};
//********************************************************************
//Class BinManager
//********************************************************************
class BinManager
{
private:
InvBin bin[30]; //array of InvBin objects
int numBins; // number of bins
//currently in use
public:
BinManager(); // default constructor
BinManager(int size, string d[], int q[]);
// the class will also have the following public member functions:
string getDescription(int index); //returns name of one item
int getQuantity(int index) ; // returns qty of one item
string DisplayAllBins() ; //returns string having one line
//for each item
bool addParts(int binIndex, int q); //these returns true if the
bool removeParts(int binIndex, int q); //action was done and false
// if it could not be done-
//see validation information
};
#endif