list program, core dump error

Hello everybody, i really need a help, i was trying to create a db of books, with a class book and a list that contains the books, i did all the stuff needed but i can't figure out why the function i created to add a book to the list gives me an error, can you help me? Really thank you.

The function is "add_book", also the program is not finished yet, i need to add the last things to the main.
Here is the code:

#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>

using namespace std;

class book {

public:

char* titolo;
char* autore;
float prezzo;
long int data;

book();

}addedBook;

class node{

public:
book libro;
node* next;

node(){
next = NULL;
}
};

class lista {

node* head;
int counter;

public:
lista();
void add_book(long int year, float price, char* title, char* autor);
void books_fromFile();
node* search(float price,long int data);
bool removeBook(node* searched);
node* getlast();
node* nodeAtIndex(int index);
int show_books();
};

lista::lista(){

head = NULL;
counter = 0;
}

node* lista::getlast(){

node* curr = head;
while (curr->next != NULL){

curr = curr->next;
}

return curr;
}

node* lista::nodeAtIndex(int index){

node* curr = head;
for (int i = 0; i < index; i++){

if(curr == NULL) {
return NULL;
}
curr = curr->next;
}
return curr;
}

void lista::add_book(long int year, float price, char* title, char* autor){

addedBook.data = year;
addedBook.prezzo = price;
addedBook.autore = autor;
addedBook.titolo = title;

if (head == NULL){
head->libro = addedBook;
} else {
getlast()->libro = addedBook;
}

counter++;
}

node* lista::search(float price, long int year){

node* curr = head;

for (int i = 0; i < counter; i++){
if (curr == NULL){

return NULL;
} else {

if (curr->libro.prezzo == price){

if(curr->libro.data == year){

return curr;
} else {

curr = curr->next;
}

curr = curr->next;
}
}

}
}

bool lista::removeBook(node* searched){

node* toRemove;
node* curr = head;
node* prev;
node* next;
bool removed = false;
int index = 0;
toRemove = search(searched->libro.prezzo, searched->libro.data);

while (curr != NULL){

if (curr->libro.prezzo == searched->libro.prezzo && curr->libro.data == searched->libro.data){

next = curr->next;
prev = nodeAtIndex(index-1);
delete curr;
prev->next = next;
removed = true;
}

curr = curr->next;
index++;
}

return removed;
}

int lista::show_books(){

node* curr = head;

if (curr != NULL){
for(int i = 0; i < counter; i++){

cout << "Libro n. " << i << ": ";
cout << curr->libro.titolo << ", " << curr->libro.autore << ", " << curr->libro.data << ", " << curr->libro.prezzo << "." << endl;
}

} else {

cout << "Lista vuota!" << endl;

return 0;
}
}

book::book() {
prezzo = 0;
data = 0;
titolo = NULL;
autore = NULL;
}

int main(){

lista lst1;

int ch, exit = 0;
string c, a;
long int rYear, year;
float rPrice, price;
char title[30];
char autor[20];

while (1){

if (exit == 1) {
break;
}

cout << "Cosa vuoi fare? " << endl;
cout << "1. Aggiungere un libro;" << endl;
cout << "2. Rimuovere un libro;" << endl;
cout << "3. Vedere la lista di libri;" << endl;
cout << "4. Scrivere la lista su un file" << endl;
cout << "5. Esci." << endl;

cin >> ch;


switch(ch){

case (1):
cout << "Titolo (. per finire): ";
cin >> c;
for (int i = 0; i < c.size(); i++){
title[i] = c.c_str()[i];
}

cout << "Autore (. per finire): ";

cin >> a;
for (int i = 0; i < a.size(); i++){
autor[i] = a.c_str()[i];
}

cout << "Data: ";
cin >> year;

cout << "Prezzo: ";
cin >> price;


lst1.add_book(year, price, title, autor);
break;

case (2):

cout << "Quale libro vuoi rimuovere? Scrivi data e prezzo: ";
cin >> rYear;
cin >> rPrice;
lst1.removeBook(lst1.search(rPrice, rYear));
break;

case (3):

lst1.show_books();
break;

case (4):

break;

case (5):

exit = 1;
break;
}
}
}


P.S.: Sorry for the format, i m trying to make it showed like a code, but it doesnt work.
Last edited on
i did all the stuff needed but i can't figure out why the function i created to add a book to the list gives me an error, can you help me?


Generally, if you get an error, you want to describe it. If it's a compilation error, tender the actual message. If your get a segmentation fault, say you get a segmentation fault. Do not say: "This doesn't work. Help!"

You never allocate memory anywhere. That's what containers like a list do. They manage memory.

1
2
3
    if (head == NULL) {
        head->libro = addedBook;
    }


If head is NULL that means it doesn't point to a valid object. So, after you confirm that head doesn't point to a valid object, you want to access a member of the object? Seems a little backwards.

Hi, thanks for your time.

Sorry, i forgot to describe the error, my bad. It's a segmentation one, and you are right, that if is a bit weird, how can i rewrite it in the correct way? Sorry i'm learning C++ language at school and i am not so good yet.
> i m trying to make it showed like a code, but it doesnt work.
[code]code goes here[/code]

> how can i rewrite it in the correct way?
I would suggest to use a header cell, not a pointer, make your list circular and use a `last' pointer initialized to `head' address.
1
2
3
4
5
6
7
8
lista::lista(): last(&head){
   head->next = &head;
}

void lista::add_book (boook b){
   this->last->next = new node(b, this->last->next);
   this->last = this->last->next;
}


If you prefer to keep `head' as a pointer and end with NULL
1
2
3
4
5
6
void lista::add_book (boook b){
   if(head==NULL)
      head = new node(b);
   else
      this->get_last()->next = new node(b);
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class book
{

	public:
	char* titolo; //loading the gun
	char* autore;
	float prezzo;
	long int data;

	book ();

} addedBook;

			cout << "Titolo (. per finire): ";
			cin >> c;
			for (int i = 0; i < c.size (); i++)
			{
				title[i] = c.c_str ()[i]; //pointing at your foott
			}
you know about std::string, ¿why don't you use it?
Topic archived. No new replies allowed.