0

im fairly new to c++ how can i fix my code to prevent abort() being called. i know the error came from the void readCsvFile(), but i have no idea how to fix it.

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

int sizeofLinkedList = 0;
struct Car
{

    int id;
    string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
    int price;
    int year;
    int mileage;
    int doors;

    Car* nextAddress;
    Car* prevAddress;
} *head, * tail;




Car* CreateNewNnode(int id, string title, int price, int year, int mileage, string fuel_type, string transmission,
    string engine_size, int doors, string colour, string body_type, string url, string sale_date) //create a newnode - use for any insert
{


    Car* newnode = new Car;
    newnode->id = id;
    newnode->title = title;
    newnode->price = price;
    newnode->year = year;
    newnode->mileage = mileage;
    newnode->fuel_type = fuel_type;
    newnode->transmission = transmission;
    newnode->engine_size = engine_size;
    newnode->doors = doors;
    newnode->colour = colour;
    newnode->body_type = body_type;
    newnode->url = url;
    newnode->sale_date = sale_date;
    newnode->nextAddress = NULL;
    newnode->prevAddress = NULL;
    return newnode;
}



// Quick sort algorithm for linked list
Car* partition(Car* head, Car* end, Car** newHead, Car** newEnd)
{
    Car* pivot = end;
    Car* prev = NULL, * cur = head, * tail = pivot;

    while (cur != pivot)
    {

        if (cur->id < pivot->id)
        {
            if ((*newHead) == NULL)
                (*newHead) = cur;

            prev = cur;
            cur = cur->nextAddress;
        }
        else
        {
            if (prev)
                prev->nextAddress = cur->nextAddress;

            Car* tmp = cur->nextAddress;
            cur->nextAddress = NULL;
            tail->nextAddress = cur;
            tail = cur;
            cur = tmp;
        }
    }

    if ((*newHead) == NULL)
        (*newHead) = pivot;

    (*newEnd) = tail;

    return pivot;
}



Car* getTail(Car* cur)
{
    while (cur != NULL && cur->nextAddress != NULL)
        cur = cur->nextAddress;

    return cur;
}

Car* quickSortRecur(Car* head, Car* end)
{
    if (!head || head == end)
        return head;

    Car* newHead = NULL, * newEnd = NULL;
    Car* pivot = partition(head, end, &newHead, &newEnd);

    if (newHead != pivot)
    {
        Car* tmp = newHead;
        while (tmp->nextAddress != pivot)
            tmp = tmp->nextAddress;

        tmp->nextAddress = NULL;
        newHead = quickSortRecur(newHead, tmp);
        tmp = getTail(newHead);
        tmp->nextAddress = pivot;
    }

    pivot->nextAddress = quickSortRecur(pivot->nextAddress, newEnd);

    return newHead;
}

void quickSort(Car** headRef)
{
    (*headRef) = quickSortRecur(*headRef, getTail(*headRef));
}

// Modified insertIntoASortedList to use quicksort
void insertIntoASortedList(Car* newnode)
{
    // Insert new node at the end of the list
    if (head == NULL)
    {
        head = tail = newnode;
    }
    else
    {
        tail->nextAddress = newnode;
        newnode->prevAddress = tail;
        tail = newnode;
    }

    // Sort the list using quicksort
    quickSort(&head);
}

void readCsvFile() {
    ifstream file("carlist(1).csv");
    string line;
    int id;
    string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
    int price = 0;
    int year;
    int mileage;
    int doors;

    while (getline(file, line)) {
        stringstream ss(line);
        string tmp;
        int id;
        string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
        int price;
        int year;
        int mileage;
        int doors;
        


        getline(ss, tmp, ',');
        id = stoi(tmp);

        getline(ss, title, ',');

        getline(ss, tmp, ',');
        price = stoi(tmp);

        getline(ss, tmp, ',');
        year = stoi(tmp);

        getline(ss, tmp, ',');
        mileage = stoi(tmp);

        getline(ss, fuel_type, ',');
        getline(ss, transmission, ',');
        getline(ss, engine_size, ',');
        getline(ss, tmp, ',');
        doors = stoi(tmp);
        getline(ss, colour, ',');
        getline(ss, body_type, ',');
        getline(ss, url, ',');
        getline(ss, sale_date, ',');


        Car* newnode = CreateNewNnode(id, title, price, year, mileage, fuel_type, transmission,
            engine_size, doors, colour, body_type, url, sale_date);
        insertIntoASortedList(newnode);
    }
}




void SearchBasedOnPrice(int price1, int price2)
{
    Car* current = head;

    while (current != NULL)
    {
        if (current->price >= price1 && current->price <= price2)
        {
            cout << current->id << ". " << current->title << " - " << current->price << " - "
                << current->year << " - " << current->mileage << " - " << current->fuel_type << " - "
                << current->transmission << " - " << current->engine_size << " - " << current->doors << " - "
                << current->colour << " - " << current->body_type << " - " << current->url << " - "
                << current->sale_date << endl;
        }
        current = current->nextAddress;
    }
    cout << "List ended here!" << endl;
}



