Need some much needed help

I have some of the code done but can someone show me how to do array and function?


This is my assignment


Modification 1: You will convert each of your four menu options from Programming Assignment 1 into a functions. For example, case 1: instead of having the code in the case statement to print your products, you will call a global function that will print the products out. It will look like:

case 1:

printProducts(YOUR_PARAMETER_LIST);

break;

case 2:



These functions will be located in a header file called functions.h. Be sure to include this file at the top of file main.cpp.

Modification 2: Your product information will now be contained in a structure (struct). This structure will house all of the product’s variables (UPC, product name, etc). This structure definition will be in the functions.h header file. You will have all of the same variables as before.

Modification 3: In main, you will create an array of these structures that will hold all of your products. You will also have a constant variable called NUM_PRODUCTS that will hold the number of products that your array has. For this assignment that number is 2, but for Programming Assignment 3 that number will increase. You must create your program with scalability in mind, meaning that if I change the value of NUM_PRODUCTS, your code will still work without any further modifications.

Modification 4: When you create a product, you will set their information to the default values used in Programming Assignment 1. The UPC will now be randomly generated and cannot be changed by the user (for a default value it will be 0, but once the product is edited it will be randomly generated). In option 2 (edit), instead of asking the user for a UPC, you will generate a new one (each time a product is edited you will generate an new UPC). The format for the UPC is 1XXXXX [that is a one with five digits after it]; where the X’s are randomly generated. Hint: generate a random number between 1 and 99,999 then add that to 100,000 (think scale and shift like the dice). There will be no error checking for duplicates, if duplicates happen that is okay.

Modification 5: For each of your functions, you will pass as arguments to the function the array and the number of students in the array (remember that arrays are passed by reference).

File Upload
Text Entry

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
135
136
137
138
139
140
141
142
  #include <iostream> // needed
#include <iomanip> // for setw()
#include <string> // for string 
using namespace std;

int main ()

{
        int UPC1, quantity1, choice;
        double price1, tValue1;
       
        int UPC2, quantity2;
        double price2, tValue2;

        //strings
        string man1= "Manufacture";
        int size = man1.length ();
        string product1 = "Product";
        string man2 = "Manufacture";
        string product2 = "Product";

       
        int x = -1; 
       
        const int DISPLAY_CHOICE = 1, //This is my menu
                  EDIT_CHOICE = 2,
                  RESET_CHOICE = 3,
                  EXIT_CHOICE = 4;

       
        cout << "1. Display products\n"
             << "2. Edit product\n"
             << "3. Reset Product to Default\n"
             << "4. Exit\n"
             << "Please enter your choice: ";

        cin >> choice;


        int display;
        cout << setprecision (2) << fixed << showpoint;

        if (choice == 1)
        { 
        cout << "Display either product 1 or product 2? Enter 1 or 2 " << endl; 
        cin >> display;

        if (display == 1)
        { 
        cout << "Product 1" << endl;
        cout << setw (0) << " UPC: " << setw(11) << UPC1 << endl;
        cout << setw (0) << " Manufacturer: " << setw(5) << man1 << endl;
        cout << setw (0) << " Product Name: " << setw(5) << product1 << endl;
        cout << setw (0) << " Price:  $" << setw (0) << price1 << endl;
        cout << setw (0) << " Quantity: " << setw (5) << quantity1 << endl;
        cout << setw (0) << " Total Value:  $" << setw (0) << tValue1 << endl;
        } 

        else if (display == 2)
        { 
        cout << "Product 2" << endl;
        cout << setw (0) << " UPC: " << setw (11) << UPC2 << endl;
        cout << setw (0) << " Manufacturer: " << setw (5) << man2 << endl; 
        cout << setw (0) << " Product Name: " << setw (5) << product2 << endl;
        cout << setw (0) << " Price:  $" << setw (0) << price2 << endl;
        cout << setw (0) << " Quantity: " << setw (5) << quantity2 << endl;
        cout << setw (0) << " Total Value:  $" << setw (0) << tValue2 << endl;
        } 
   

        if (display == 1) 
        { 
        cout << "Enter the Universal Product Code1." << endl;        cin >> UPC1;

        cout << "Enter manufacturer1." << endl;
        cin >> man1;

        cout << "Enter the name of product1." << endl;
        cin >> product1;

        cout << "Enter price1." << endl;
        cin >> price1;

        cout << "Enter quantity1." << endl;
        cin >> quantity1; 
        }
        
        if (choice == 2)
        {  

        cout << "Edit either product1 or product2? Enter 1 or 2." << endl;
        cin >> display;
        }
       
        else if (display == 2)
        { 
        cout << "Enter the Universal Product Code2." << endl; 
        cin >> UPC2;

        cout << "Enter manufacturer2." << endl;
        cin >> man2;
     
        cout << "Enter the name of  product2." << endl;
        cin >> product2;
        
        cout << "Enter price2." << endl;
        cin >> price2;

        cout << "Enter quantity2." << endl;
        } 
        } 



        if (display == 1)
        {
        cout << "display1" << endl;
       
        if (choice == 3)
        { 
        cout << "Set either product1 or product 2 to default values?" << endl;
        cout << "Input 1 for product1 or 2 for product 2." << endl;
        cin >> display;


        
        }  


        }


   

        
        
       

        }


Hi,

i don't understand your question and i don' know what this setw() - thing should be ...

anyway ... what you need is FUNCTIONS in your header- file, which will look like

1
2
3
4
5
6
7
8
void printProducts( ... args ...){

 ... here comes all the statements....
what the function does, which should be (mainly) your code from above, if it works ...
... just as "main" with a different name ...

}


and your "args" are an array of structs:

1
2
3
4
5
6
7
8
 struct prod{
int UPC;
char* name; // or maybe string ...

...etc...
}
typedef struct prod PRODUCT;


then you have a type named Product.
Now, declare an array of type PRODUCT just as an array of type double, maybe like

 
PRODUCT arr_prod[10];

for example .... with a fixed length.


Don't know if taht helps...
Last edited on
Topic archived. No new replies allowed.