数组 C++ 中销售最多和最少的项目
Most and least sold item in array C++
我试图输出数组中销售最多和最少的项目,但它要么不正确,要么值为空,我看不出为什么。每件商品都有一个用于价格、名称和销售数量的数组。我的想法是确定哪个是销售最少的项目,然后使用它来访问名称数组,以便它输入项目的名称而不是最低的数字。
这是我到目前为止所做的:
#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 ();
}
相关文章:
- 有没有办法使用输入的维度初始化 2d 数组中的所有项目而不循环C++?
- 为什么 C++ 中的字符数组返回其项目而不是地址?
- 数组中无法解释的最后一个项目
- 数组 C++ 中销售最多和最少的项目
- 指向单个项目或数组的指针
- 如何从类中返回自定义数组项目并操纵其属性?C
- 计数器将项目添加到数组中,并剩余
- 如何将类的项目添加到C 的数组中,而无需给它们命名
- 适当的JNI转换将数组项目从jobjectarray到分开const char
- C++检查项目是否在数组中
- C,如何将多维数组传递给CLR/类库项目中的函数
- 将项目插入数组
- C++从指针数组中删除项目
- 根据世界位置查找一维数组中的通用项目
- 在C++中初始化数组时出现 Seg 错误(欧拉项目 3)
- 对包含三个项目的数组进行排序
- 这是一个具有基于数组的列表函数的数据结构项目,我似乎无法摆脱这个无限循环。
- 如何在C++中创建一个返回字节数组的函数?Arduino项目
- 基类的指针数组;每次项目启动我都有不同的结果
- 从庸医(队列/堆栈)中包含的圆形数组顶部弹出一个项目