汽车经销商与死胡同 - 无尽的循环,没有解决方案

时间:2012-09-28 13:50:23

标签: c++ list

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

我的程序是读取两个代表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
}
}

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

提前致谢,

1 个答案:

答案 0 :(得分:1)

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

linkedNode* tmpNode;

下一次提到tmpNode如下:

tmpNode->data = model;

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