2

我似乎无法弄清楚为什么我的代码不起作用。

我的程序是关于读取两个代表cardealership 的文本文件,并根据模式将输入放入链接列表或STL 列表中。然后读取订单并根据可用性创建错误日志。问题是它一直在链表模式下循环,并且似乎没有写入错误日志。

我对此感到非常愚蠢。我不希望你们解决错误,但教我如何去做。我感谢更有经验的人对我的代码的任何审查。我已经尝试在 XCode 和 VS2012(带有 Win8 的 VM)中的 Eclipse 中进行调试。在所有 IDE 中,变量都没有显示在我不理解的调试编辑器中。我使用 MacOSX GCC 编译器。

所以这里是txt文件:input.txt

Brera 3
Golf 5
Punto 13
Fiesta 19

和orders.txt

323 Brera 1
324 Golf 6
354 Punto 3
337 Gobldibock 1

这是我的主要方法:

// file main.cpp
#include "cardealership.hpp"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

int main( int argc, char* argv[] ) {
std::cout << "argv[2]: " << argv[2] << "\n";
std::cout << "argv[3]: " << argv[3] <<"\n";
const unsigned int mode = atoi( argv[1]);
std::string arg2 = argv[2];
std::string arg3 = argv[2];

cardealership* dealer = new cardealership(arg2, arg3, mode);
std::cout << "dealership created" << "\n";
dealer->readInputFileToList();
std::cout << "still running" << "\n";
dealer->readOutputFileAndAlterInventory();
std::cout << "finishing" << "\n";

return 0;
}

以下是包含实际列表的类的标题:

// file cardealership.hpp
#ifndef CARDEALERSHIP_HPP   // prevent multiple inclusions
#define CARDEALERSHIP_HPP

#include <string>
#include <list>
#include <vector>

typedef struct linkedNode
{
  char* data;               // will store information
  int amountOfCars;
  linkedNode* next;             // the reference to the next node.
};

class cardealership
{
public:

cardealership (std::string inputFile, std::string ordersFile, const unsigned int mode);
~cardealership();

  void readInputFileToList();
void readOutputFileAndAlterInventory();
  void printInventory (); //TODO

private:
  const unsigned int mode;
std::string inputFile;
std::string ordersFile;

std::list<linkedNode*> listOfCars; //Using Linked Node without linking them...
std::vector<linkedNode*> linkedListOfCars;
};
#endif

和页脚:

// file cardealership.cpp
#include "cardealership.hpp"
#include <iostream>
#include <cstring>
#include <fstream>
#include <sstream>

cardealership::cardealership(std::string inputFile, std::string ordersFile, const unsigned int mode)
: inputFile(inputFile),
ordersFile(ordersFile),
mode(mode)
{}

cardealership::~cardealership()
{
listOfCars.clear();
linkedListOfCars.clear();
}
void cardealership::readInputFileToList(){
std::ifstream infile;
std::string line;
infile.open(inputFile.c_str(), 
            std::ifstream::in);
if (!infile.good()){
    std::cout << "Na na na Input File" << "\n";
}
linkedNode* previousNode;

while(getline(infile, line)){

    char* model;
    int amountOfCars;
    linkedNode* tmpNode;
    std::stringstream helperStream;

    getline(infile, line);

    helperStream << line;
    helperStream >> model;
    helperStream >> amountOfCars;

    //Test
    std::cout << "Line: " << line << std::endl;
    std::cout << model << ", " << amountOfCars << std::endl;

    tmpNode->data = model;
    tmpNode->amountOfCars = amountOfCars;

    if (mode == 0) { //Use linked list
        if(previousNode != NULL){
            previousNode->next = tmpNode; //Link that shit
            previousNode = tmpNode;
            linkedListOfCars.push_back(tmpNode);
        }
        else{
            linkedListOfCars.push_back(tmpNode);
        }

    }
    else if (mode == 1){ //Use STL list
        listOfCars.push_back(tmpNode);
    }
    else{
        std::cout<< "invalid mode" << std::endl;
    }


    if (infile.eof()){
        break; // Not too nice but necessary because of last line problem
    }
}
infile.close();
}

