2014-03-27 131 views
0

該程序從10個倉位中選擇具有相同數量零件的任何倉。當程序選擇一個時,它會詢問我們是要添加還是刪除特定倉中的零件。我如何從結構數組中獲取元素。
//結構 結構庫存 {獲取結構數組中的元素

char description[35]; 
    int num; 
}; 

//Function Prototypes. 

void choiceMenu(Inventory[], int); 
void AddParts(Inventory[], int); 
void RemoveParts(Inventory[]); 

int main() 
{ 
    char election; 
    int choice; 

    const int Number_Bins = 10; 
    Inventory parts[Number_Bins] = { 
            {"Valve", 10}, 
            {"Bearing", 5}, 
            {"Bushing", 15}, 
            {"Coupling", 21}, 
            {"Flange", 7}, 
            {"Gear", 5}, 
            {"Gear Housing", 5}, 
            {"Vacuum Gripper", 25}, 
            {"Cable", 18}, 
            {"Rod", 12} 
            }; 

是否有其他辦法做到這一點不把從0數組的元素到9喜歡嘗試用蓄電池做。我如何從數組中獲取特定的元素。

void choiceMenu(Inventory bin[], int z) 
{ 


    cout << "        Inventoy Bins\n"; 
    cout << "        = = = = = = = = \n"; 
    cout << " *Choose the part of your preference.\n"; 
    cout << " 1. Valve" << bin[0].num << endl; 
    cout << " 2. Bearing. Currently Number of Bearing = " << bin[1].num << endl; 
    cout << " 3. Bushing. Currently Number of Bushing = " << bin[2].num << endl; 
    cout << " 4. Coupling. Currently Number of Coupling = " << bin[3].num << endl; 
    cout << " 5. Flange. Currently Number of Flange = " << bin[4].num << endl; 
    cout << " 6. Gear. Currently Number of Gear = " << bin[5].num << endl; 
    cout << " 7. Gear_Housing" << bin[6].num << endl; 
    cout << " 8. Vacuum_Gripper" << bin[7].num << endl; 
    cout << " 9. Cable. Currently Number of Cable = " << bin[8].num << endl; 
    cout << " 10. Rod. Currently Number of Rod = " << bin[9].num << endl;  
    cout << " 11. Choose 11 to quit the Program" << endl; 

}

+1

'爲(INT I = 0; I

+0

這將使所有的箱子改變它們中所有部件的數量。 – user3019164

+0

我不明白你的問題,所以我猜。 –

回答

0

我想你問如何通過數組元素,增加或刪除部分功能之一。您可以通過使用參考變量(http://www.cprogramming.com/tutorial/references.html)來完成此操作。

例如,在你的情況下,該功能將刪除部分:

void RemovePart(Inventory& part) 
{ 
    if (part.num > 0) 
     part.num -= 1; 
    cout << part.description << " now has " << part.num << " parts." << endl; 
} 

然後就可以調用與數組元素的功能。例如,這消除一個軸承:

RemovePart(bin[1]); 
+0

將詢問用戶想要移除或添加的部件數量。新的結果必須顯示在列表中。 – user3019164