如何更改数组的值然后显示整个新数组?

时间:2015-02-02 00:35:17

标签: c++ arrays

我需要帮助。我有一个10的数组,它由一个字符串和一个初始化它的值组成。我的问题是我如何要求用户输入要添加的内容或要删除的内容。然后显示更新的列表。 例如,选择valve,然后再添加2个,这将使其成为12,然后使用其他数组显示更新的数组。我还需要通过一个函数传递它。请提前感谢您的帮助,到目前为止我的代码。随意批评你们想要的人。它只会帮助我变得更好:)

#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <cstring>
#include <string>
using namespace std;
//define Structure as invParts
struct invParts{

string name;
int qty;


};


void addparts(invParts *&c,int);
void removeparts(invParts *&c, int);




int main()
{
invParts *descrip;
int row = 0;
char choice;


invParts bin[10]= { {"valve",10}, {"Bearing",5}, {"Bushing",21},
                     {"Coupling",7}, {"Flange",5}, {"Gear",5}, 
                     {"Gear House", 5}, {"Vacuum         Gripper", 25}, 
                     {"Cable",18}, {"Rod",12}, };



cout<<"-----------------------------------------------------" << endl;
cout<<"Part Description" << "      " << "Number of parts in the bin" << endl;
cout <<"----------------------------------------------------" << endl;
cout << endl;
for(row = 0; row < 10; row++)
{
    cout << setw(11)<< left <<bin[row].name << setw(25) << right << bin[row].qty<< endl;



}
cout << endl;
cout << "Here are 3 options" << endl;
cout << "Type A , to Add parts" << endl;
cout << "Type R , to Remove parts" << endl;
cout << "Type E, to Exit Program" << endl;
cout << "Choose your option: "; 
cin >> choice;
cout << endl;
switch (choice)
{
int num;
case 'A' :
case 'a' :  cout <<"You will now add" << endl;
            addparts(descrip,num);
            break;

case 'R':
case 'r': cout <<"You will now remove" << endl;
          //removeparts(descrip,num);

             break;
case 'E':
case 'e': cout<<"Now exiting program" << endl;
            exit(0);




}






system("pause");
return 0;

}

void addparts(invParts *&c,int number)
{
    string what;
    int n;


    cout <<"Which part? " << endl;
    cin >> what;


         //am i doing this right?

    if ( what == "valve" || what == "Valve")
        cout <<"How much do you want to add? "<<endl;
        cin >> n;



}
/*void removeparts(invParts *&c, int)
{

//you guys can show me how to do the add, i can do the remove


}
*/

1 个答案:

答案 0 :(得分:1)

您使用错误的数据结构来捕获广告资源。你需要像

这样的东西
typedef std::map<std::string, int> Inventory;

这是您的计划的更新版本。

#include <iostream>
#include <string>
#include <iomanip>
#include <map>

using namespace std;

typedef std::map<std::string, int> Inventory;

void initializeInventory(Inventory& inv);
void displayInventory(Inventory const& inv);
void addparts(Inventory& inv);
void removeparts(Inventory& inv);


int main()
{
   Inventory inv;
   char choice;

   initializeInventory(inv);

   displayInventory(inv);

   while ( true )
   {
      cout << "Here are 3 options" << endl;
      cout << "Type A , to Add parts" << endl;
      cout << "Type R , to Remove parts" << endl;
      cout << "Type E, to Exit Program" << endl;
      cout << "Choose your option: "; 
      cin >> choice;
      cout << endl;
      switch (choice)
      {
         case 'A' :
         case 'a' :  cout <<"You will now add" << endl;
                     addparts(inv);
                     break;

         case 'R':
         case 'r': cout <<"You will now remove" << endl;
                   //removeparts(inv);

                   break;
         case 'E':
         case 'e': cout<<"Now exiting program" << endl;
                   exit(0);
      }

      displayInventory(inv);
   }

   return 0;
}

void initializeInventory(Inventory& inv)
{
   inv["Valve"] = 10;
   inv["Bearing"] = 5;
   inv["Bushing"] = 21;
   inv["Coupling"] = 7;
   inv["Flange"] = 5;
   inv["Gear"] = 5; 
   inv["Gear House"] =  5;
   inv["Vacuum Gripper"] =  25;
   inv["Cable"] = 18;
   inv["Rod"] = 12;
}

void displayInventory(Inventory const& inv)
{
   cout<<"-----------------------------------------------------" << endl;
   cout<<"Part Description" << "      " << "Number of parts in the bin" << endl;
   cout <<"----------------------------------------------------" << endl;
   cout << endl;

   for (auto item : inv )
   {
      cout << setw(15) << left << item.first << setw(25) << right << item.second << endl;
   }

   cout << endl;
}

void addparts(Inventory& inv)
{
   string what;
   int n;

   cout <<"Which part? " << endl;
   cin >> what;


   //am i doing this right?
   Inventory::iterator iter = inv.find(what);
   if ( iter == inv.end() )
   {
      cout << "There is no such part in the inventory.\n";
      return;
   }

   cout <<"How much do you want to add? "<<endl;
   cin >> n;

   iter->second += n;
}

void removeparts(Inventory& inv)
{
   // ????
}

更新

可以使用以下方式初始化库存:

Inventory inv = { {"Valve",10}, {"Bearing",5}, {"Bushing",21},
                  {"Coupling",7}, {"Flange",5}, {"Gear",5}, 
                  {"Gear House", 5}, {"Vacuum Gripper", 25}, 
                  {"Cable",18}, {"Rod",12} };

无需单独的功能来初始化库存。

相关问题