class function to display not working

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <iomanip>
#include <fstream>
#include <math.h>
#include <windows.h>
#include <string>
#include <sstream>
#include <cstdlib>
#include "prog4classesandfuncts.h"
//#include "ConsoleIOManager.h"

using namespace 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 ;
};

and the output:
parrallels
Regular pliers           25
N.nose pliers            5
screwdriver              25
P.head screwdriver       6
Wrench-large             7
Wrench-small             18
Drill                    51
Cordless drill           16
Hand saw                 12
constructor
1   Regular pliers          25
2   N.nose pliers           5
3   screwdriver             25
4   P.head screwdriver      6
5   Wrench-large            7
6   Wrench-small            18
7   Drill                   51
8   Cordless drill          16
9   Hand saw                12
Current Inventory:
still not working
0     empty                   0
1     empty                   0
2     empty                   0
3     empty                   0
4     empty                   0
5     empty                   0
6     empty                   0
7     empty                   0
8     empty                   0


classes that are commented out are in the header (not included in this text)
It's a bit hard to follow without the header.

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).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <string>
#include <vector>

using namespace std;

// Class declarations ////////////////////////////////////////////////////
class InvBin
{
	string description;
	string numBins;
	int qty;
public:
	InvBin(string, int);
	string getDescription();
	int getQty();
};

class BinManager
{
	int numBins; // Not really required anymore, use bin.size() instead.
	vector<InvBin> bin;
public:
	BinManager(int, string d[], int q[]);
	BinManager();
	string DisplayAllBins();

};

// Constructors ///////////////////////////////////////////////
InvBin::InvBin (string d = "empty", int q = 0)
{description = d; qty = q;} 

BinManager::BinManager() 
{ numBins = 0;} 

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]));
		cout<<fixed<<left<<index +1<<"   ";
		cout<<setw(20)<<bin.end()->getDescription();
		cout<<"    "<<bin.end()->getQty()<<endl;
	}
	Sleep(2000);
}

// Member functions /////////////////////////////////////////
string InvBin::getDescription()
{  
   return description;
}

int InvBin::getQty( )
{
    return qty;
}

string BinManager::DisplayAllBins()
{
	int i;
	cout<< "Current Inventory: "<<endl;
	cout<<"still not working"<<endl;
	for (i=0; i<bin.size();i++)
	{
		cout<<fixed<<left<<i<<"     "<<setw(20);
		cout<<bin[i].getDescription();
		cout<<"    "<<bin[i].getQty()<<endl;
	}
	Sleep(4000);
	return 0 ;
};
Last edited on
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..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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 
also the "problem" asks us to load the data for the InvBin objects form parallel arrays, one for the string and one for the int.
Topic archived. No new replies allowed.