如何在 Visual Studio 的 C++ 中修复 abort()

问题描述 投票:0回答:0

我是 C++ 的新手,我如何修复我的代码以防止调用 abort()。我知道错误来自 void readCsvFile(),但我不知道如何修复它。

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

仅当我只使用 getline(ss, tmp, ','); id = stoi(tmp);,代码可以运行,但输出的其余部分将不正确,但是当我使用上面的完整代码时,将调用 abort() 。那我该如何解决

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


c++ visual-studio abort
© www.soinside.com 2019 - 2024. All rights reserved.