void cardealership::readOutputFileAndAlterInventory(){
std::ifstream infile;
std::string line;
infile.open(ordersFile.c_str(), std::ifstream::in);
if (!infile.good()){
        std::cout << "Na na na Orders File" << "\n";
    }
int id;
char* model;
int amountNeeded;
std::ofstream log("errorLog.txt");
if (!log.good()){
    std::cout << "Na na na Log File" << "\n";
}

while (getline(infile, line)){
    getline(infile, line);

    std::stringstream helperStream;

    helperStream << line;
    helperStream >> id;
    helperStream >> model;
    helperStream >> amountNeeded;


    if (mode == 0) { //Use linked list
        linkedNode* tmpNode;
        linkedNode* previousNode;

        if (!linkedListOfCars.empty()){
            tmpNode = linkedListOfCars.front();

            while(tmpNode){
                if (tmpNode->data == model) {
                    std::cout<< "Model found!" << std::endl;
                }

                if (tmpNode->amountOfCars > amountNeeded){
                    std::cout<< "Enough cars available!" << std::endl;
                    tmpNode->amountOfCars -= amountNeeded;
                }

                if (tmpNode->amountOfCars <= amountNeeded){ //

                    if (previousNode != NULL) {
                        linkedNode tmp3Node = *previousNode;
                        tmp3Node.next = tmpNode->next;
                    }

                    linkedNode* tmp2Node;
                    tmp2Node = tmpNode->next;
                    tmpNode = NULL;
                    tmpNode = tmp2Node;

                    //write error to log
                    log << "ID: "<< id << ", Not enough items!";

                    previousNode = tmpNode;
                    tmpNode = tmpNode->next;


                }



            }
            if (!tmpNode) {
                std::cout<< "Model not found." << std::endl;
                //write error to log.
                log << "ID: "<< id << ", Model not available!";
            }
        }
        else{
            std::cout<< "No cars to sell." << std::endl;
        }

    }
    else if (mode == 1){ //Use STL list
        if (!listOfCars.empty()) {
            //Iterator copied and adapted from: http://www.cplusplus.com/reference/stl/list/begin/
            std::list<linkedNode*>::iterator it;

            for ( it=listOfCars.begin() ; it != listOfCars.end(); it++ ){
                linkedNode* tmpNode = *it;

                if (tmpNode->data == model) {
                    std::cout<< "Model found!" << std::endl;
                }

                if (tmpNode->amountOfCars >= amountNeeded){
                    std::cout<< "Enough cars available!" << std::endl;
                    tmpNode->amountOfCars -= amountNeeded;
                }
                if (tmpNode->amountOfCars < amountNeeded){
                    //delete entry and write error to log
                    it = listOfCars.erase(it);
                    log << "ID: "<< id << ", Not enough items!";


                }
                if (it++ == listOfCars.end()) {
                    //Write error to log if end of list is reached
                    log << "ID: "<< id << ", Model not available!";
                }

            }

        }
        else{
            std::cout<< "No cars to sell." << std::endl;
        }


    }
    else{
        std::cout<< "Invalid mode." << std::endl;
    }
}
infile.close();
}

void cardealership::printInventory (){
if (mode == 0) { //Use linked list
}
}

这篇文章现在对我来说似乎很长,但我希望我仍然可以得到一些帮助......

提前致谢,

大号

4

1 回答 1

1

添加新的linkedNode时,您声明一个指向节点的指针:

linkedNode* tmpNode;

下一次提到 tmpNode 如下:

tmpNode->data = model;

然而,tmpNode 不是一个linkedNode,它只是一个指向一个的指针。您基本上是在尝试将一些数据保存到程序的其他部分可能使用的空间中。您需要为 tmpNode 创建一个 linkedNode 以指向它,以便它拥有自己的存储空间。您可能需要查看关键字。

于 2012-09-28T13:57:41.537 回答