C++ 严重的结构问题C++;

C++ 严重的结构问题C++;,c++,C++,我迷路了,我很确定这个代码有95%的错误,我需要帮助。我需要做的是拿一个.txt文件,把单词分成一个类型、价格和想要的或要出售的。它基本上像craigslist一样工作,但要求我计算操作量,如果找到匹配项,则从struct数组中删除想要的和要出售的。我真的不知道在匹配后如何将该项从数组中删除,并且我一直收到一个错误消息“std::ifstream没有名为“find”的成员” 代码如下: #include <iostream> #include <string> #incl

我迷路了,我很确定这个代码有95%的错误,我需要帮助。我需要做的是拿一个.txt文件,把单词分成一个类型、价格和想要的或要出售的。它基本上像craigslist一样工作,但要求我计算操作量,如果找到匹配项,则从struct数组中删除想要的和要出售的。我真的不知道在匹配后如何将该项从数组中删除,并且我一直收到一个错误消息“std::ifstream没有名为“find”的成员”

代码如下:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

// Maximum amount to read from a line in the file
const int SIZE = 100;  // Size of input array

char input[SIZE];     // To hold file input

struct itemArray
{
    string type;
    int price;
    int status; //for sale =0, wanted = 1
    int length;
};

void findItem(itemArray[i].type, itemArray[i].price)//searches to see if there is a match, if there is it deletes it from the array
{
    for (int x = 0; x < itemArray.length) //will search for everything in the array
    {
        if itemArray[i].type == itemArray[x].type && itemArray[i].price >= itemArray[x].price && itemArray[x].status ==0;) //if the item outside of the function matches in both type and price then sell it
        {

        }
    }
}

// Function prototypes


int main()
{
    int index;
    int length;
    string line;
    ifstream myfile("messageBoard.txt"); //opening the file
    if(myfile.is_open())
    {
        while (myfile.good())
        {
            getline(myfile, line);

            int indexofcomma = myfile.find(",");

            itemArray[index].type = myfile.substr(0, indexofcomma); //setting the type of item

            string newstr = myfile.substr(indexofcomma+2, myfile.length()-1); //shortening the string to get rest of information


            int newcomma = newstr.find(","); //separate the the word into a item when a comma is detected

            if (newstr.substr(0, newcomma) == "wanted")

                {

                     itemArray[index].forSale = false;  //prints out a 0

                     //findItem(itemArray.type, itemArray.status-1, itemArray.price);

                 } else {

                    itemArray[index].forSale = true; //prints out a 1

                 }

            itemArray[index].price = stoi(newstr.substr(newcomma+2, newstr.length())); //converts string to int and sets price of Item

            index++;

            length++;//keeping track of the length of the array

            cout << itemsArray[10].type << " " << itemsArray[10].price << endl;

        }

        for(int i = 0; i <= itemArray.length; i++)//loop to find every item that is wanted in the list
        {
            if (itemArray[i].status = 1)//if item is wanted
            {
                findItem(itemArray.type[i], itemArray[i].price)//look for a seller with a similar price
            }
        }
    }


    myfile.close;
    return 0;
}
非常感谢您的帮助,您可以说我是白痴或任何您需要的东西,但我需要洞察力。

单一责任原则 学习它。热爱它。遵循它。编写代码,每个部分都有一个而且只有一个责任

在本例中,我将从一个只存储输入文件中一项的结构开始:

struct item { 
    std::string item;
    std::string action;
    int price;
};
然后,我将编写一段代码,将文件中的一项读取到结构中:

std::istream &operator>>(std::istream &is, item &i) { 
    std::getline(is, i.name, ',');
    std::getline(is, i.action, ',');
    return is >> i.price;
}
然后编写代码,将文件中的数据读取到项目向量中:

std::ifstream infile("input.txt");

std::vector<item> items {
    std::istream_iterator<item>(infile),
    std::istream_iterator<Item>(),
};
std::ifstream infle(“input.txt”);
向量项{
标准::istream_迭代器(infle),
std::istream_迭代器(),
};
从这里开始,听起来像是要将数据划分为“通缉”物品和“待售”物品的一部分。然后将“待售”物品与“通缉”物品进行匹配,其中物品描述相同,销售价格低于或等于购买价格

要做到这一点,您可能需要从
std::partition
开始,将待售商品与所需商品分开

然后使用
std::sort
对每一半进行排序(可能是根据名称和价格)


最后,您可以使用
std::set_symmetric_difference
删除匹配的通缉/待售项目。

