Passing arrays through header files

So, I'm finishing up my first C++ class and am pruning our last project for fun. We were asked to reprogram our previous project where we created a vending machine using classes. Well, I wanted to take this a step further since I didn't really like the way to the code looked with how I had to set my item descriptions and so forth. I've been messing around with headers, but am having problem passing arrays through it. I can pass a constant integer just fine, just not the array. I've searched Google for a few hours and can't seem to come across the information I need.

stock.h
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
#include <iostream>
#include <string>
#ifndef STOCK_H
#define STOCK_H

using namespace std;

extern const int ITEM_AMT=10;
extern string stockNames[ITEM_AMT];
extern string stockDescs[ITEM_AMT];

class stock        // Structure for vending machine stock
{
  public:
    void setInventory(int slot1, int slot2, int slot3, int slot4, int slot5, int slot6, int slot7, int slot8, int slot9, int slot10);
    void setPrice(int slot1, int slot2, int slot3, int slot4, int slot5, int slot6, int slot7, int slot8, int slot9, int slot10);
    void setStockNames();
    void setStockDescriptions();
    void displayMenu(stock inventory);
    void dispenseItem(int item);
    int getItem(int item);
    string getName(int item);
    bool checkStock(int item);
  private:
    int stockItem[ITEM_AMT];
    string stockName[ITEM_AMT];
    string stockDesc[ITEM_AMT];
};

#endif 


stock.cpp
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    const int ITEM_AMT=10;
    string stockNames[ITEM_AMT];
    string stockDescs[ITEM_AMT];

    stockNames[0] = " Paper clips!!!";
    stockNames[1] = " Coffee";
    stockNames[2] = " Elephant";
    stockNames[3] = " Flowers";
    stockNames[4] = " Guiness (pint)";
    stockNames[5] = " Pepsi (20oz)";
    stockNames[6] = " Puppy (Boston Terrier)";
    stockNames[7] = " Sandwhich (ham)";
    stockNames[8] = " Tickle Me Elmo";
    stockNames[9] = "Umbrella (red & white)";

    stockDescs[0] = "a Box of Paper Clips. What were you thinking?";
    stockDescs[1] = "a cup of Coffee.";
    stockDescs[2] = "an Elephant. (Shipping not included)";
    stockDescs[3] = "some flowers.";
    stockDescs[4] = "a pint of Guiness. (Must be 21 or older, except in Germany)";
    stockDescs[5] = "a 20oz Pepsi. Goes great with a sandwhich!";
    stockDescs[6] = "a Boston Terrier puppy. Give it lots of hugs!";
    stockDescs[7] = "a ham sandwhich (goes great with a Pepsi!).";
    stockDescs[8] = "a Tickle Me Elmo (watch out for crazy moms...).";
    stockDescs[9] = "an umbrella.";
}


VendingMachine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include "Stock.h"
#include "Coins.h"

using namespace std;

int main()
{
    coins cash, buffer, returned;       // Declare variables for the coins struct.
    stock inventory, price;             // Declare variables for the stock struct.
    int choice, item;                   // Declare the integer "choice" for us in the switch statement
    char answer;

    cash.setCoins(300,75,30,15);                            // Call function to set the initial amount of cash in the machine.
    inventory.setInventory(3,3,3,3,3,3,3,3,3,3);            // Call function to set the amount of inventory for each item.
    inventory.setStockNames();
    inventory.setStockDescriptions();
    price.setPrice(15,25,200,75,100,30,150,50,250,125);     // Call function to set each item's price 


setStockNames() function definition:
1
2
3
4
5
6
7
void stock::setStockNames()
{
    for(int i=0; i < ITEM_AMT;i++)
    {
        stockName[i] = stockNames[i];
    }
}


setStockDescriptoins() function definition:
1
2
3
4
5
6
7
void stock::setStockDescriptions()
{
    for(int i=0; i < ITEM_AMT;i++)
    {
        stockDesc[i] = stockDescs[i];
    }
}


When not passing anything through as I have it in the above code snippets, it can't find the array variable from stock.cpp. It can still find the constant ITEM_AMT though.

I've tried passing the array through the function as a variable, but I receive a weird error, as if the compiler doesn't like using strings:

|34|error: no matching function for call to 'stock::setStockNames(std::string [10])'|
|17|note: candidates are: void stock::setStockNames(std::string)|

So, any help would be greatly appreciated. :) Or, if you have a better way of setting variables that would be great. I'm trying to design this in a way that would be condusive to what you might see in the real world, depending on how the vending machine was set up. I figure a user somewhere might enter information into a GUI about what items were supposed to go where, and then that information would travel via a network connection to the vending machine.

Previously I was setting values for my descriptions the same way I've been setting them for my prices and so forth, but making a call to setStockDescriptions in the main body of my code with like 3 pages wide worth of text just seems super clunky. I figure there has to be a cleaner and more efficient (and modular) way to do this.

Thanks!!!
Last edited on
Topic archived. No new replies allowed.