卡死胡同-无休止的循环,没有解决方案

cardealership with dead end - Endless loop and no solution

本文关键字:解决方案 循环 死胡同 无休止      更新时间:2023-10-16

我似乎不明白为什么我的代码不起作用。

我的程序是关于读取两个表示卡片等级的文本文件,并根据模式将输入放入链接列表或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
}
}

这篇帖子对我来说似乎很长,但我希望我还能得到一些帮助。。。

提前感谢

L

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

linkedNode* tmpNode;

tmpNode的下一个例子如下:

tmpNode->data = model;

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