Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8601767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:55:13+00:00 2026-06-12T01:55:13+00:00

I just can’t seem to figure out why my code doesn’t work. My program

  • 0

I just can’t seem to figure out why my code doesn’t work.

My program is about reading two text files representing a cardealership, and putting the input into a linked list or an STL list depending on mode. Then the orders are read and depending on the availability an error log is created.
The thng is that it keeps on looping in the linked list mode and that is doesn’t seem to write the error log.

I feel pretty stupid about this. I don’t want you guys to solve the error but teach me how to do it. I’m thankful for any review of my code by more experienced people.
I’ve tried debugging in Eclipse in XCode and in VS2012(VM with Win8). In none of the IDEs the variables are shown in the debug editor which I just don’t understand. I use the MacOSX GCC Compiler.

So here are the txt files:
input.txt

Brera 3
Golf 5
Punto 13
Fiesta 19

and orders.txt

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

this is my main method:

// 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;
}

Here are the header of the class containing the actual lists:

// 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

and the footer:

// 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
}
}

The post seems pretty long to me now but I hope I can still get some help…

Thanks in advance,

L

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:55:14+00:00Added an answer on June 12, 2026 at 1:55 am

    When adding a new linkedNode you declare a pointer to a node:

    linkedNode* tmpNode;
    

    The next mention of tmpNode is the following:

    tmpNode->data = model;
    

    However, tmpNode isn’t a linkedNode, it’s just a pointer to one. You’re basically trying to save some data into space that other parts of your program may use. You need to make a linkedNode for tmpNode to point to so that it has its own storage. You may want to look into the new keyword.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just can't seem to figure out why my code is not working at
I just can't seem to figure this out. I've tried text-align: center; , display:
I just can't figure out, why this slidetoggle does't work. It just runs once:
I just can't seem to figure out how to make efficient and clean looking
I just can't seem to figure this one out... As far as I know,
Just can't seem to figure this out, how to prevent the outer loop continuing
I just can't seem to figure this out - I'm trying to get the
I just can not figure out the following code. int d = 5; float
I just can't seem to figure this out. I found some similar Questions here
I just can't figure out or find any solution here, so I chose to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.