0

I am trying to debug a program and not quite understanding what is happening. After filling the book array from user input, I am trying to order an array of pointers to book objects in alphabetical order (by book name). Running the program causes a seg fault, and the debugger is showing me that when my orderAlphabetically function is called, the pointer array (which up until then was perfectly fine), only contains three elements. Any ideas why this may be?

Thanks!

#include <iostream>
#define MAXBKS 20
using namespace std;

typedef struct book{

    float price;
    string name;
    string author;

}book;


// addBooks():  Gather user input and add book structs to array.
// @Params: the main library array, and its size.
// 
void addBooks(book library[], int size){

    string name, author, price;

    for(int i = 0; i< size; ++i){

        cout<< "Enter the name of the book, author, and price, each separated by \"~\": "; 

        if (cin.peek() == '\n') break;

        getline(cin, name, '~');    
        getline(cin, author, '~');  
        getline(cin, price, '~');   

        library[i].name = name;
        library[i].author = name;
        library[i].price = stod(price);

        cin.get();
    }
}


// orderAlphabetically():   assign pointers to alphabetically ordered book 
//                          titles. 
// @Params: the main library array, and the pointer array to hold the order
// 
void orderAlphabetically(book *library_ptrs[], int size){

    int pos;
    book *temp;

    for (int i = size; i > 1; i--){
        pos = 0;
        for (int j = 0; j < i; ++j){
            if ((library_ptrs[i]->name).compare(library_ptrs[pos]->name) > 0 )
                pos = j;
        }

        temp = library_ptrs[pos];
        library_ptrs[pos] = library_ptrs[i-1];
        library_ptrs[i-1] = temp;
    }
}

void printLibrary(book library[], int size){

    int i = 0;

    while ((library[i].price != 0) && (i < size)){

        cout<< "\nBook "<< i+1<< ": "<< endl;
        cout<< library[i].name<< endl;
        cout<< library[i].author<< endl;
        cout<< library[i].price<< endl;

        cout<<endl;
        ++i;
    }

}


int main(){
    book library[MAXBKS] = {0};
    book *library_ptrs[MAXBKS] = {0};

    // Add books to the library until the user enters 'enter'
    addBooks(library, MAXBKS);

    // Print entered books
    cout<< "\nThese are the books you have added: "<< endl;
    printLibrary(library, MAXBKS);

    // Order Alphabetically
    for (int i = 0; i < MAXBKS; ++i)
        library_ptrs[i] = library + i;

    orderAlphabetically(library_ptrs, MAXBKS); 

    return 0;
}

Edit: fixed for loop

1 Answer 1

1
while ((library[i].price != 0) && (i < size)) // i <= size is wrong


if ((library_ptrs[i]->name).compare(library_ptrs[pos]->name) > 0) // i was initialized to size, so similar problem, out of bound.

Please take a look at std::sort() instead of rolling your own sorting function.

Sign up to request clarification or add additional context in comments.

2 Comments

why is that? I have that there in case not all 20 elements of the book array have been filled. And that part of the program runs fine...
C/C++ array index starts at 0, so when i == size(in your case 20) library[20] will access the 21st element in the array, which is out of bound.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.