Need help linking structure with function and switch statement.

I'm having an issue with my functions. It's saying certain identifiers aren't declared. I've already declared them in main. When I try to compute it within the switch statement instead of the function the program runs until it has to do the math.

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

using namespace std;

    struct bins {
                   string partDesc; 
                   int numParts;
                   };
    const int myParts = 10;
    
    bins description[myParts]={
                                    {"Valve", 10},
                                    {"Bearing", 5},
                                    {"Bushing", 15},
                                    {"Coupling", 21},
                                    {"Flange", 7},
                                    {"Gear", 5},
                                    {"Gear Housing", 5},
                                    {"Vacuum Gripper", 25},
                                    {"Cable", 18},
                                    {"Rod", 12}
                                    };
void display(bins);             
int AddParts(bins&);                    
int RemoveParts(bins&);

int main()

{
    int i;
    int bin;
    int add;
    int add_part;
    int sub;
    int choice2;
    char choice1;
    bins part;
    
    display(part);
    
    do
    { 
    cout << "Would you like to modify a bin? Y/N: ";
    cin >> choice1;
    if(choice1 == 'Y' || choice1 == 'y');
    {
    cout << "Enter 0 for Valve, 1 for Bearing, 2 for Bushing, 3 for Coupling, 4 for Flange, 5 for Gear, 6 for Gear Housing, 7 for Vacuum Gripper, 8 for Cable, 9 for Rod: ";
    cin >> bin;
   
    cout << "Would you like to add or remove to the bin? Type 1 to add, 2 to remove: ";
    cin >> choice2;
    
    switch (choice2)
    {
    case 1:
                       
         break;
    case 2:
         
         RemoveParts(part);
         break;
    }
}
    cout << endl;
    cout << "Would you like to continue? Y/N: ";
    cin >> choice1;
    while (choice1 == 'N' || choice1 == 'n');
    display(part);



system("pause");

}

while (choice1 == 'N' || choice1 == 'n');
return 0;

}  

void display (bins) 
{
    for (int i=0; i < myParts; i++)
    {    
        cout << "Part Description: " << description[i].partDesc << endl;
        cout << "Number of Parts in the Bin: " << description[i].numParts << endl;    
    }
}

int AddParts (bins& numParts)                           
{
    for (int i=0; i < myParts; i++)
    {
        cout << "How many parts would you like to add? ";
        cin >> add;
        add_part = description.numParts[i] + add;
        cout << "Bin " << bin << " now has: " << add_part << endl;    
    }
}

int RemoveParts (bins& numParts)
{
    for (int i=0; i < myParts; i++)
    {
        cout << "How many parts would you like to remove? ";
        cin >> sub;
        sub_part = description[i].numParts - sub;
    }
}
Last edited on
Line 45: Get rid of the ;

Line 6: You need to include the <string> header.

Line 95: add is undefined. You can't reference a local variable declared in main.

Line 96: Ditto. Your subscript is also in the wrong place.

Line 97: Ditto.

Line 106: Ditto.

Line 90, 101: Your arguments make no sense.



Last edited on
I've changed the arguments in lines 90 and 101. How do I declare the variables used in functions outside of main?
You need to pass them as arguments.

Line 56: You're not calling AddParts.

Lines 90 & 101 you're declaring an argument of type bins named numParts, but you're not using the argument anywhere. Note that lines 96 and 107 refer to the global structure.

Line 37: You're declaring a single instance of type bins. This structure is uninitialized. This is what you pass to addParts, removeParts and display, but because the argument is not used, it doesn't make any difference.

Note that the variables add and sub do not need to be passed from main. They can be simple local variables within addPart and removePart respectively.

Your calculations on lines 96 and 107 are also defective. They don;t update the global structure.
Last edited on
Topic archived. No new replies allowed.