int main()
{
    head = NULL;

    srand(time(0));
    int noOfcar, choice = 1;
    int CarID, Year;
    string Brand, Type, color;
    int p1;
    int p2;

    cout << "Enter your searching p1: ";
    cin >> p1;
    cout << "Enter your searching p2: ";

    cin >> p2;
    readCsvFile();
    SearchBasedOnPrice(p1, p2);


    cout << endl;
    int answer; string word;
    cout << "Do you want to search anything from the list or not? 1 - Yes, 0 - No: ";
    cin >> answer;
    cin.ignore();


    while (answer == 1)
    {


        cout << "Enter your searching p1: ";
        cin >> p1;
        cout << "Enter your searching p2: ";

        cin >> p2;
        readCsvFile();
        SearchBasedOnPrice(p1, p2);

        cout << "Do you want to edit anything? 1 - Yes, 0 - No: ";
        cin >> answer;

        int CarID;
        if (answer == 1)
        {
            cout << "Enter your car ID: ";
            cin >> CarID;
            
        }

        cout << "Do you still want to search anything from the list or not? 1 - Yes, 0 - No: ";
        cin >> answer;
        cin.ignore();
        system("pause");
        system("cls");
    }
    return 0;
}

only when i only use getline(ss, tmp, ','); id = stoi(tmp);, the code can be run, but the rest of the output will be incorrect, but when i use the full code above, the abort() will be called. so how can i fix it

void readCsvFile() {
    ifstream file("carlist(1).csv");
    string line;
    int id;
    string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
    int price = 0;
    int year;
    int mileage;
    int doors;

    while (getline(file, line)) {
        stringstream ss(line);
        string tmp;
        int id;
        string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
        int price;
        int year;
        int mileage;
        int doors;
        


        getline(ss, tmp, ',');
        id = stoi(tmp);

        getline(ss, title, ',');

        ss >> price;

        ss >> year;
        getline(ss, tmp, ',');
        mileage = stoi(tmp);
        getline(ss, fuel_type, ',');
        getline(ss, transmission, ',');
        getline(ss, engine_size, ',');
        ss >> doors;
        getline(ss, colour, ',');
        getline(ss, body_type, ',');
        getline(ss, url, ',');
        getline(ss, sale_date, ',');


        Car* newnode = CreateNewNnode(id, title, price, year, mileage, fuel_type, transmission,
            engine_size, doors, colour, body_type, url, sale_date);
        insertIntoASortedList(newnode);
    }
}
void readCsvFile() {
    ifstream file("carlist(1).csv");
    string line;
    int id;
    string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
    int price = 0;
    int year;
    int mileage;
    int doors;

    while (getline(file, line)) {
        stringstream ss(line);
        string tmp;
        int id;
        string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
        int price;
        int year;
        int mileage;
        int doors;
        


        getline(ss, tmp, ',');
        id = stoi(tmp);

        getline(ss, title, ',');

        getline(ss, tmp, ',');
        price = stoi(tmp);

        getline(ss, tmp, ',');
        year = stoi(tmp);

        getline(ss, tmp, ',');
        mileage = stoi(tmp);

        getline(ss, fuel_type, ',');
        getline(ss, transmission, ',');
        getline(ss, engine_size, ',');
        getline(ss, tmp, ',');
        doors = stoi(tmp);
        getline(ss, colour, ',');
        getline(ss, body_type, ',');
        getline(ss, url, ',');
        getline(ss, sale_date, ',');


        Car* newnode = CreateNewNnode(id, title, price, year, mileage, fuel_type, transmission,
            engine_size, doors, colour, body_type, url, sale_date);
        insertIntoASortedList(newnode);
    }
}


  • 2
    Start by removing just about all your code. Then add bit by bit, building with extra warnings enabled and treated as errors, and testing each bit before adding the next bit of code. Then when you get the crash, you try to isolate it into a [mre] and use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to catch the crash to see when and where it happens in your code, and to examine variables and their values to make sure all of them are correct. – Some programmer dude Feb 19 '23 at 10:23
  • If only catching the crash in the [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) is not enough, then use it to step through the code line by line while monitoring variables and their values. At the same time use pen and paper to draw all the lists and the pointers you manipulate in the code, to visualize what's going on. Erase and redraw arrows (pointers) and boxes (nodes) and you work with them in the code. That should help you see what's going on and what's happening. – Some programmer dude Feb 19 '23 at 10:25
  • On a different note, whatever resource you're using to learn C++ I would argue it teaches you more C than C++. With C++ you should have separate classes for the list, the nodes and the data in the node. And use member functions of the classes to manipulate the objects. Global variables and functions are likely not needed at all for your assignment. – Some programmer dude Feb 19 '23 at 10:27
  • Note that [stoi](https://en.cppreference.com/w/cpp/string/basic_string/stol) throws an exception at the slightest error in its parameter. So any error in the test file, or in one of the `getline`s will get you an uncaught exception. That is *one way* of triggering an abort() call. – BoP Feb 19 '23 at 10:57
  • When it `aborts` break into the Visual Studio debugger. Then switch the "Stack Frame" on the Debug toolbar of Visual Studio to your code. – drescherjm Feb 19 '23 at 13:31
  • @drescherjm it bring me to the code ``` if (_Errno_ref == ERANGE) { _Xout_of_range("stoi argument out of range"); } ```, but i have no idea what that mean – Myste Myzte Feb 19 '23 at 13:58
  • You did not switch the "Stack Frame" to your code and look at the variables. – drescherjm Feb 19 '23 at 14:03
  • 1
    What it likely means is one of the `getline(ss, tmp, ',');` did not read something that is convertible to an int. – drescherjm Feb 19 '23 at 14:05
  • @drescherjm thanks man i finally found whats the problem from your advice, appreciate your help my g – Myste Myzte Feb 19 '23 at 14:42

0 Answers0