Array of structures problems

I have been dealing with this problem for quite some time, I've been assigned to develop a program that can store products' information within structures (the number of products is undefined). I thought I should use an array of structures, but I don't know how to declare it properly.
This is what I thought would work:

1
2
3
4
5
6
struct product {
string name;
string in_stock;
float sale_cost;
int id; }
         prod [n]; //n being the undefined number of products the user will register/delete/modify 


I already saw an example of array of structures, but it uses a defined number.
Can you use std::vector?
new/delete?
If you cannot, just create an array who'll be big enough for your uses.
"assigned to real work" => use std::vector

"assigned to homework" => learn about dynamic memory management.
The program should not deal with dynamic memory management due to being programmed in very basic topics, anyways, so if I just use a big enough array like:

1
2
3
4
5
6
7
8
#define n 500

struct product {
string name;
string in_stock;
float sale_cost;
int id; }
            prod [n];


how can I access it properly in different functions (without using pointers)?
You would just use it like:

1
2
prod[0].id = 100;
prod[0].name = "Item_100";


The in_stock variable looks (by its name) as though it should be a bool and not a string?

I would suggest that you add a default constructor to your struct so that you initially set the member variables to a default value so that you can tell when you have reached the end of the array that has been used, say id = -1, as you have chosen not to use vectors or pointers.
Last edited on
Oh, my bad, I should have specified what the variables mean:

1
2
3
4
5
6
7
8
9
#define n 500

struct product {
string name; //name of the item
int in_stock; //number of the item in stock
float sale_cost; //cost of the item
int id; //Identification number of the item 
}
            prod [n];


(Changed the in_stock type of data to a proper one, though that ajh32 says looks like it might fit in the program later)

So my problem with the syntaxis of the structure is solved, thanks alot. But the other problem, about how to access the data from different functions, remains. Can you help with some examples?
Well it looks like you are declaring prod as global:

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
#include <stdio.h>
#include <string>

using namespace std;

#define n 500

struct product {
string name;
string in_stock;
float sale_cost;
int id; } prod [n];

void func()
{
   prod[1].id = 101;
   // etc ...
}

int main()
{
   prod[0].id = 100;
   prod[0].name = "Item_100";

   func();

   return 0;
}
Last edited on
My bad, again. Yes, I am declaring the prod as global (I should learn to include all those little details before hand).
I see, so declaring it as global lets me access the data from any function? Sorry if it turns out obvious, but I got a misconception about variable usage during my courses.
so declaring it as global lets me access the data from any function?


yes
Fantastic, thanks alot for your help!
If I were you instead of using your struct directly I would encapsulate it in a class that is a bit like a stack, like so:

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
#include <stdio.h>
#include <string>

using namespace std;

#define max 500

struct product
{
   string name;
   string in_stock;
   float sale_cost;
   int id;
};

class Products
{
public:
   Products()
      : count(0)
   {
   }

   void push(product& p)
   {
      prod[count++] = p;
   }

   product& pop()
   {
      return prod[count--];
   }

   product get_at(const unsigned int index)
   {
      product p;
      if (index <= count)
      {
         p = prod[index];
      }

      return p;
   }

private:
   product prod[max];
   int count;
} products;

void func()
{
   product p = {"Item_2", "2", 55.99, 101};
   products.push(p);
   // etc ...
}

int main()
{
   product p = {"Item_1", "1", 123.45, 100};
   products.push(p);
   func();

   return 0;
}
I'll take note about that, I did consider using classes, but is it kind of forbidden for me to use them this semester, as they want us to just use some specific c++ topics. Anyhow, I will keep on mind using classes next time, thanks!
Well just change the Class to a struct and it will still work just the same :-)
Interesting, never thought out of the box in programming like that, thanks again, pal!
Topic archived. No new replies allowed.