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

时间:2018-04-09 14:06:00

标签: c++ arrays

我试图在数组中输出最多和最少销售的项目,但它输出不正确或空值,我不明白为什么。每个项目都有一个数组,用于显示价格,名称和销售数量。我的想法是确定哪个销售量最少的商品然后使用它来访问名称数组,这样就可以输出商品名称而不是最低数字。

到目前为止我所做的事情:

#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 ---------\n\nPlease 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;
}

2 个答案:

答案 0 :(得分:3)

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

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 ---------\n\nPlease 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];

答案 1 :(得分:3)

使用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 ();
}