passing arrays to constructors plz help

Hello, I have this programming challenge that has stopped me dead in my tracks; hopefully someone can help :) The code is supposed to take two arrays (one string and one int) in addition to an integer value and pass all three in an object that is passed to a class called BinManager, the BinManager has a three parameter constructor to is supposed to take these three parameters and put them in an array of InvBin objects (which is another class in the programm) I've written the code, but instead of getting the output "thing9" im getting "empty" if anyone could find where i'm going wrong, it would be a huge help. fyi there's probably something wrong with the three parameter constructor in the BinManager class, but i've tried everything. thanks for any input.

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
  using namespace std;
#include <iostream>
#include <iomanip>
class InvBin
{
private:
    string description;
    int qty;
    
public:
    InvBin (string d = "empty", int q = 0)
    { description = d; qty = q; }
    
    void setDescription(string d)
    {description =d;}
    
    string getDescription()
    {return description;}
    
    void setQty(int q)
    {qty =q;}
    
    int getQty( )
    {return qty;}
};


class BinManager
{
private:
    InvBin bin[20];
    int numBins;
    
public:
    BinManager()
    { numBins = 0; }
    BinManager(int size, string d[], int q[])
    {
        for( int count=0; count <size; count ++)
        {
            bin[count].setDescription(d[count]);
            bin[count].setQty(q[count]);
        }
    }
    
    string getDescription(int index)
    {
        return bin[index].getDescription();
    }
    
    int getQuantity(int index)
    {
        return bin[index].getQty();
    }
    
    string displayAllBins();
    
};


int main ()
{
    string test;
    string array_descript[20]{"thing1","thing2","thing3","thing4","thing5","thing6","thing7","thing8","thing9"};
    int array_quantity[20]{2,53,5,36,6,1,1,7,2};
    
    BinManager object1(9,array_descript,array_quantity);

    test= object1.getDescription(9);
    cout << test;
    
}
Last edited on
Don't know about the rest of your code but the first index of an array is 0. You are adding 9 elements therefore the last element "thing9" is element 8. test= object1.getDescription(8);
There are many things you should change here, but I think your question can be answered as follows:

C++ uses zero-based indexing for arrays. That means an array of 9 items can be accessed using index from 0 to 8, not 1 to 9.

So in line 69, use 8 not 9. Instead of test= object1.getDescription(9);
try test= object1.getDescription(8);
Wow, I feel silly for putting that up here now lol, thanks alot!
Topic archived. No new replies allowed.