pointers/namespace/class

how might i use namespace here?

Develop software using dynamic arrays (using the new and delete operators) that allows a user to enter the names of the athletic equipment below. Your program must use pointer variables for all variables.


here's my take:

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
#include <iostream>
#include <vector>

using namespace std;

#define SIZE 5

#ifndef INVENTORY
#define INVENTORY

//definition of inventory structure
typedef struct inventory
{
   string name_equipment;
   double cost;
   int quantity;
}inventory;
#endif

//function to display inventory
void display(inventory *inv)
{
   cout<<"Name of equipment\t Cost\t Quantity"<<endl;
   for(int i=0; i<SIZE; i++)
   {
       cout<< inv[i].name_equipment<<"\t ";
       cout<< " $"<< inv[i].cost<<"\t ";
       cout<< inv[i].quantity <<endl;
   }
}

//main function
int main()
{
   inventory *inv = new inventory[SIZE];
   vector<inventory> cart;
  
   inv[0] = {"Nike basketball shoes" , 179.99, 42};
   inv[1] = {"Under Armour T-shirt" , 29.99, 44};
   inv[2] = {"Brooks running shoes" , 121.44, 13};
   inv[3] = {"Asics running shoes" , 165.88, 12};
   inv[4] = {"Under Armour shorts" , 45.77, 35};
      
   cout<<"*** RECEIPT *** "<<endl;
   cout<<"1. Nike basketball shoes $179.99 x4 "<<endl;
   cout<<"2. Under Armour T-shirt $ 29.99 x3 "<<endl;
   cout<<"3. Under Armour shorts $ 45.77 x4 "<<endl;
   cout<<"4. Asics running shoes $165.88 x1 "<<endl;
  
   //i might make a function for this in case i want to test with user input
   double total = 4 * 179.99 + 3 * 29.99 + 4 * 45.77 + 165.88;
   cout<<" Total Cost: $"<<total<<endl;
   double tax = total * 8.25 / 100;
   cout<<" Tax (8.25%): $"<<tax<<endl;
   cout<<" Total Cost with Tax : "<<(total +tax)<<endl;
  
   cout<<"\n\nInventory before Mark\'s purchase: "<<endl;
   display(inv);
  
   inv[0].quantity-=4;
   inv[1].quantity-=3;
   inv[4].quantity-=4;
   inv[3].quantity-=1;
  
   cout<<"\n\nInventory after Mark\'s purchase: "<<endl;
   display(inv);

   delete []inv;
  
   return 0;      
}
Last edited on
Develop software using dynamic arrays
I think you're expected to create your own array class, rather than using std::vector. The exercise is probably to help you learn how std::vector works.

Your program must use pointer variables for all variables.
Doesn't make a whole lot of sense to me.
typedef struct is ancient/C code. you do not need the typedef anymore, its a type already in modern c++. Likewise putting the variable after the } is bad form now, its making a global variable that is no longer useful (this again is 1990s era or earlier stuff that is long dead but still supported).

A class and a struct are virtually interchangeable in c++. If you explicitly make public and private blocks in your objects, then you can swap the keyword and nothing changes at all. If you rely on the default public and private, they are reversed (this is the only difference) with class default to private and struct default to public.

pointers you may want to read a bit on but in an nutshell, if you consider ram of your computer to be an array of bytes, then a pointer is an index into that array.
so
int x[10]; //represents ram
int y = 3; //represents a pointer
cout x[y]; //y is a lot like a pointer, its a position into the array.
the syntax for a pointer is nothing like the above, but conceptually, it is the same. You ask the OS for a location (or block thereof) that it is not using, it gives it to you, and when done, you give it back.
the above, in pointer lingo, then:
int *y = new int[10]; //y is still an offset into the x array (ram) its just unsaid now, it is now understood that ram exists out there somewhere and that y is offset into it.
delete[] y; //return the memory to the OS

ok, so to do this you need to keep #4 in mind and make a lot of pointers. This is an idiotic requrement, but you can do it.
start there, and try to make your object (struct or class) using pointer variables, and a constructor for it (this is where your new statements go for all those pointers) and a destructor for it (this is where all the deletes go).

a little to start:

1
2
3
4
5
6
7
8
9
10
11
12
13

struct inventory
{
   string *name_equipment;
   double *cost;
   int* quantity;
   inventory() 
   {
   name_equipment = new string;
   cost = new double;
   quantity = new int;      
   }
};


display should be a member function, not on the side. that is also a C concept, in c++ the function belongs with the object, in C, you have it external and may tie it with a function pointer (lets not do that today) at best or leave them decoupled (a bit troubling to organize and maintain).
Last edited on
Topic archived. No new replies allowed.