2014-03-27 3 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} 
            }; 

는 누적으로 일을하려고처럼 9 0에서 배열의 요소를 넣지 않고 그것을 할 수있는 다른 방법이 있습니까. 배열에서 특정 요소를 취하는 방법은 무엇입니까?

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

'위해 (; 나는 Number_Bins를

+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; 
} 

는 그런 다음 배열 요소와 그 함수를 호출 할 수 있습니다. ":"<< 빈 [I] {COUT << 빈 [i]를 .description <<

RemovePart(bin[1]); 
+0

은 제거하거나 추가하려는 부품 수를 묻습니다. 그리고 새로운 결과가 목록에 표시되어야합니다. – user3019164