Linked list

Whats wrong with my code
plz tell me
it throws exception and stops


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
 // Linked List.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

struct book{
	string title;
	string author;
	book *next;
}*head;

void inBook()
{
	head = NULL;
}

void data(string t, string ath)
{
	book *newBook;
	book *tr = new book;
	tr = head;
	newBook = new book;
	newBook->title = t;
	newBook->author = ath;
	newBook->next = NULL;
	if (tr->next != NULL)
	{
		while (tr->next != NULL)
		{
			tr = tr->next;
		}
		tr = newBook;
	}
	else
	{
		tr = newBook;
	}
}

void printList()
{
	book *tr = head;
	while (tr->next != NULL)
	{
		cout << "Author of Book :" << tr->title << "is :" << tr->author << endl;
		tr = tr->next;
	}
}

int main(int argc, char*argv[])
{
	string bookName, AuthorName;
	char quit=' ';

	do
	{
		cout << "Enter data :\n";
		cin >> bookName >> AuthorName;
		data(bookName, AuthorName);
		cout << "Press q to quit :\n";
		cin >> quit;
	} while (quit != 'q');

	cout << "\nThis will print the linked list :";
	Sleep(2000);
	printList();
	return 0;
}
Here's the first issue I saw:
1
2
	book *tr = new book;
	tr = head;


You create a book and give the address to tr. Then you change tr's value to point at whatever head points at (which is nothing). So you created a book, then dropped it an picked up nothing.

Also note that inBook() is never called.
Thank you stewbond ...
Topic archived. No new replies allowed.