数组 C++ 中销售最多和最少的项目

Most and least sold item in array C++

本文关键字:项目 数组 C++      更新时间:2023-10-16

我试图输出数组中销售最多和最少的项目,但它要么不正确,要么值为空,我看不出为什么。每件商品都有一个用于价格、名称和销售数量的数组。我的想法是确定哪个是销售最少的项目,然后使用它来访问名称数组,以便它输入项目的名称而不是最低的数字。

这是我到目前为止所做的:

#include "stdafx.h"
#include <string>
#include <conio.h>
#include <iostream>
#include <array>
using namespace std;
int iNumOfItems = 0;
string sChoice = "";
string sItems[5] = {};
int iItemPrice[5] = {};
int iNumSold[5] = {};
int iItemNum = 0;
int iMostSoldItem = iNumSold[0];
int iLeastSoldItem = iNumSold[0];
int iCount = 0;

int main()
{
    do {
        cout << "--------- ENTER NEW ITEMS ---------nnPlease enter the item Name: ";
        cin >> sItems[iCount];
        cout << "nPlease enter the price of: " << sItems[iCount] << "n";
        cin >> iItemPrice[iCount];
        cout << "nWould you like to enter another item? Y/N n";
        cin >> sChoice;
        if (sChoice == "Y" || sChoice == "y")
        {
            ++iCount;
            ++iNumOfItems;
        }
    } while ((sChoice == "Y" || sChoice == "y") && iNumOfItems < 5);
    //most sold item
    for (int i = 1; i < 5; i++) {
        if (iNumSold[i] > iMostSoldItem) {
            iMostSoldItem = iNumSold[i];
        }
    }
    cout << "nMost sold item: " << sItems[iMostSoldItem];

    //least sold item
    for (int i = 1; i < 5; i++) {
        if (iNumSold[i] < iLeastSoldItem) {
            iLeastSoldItem = iNumSold[i];
        }
    }
    cout << "nLeast sold item: " << sItems[iLeastSoldItem];
    _getch();
    return 0;
}

专注于回答问题,而不是改进代码:

cout << "nMost sold item: "<< sItems[iMostSoldItem];

iMostSoldItem是售出最多商品的价值,而不是指数。要修复,您需要另一个变量来跟踪索引:

int mostSoldItemIndex;

然后

mostSoldItemIndex = 0;
iMostSoldItem = iNumSold[0]; // Move this here
for (int i = 1; i < iNumOfItems; i++) {   // Changed 5 to iNumOfItems
    if (iNumSold[i] > iMostSoldItem) {
        iMostSoldItem = iNumSold[i];
        mostSoldItemIndex = i;   // Save the index
    }
}
cout << "nMost sold item: "<< sItems[mostSoldItemIndex];

您也需要为iLeastSoldItem执行此操作。

我没有注意到,您从不要求用户输入每件售出商品的数量。您需要添加:

cout << "--------- ENTER NEW ITEMS ---------nnPlease enter the item Name: ";
cin >> sItems[iCount];
cout << "nPlease enter the price of: " << sItems[iCount] << "n";
cin >> iItemPrice[iCount];
// Add this
cout << "nPlease enter the number sold of: " << sItems[iCount] << "n";
cin >> iNumSold[iCount];

使用 STL,您的代码可以大大简化。

std::minmax_element可以用一个函数调用替换这两个循环:

#include <algorithm>
#include <iostream>
#include <cstddef>
#include <string>
#include <vector>
struct Item {
  std::string name;
  int price = 0;
  int sold = 0;
};
class Main {
private:
  std::vector<Item> items;
public:
  void input_item () {
    auto &item = items.emplace_back ();
    std::cout << "--------- ENTER NEW ITEMS ---------n";
    std::cout << "n";
    std::cout << "Please enter the item Name: ";
    std::cin >> item.name;
    std::cout << "n";
    std::cout << "Please enter the price of: " << item.name << "n";
    std::cin >> item.price;
    std::cout << "n";
  }
  void input_items () {
    std::string sChoice = "y";
    while ((sChoice == "Y" || sChoice == "y")) {
      input_item ();
      std::cout << "Would you like to enter another item? Y/N n";
      std::cin >> sChoice;
    }
  }
  void input_sold_item_qty () {
    std::cout << "--------- INPUT SOLD ITEMS ---------n";
    std::cout << "n";
    std::cout << "Please select an item: n";
    for (size_t i = 0; i < items.size (); i++) {
      std::cout << 1 + i << ": " << items.at (i).name << "n";
    }
    int item_num;
    std::cin >> item_num;
    std::cout << "n";
    auto &item = items.at (item_num - 1);
    std::cout << "Please enter the ammount sold: " << item.name << "n";
    std::cout << "Quantity: ";
    std::cin >> item.sold;
    std::cout << "n";
    std::cout << "You have sold " << item.sold << " of " << item.name << "n";
  }
  void input_sold_item_qtys () {
    std::string sChoice = "y";
    while (sChoice == "Y" || sChoice == "y") {
      input_sold_item_qty ();
      std::cout << "Would you like to input more sold items? Y/N n";
      std::cin >> sChoice;
    }
  }
  static bool sold_compare (const Item &l, const Item &r) {
    return l.sold < r.sold;
  }
  void print_least_most_sold_items () {
    auto pair = std::minmax_element (std::begin (items), std::end (items), sold_compare);
    std::cout << "Most sold item: " << pair.second->name << "n";
    std::cout << "Least sold item: " << pair.first->name << "n";
  }
};
int main () {
  Main m;
  m.input_items ();
  m.input_sold_item_qtys ();
  m.print_least_most_sold_items ();
}