长时间用户,第一次海报 . 我的作业中存在二进制搜索功能的问题(除了这一个问题,我完全已经完成了) .

该程序允许用户管理销售各种类型产品的小商店的库存 . 它将首先从名为“inventory.dat”的文本文件中读取库存,读取产品名称,sku,数量和价格 . 它将包含多达100个产品 .

然后,它应该为用户提供一个菜单,其中包含以下选项:

  • 显示按sku排序的库存 . (displayInventory函数)

  • 通过sku查找产品 . (lookupSku函数)

  • 按名称查找产品 . (lookupname函数)

  • 退出

程序应执行所选操作,然后重新显示菜单 .

当我选择选项2并输入sku编号时,我的程序会输出随机项目的信息,而不是用户输入的信息 .

这是我在main函数中开始的代码以及lookupBySku函数:

int main()
{ 

// Initialize variables & array
int userInput;

//max array size
const int MAX_SIZE = 100; //const to make it constantly be 100
//declare and open input file
ifstream fin;
fin.open("inventory.dat");

//declare strings to hold data
string productName, sku, quantity, price;

if (!fin)  // checking if file was inputted correctly or not.
{
    cout << "File not found! Try again. " << endl;
    return 1;
}

    Inventory items[MAX_SIZE];
    Inventory temp[MAX_SIZE];


cout << left << setw(20) << "Product Name " <<
left << setw(10) << "sku #" <<
left << setw(3) << "Quantity " <<
left << setw(0) << " Price\n";


//declare number of objects in inventory
int numOfObjects = 0;

while(!fin.eof())
{
    getline(fin, productName);
    getline(fin, sku);
    getline(fin, quantity);
    getline(fin, price);
    if(productName == "")
    {
    break;
    }
    items[numOfObjects].name = productName;
    cout << left << setw(20) << items[numOfObjects].name;
    items[numOfObjects].sku = sku;
    cout << left << setw(10) << items[numOfObjects].sku;
    items[numOfObjects].quantity = quantity;
    cout << left << setw(4) << items[numOfObjects].quantity;
    items[numOfObjects].price = price;
    cout << left << setw(0) <<"\t\t" << items[numOfObjects].price << endl;
    ++numOfObjects;
}

//check to see if there are more than 100 items
if(numOfObjects > 100)
{
    cout << "File is too large, terminating program...\n";
    return -1;
}

// Constants for menu choices
const int DISPLAY_BY_SKU = 1,
LOOKUP_BY_SKU = 2,
LOOKUP_BY_NAME = 3,
QUIT_CHOICE = 4;

do
{

// Set up numeric output formatting.
cout << fixed << showpoint << setprecision(2);
cout << "Manage Inventory Menu\n" <<endl;
cout << "1. Display inventory sorted by sku. " <<
"\n2. Lookup a product by sku. " <<
"\n3. Lookup a product by name. " <<
"\n4. Quit the program\n" << endl;

cout << "Enter your choice : ";
cin >> userInput;
cout << endl;


while (userInput < DISPLAY_BY_SKU || userInput > QUIT_CHOICE)
{
    cout << "Please enter a valid menu choice: ";
    cin >> userInput;
}

   switch(userInput)
    {
        case DISPLAY_BY_SKU:
        {
          displayInventory(items, numOfObjects);
          break;
        }
        case LOOKUP_BY_SKU:
        {
        lookupSku(items, numOfObjects);
        break;
        }
        case LOOKUP_BY_NAME:
       {
        lookupName(items, numOfObjects);
        break;
       }
    case 4:
        cout << "Exiting the program." <<endl;
        break;
    }
}
while (userInput != QUIT_CHOICE);

fin.close();
return 0;   }


 void lookupSku (Inventory items[], int numOfObjects)  {
     string number;
     int first = 0;
     int last = numOfObjects - 1;
 int middle;
 int position = -1;
 bool found = false;


 cout << "Enter the sku that you'd like to search for : ";
 cin >> number;
 cout << endl;


     while(!found && first <= last)
     {
         middle = (first + last)/2;
         if(items[middle].sku == number)
         {
             found = true;
             position = middle;
         }
             else if(items[middle].sku > number)
             {
                 last = middle - 1;
             }
                 else if(items[middle].sku < number)
                 {
                     first = middle + 1;
                 }
        }

 cout << "Your item is shown below : \n" << endl
 << "Product Name : "<< items[middle].name << endl
 << " Sku : " << items[middle].sku << endl
 <<" Quantity : " << items[middle].quantity << endl
 << " Price : " << items[middle].price << endl
 << endl;
  }