Jerry Coffin概述了基于需求的软件开发的基本思想,并提供了第一步:决定如何存储基本数据(如果您愿意,可以使用“模式”)

接下来,你需要找到一个算法来解决这个问题。你已经用一些呆板的机械术语描述了它,但这个程序的目的似乎是“匹配买家和卖家,并打印无法匹配的列表。”

让我们用一份买家和卖家的名单来解决这个问题。如果要价和售价完全相等,我们可以通过经纪人达成交易。(这不是特别现实,但似乎是示例数据集中反映的意图。)

如果买家和卖家的名单都进行了排序,我们可以简单地按照相同的顺序阅读,并快速发现谁失踪了。把它想象成一个考勤点名:只要有一张注册学生名单和一张按字母顺序排列的签到表,就很容易看出谁没来上课。浏览一下买家名单,我可以找到这类人是谁“考勤检查。”

要对数据进行排序,我们需要定义一个小于的关系。我们将先对买家进行排序,然后再对卖家进行排序,以便将排序后的数据的两部分作为单独的列表处理。这种关系可以称为
操作符>(std::istream&s,item&i){
std::getline(s>>std::ws,i.item,',');
std::getline(s>>std::ws,i.action,',');
价格;
返回s;
}

std::ostream&operator您正确地调用
getline
将一行读入字符串,但随后您在文件流上调用
find()
,而不是字符串。我无法帮助回答您的其余问题,因为它有点太宽。
myfile.close
可能应该是我
myfile.close()
还有……你是说,
我肯定这是95%*对吧*
?@你当然可以用一个调用来
排序
,但是对于那些看起来像如果只是从文件中读取数据比较困难,我宁愿避免。我没有真正考虑过
set\u symmetric\u difference
,但是重读一遍,你是对的——因为他只想删除匹配的项,这可能是最合适的(我本来想得到一个匹配项的列表,但你们说得对——他似乎不想要那个)。
std::ifstream infile("input.txt");

std::vector<item> items {
    std::istream_iterator<item>(infile),
    std::istream_iterator<Item>(),
};
#include <string>
#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>

typedef std::string item_action;
const item_action sell = "for sale";
const item_action buy = "wanted";

struct item {
    item_action action;
    std::string item;
    int price;
};

// Compare such that matching "buy" and "sell" are equal.
// (Neither is less-than the other.)
bool item_price_comparison( item const & lhs, item const & rhs ) {
    return lhs.item < rhs.item? true
        : rhs.item < lhs.item? false
        : lhs.price < rhs.price;
}

// Compare such that sells come before buys.
bool operator < ( item const & lhs, item const & rhs ) {
    return lhs.action < rhs.action? true
         : rhs.action < lhs.action? false
         : lhs.item < rhs.item? true
         : rhs.item < lhs.item? false
         : lhs.price < rhs.price;
}

std::istream & operator >> ( std::istream & s, item & i ) {
    std::getline( s >> std::ws, i.item, ',');
    std::getline( s >> std::ws, i.action, ',');
    s >> i.price;
    return s;
}

std::ostream & operator << ( std::ostream & s, item const & i ) {
    s << i.item << ", ";
    s << i.action << ", ";
    s << i.price << "\n";
    return s;
}

int main() {
    std::ifstream file( "messageBoard.txt" ); // Open the file.
    std::vector< item > items; // Create the "database."
    std::copy( std::istream_iterator< item >( file ), {},
               std::back_inserter( items ) ); // Read the data.

    std::sort( items.begin(), items.end() ); // Organize the data.
    // Split list: items before first_buyer are for sale, after are wanted.
    auto first_buyer = std::lower_bound( items.begin(), items.end(),
                           item{ buy } );

    std::set_symmetric_difference( // Find mismatches between...
        items.begin(), first_buyer, // ... the list of sellers...
        first_buyer, items.end(), // ... and the list of buyers...
        std::ostream_iterator< item >( std::cout ), // and print the results.
        item_price_comparison // Let matching buy and sell be equal.
    );
}
// Compare such that matching "buy" and "sell" are equal.
// (Neither shall be less-than the other if they have a deal.)
// Assume that we are comparing one buy with one sell.
bool item_price_comparison( item const & lhs, item const & rhs ) {
    item const & buyer = lhs.action == buy? lhs : rhs;
    item const & seller = lhs.action == sell? lhs : rhs;

    return lhs.item < rhs.item? true
        : rhs.item < lhs.item? false
        : seller.price < buyer.price? false // We have a deal.
        : lhs.price < rhs.price; // Usual rule applies if no deal.
}