My code is supposed to accept into for book data and sort it.
My problem is when i enter Data it doesnt return to the menu. i know atm it is // but it seems to fail after i enter the data. Also i'm having problem trying to add the new book to the start of the list.
#include <cstdlib>
#include <iostream>
using namespace std;
void addEnd();
void addStart();
void addChoice();
void addSort();
void deleteStart();
void deleteEnd();
void deleteChoice();
void deleteObject();
void sort();
void search();
void display();
void menu();
struct node
{
char title[40]; //title name allowed u pto 40
char author[40]; // author name allowed up to 40
double price;
node *nxt;
};
node *start_ptr = NULL;
node *current;
int main(int argc, char *argv[])
{
menu();
system("PAUSE");
return EXIT_SUCCESS;
}
void menu()
{
int choice, select;
char y, Y, n ,N;
cout << "Welcome" << endl;
cout << "1. Enter a new book." << endl;
cout << "2. Delete a current book." << endl;
cout << "3. Display current books." << endl;
cout << "4. Search for a book." << endl;
cout << "5. Sort books." << endl;
cout << "6. Exit. " << endl;
cin >> choice;
if(choice == 1)
{
cout << "Where would you like to enter the new book." << endl;
cout << "1. Start." << endl;
cout << "2. End." << endl;
cout << "3. Choose a location to enter." << endl;
cout << "4. Enter and sort by name." << endl;
cout << "5. Return to main menu." << endl;
cin >> select;
if(select = 1)
{
system("cls");
addStart();
}
else if(select = 2)
{
system("cls");
addEnd();
}
else if(select = 3)
{
system("cls");
}
else if(select = 4)
{
system("cls");
// call add
//call sort
}
else
{
system("cls");
menu();
}
}
else if(choice == 2)
{
cout << "Where would you like to delete a book." << endl;
cout << "1. Start." << endl;
cout << "2. End." << endl;
cout << "3. Choose a location to delete." << endl;
cout << "4. Delete a specific object." << endl;
cout << "5. Return to main menu." << endl;
cin >> select;
if(select = 1)
{
system("cls");
deleteStart();
display();
cout << "Return to main menu(y or n)?" << endl;
cin >> select;
if(select == y || select == Y)
{
system("cls");
menu();
}
else
{
system("cls");
//exit;
}
}
else if(select == 2)
{
deleteEnd();
display();
cout << "Return to main menu(y or n)?" << endl;
cin >> select;
if(select == y || select == Y)
{
system("cls");
menu();
}
}
else if(select = 3)
{
system("cls");
// call display -> ask where to delete
}
else if(select = 4)
{
system("cls");
//call display
//call temp slot
//display temp slot data
}
else
{
system("cls");
//call menu
}
}
else if(choice == 3)
{
display();
system("pause");
system("cls");
menu();
}
else if(choice == 4)
{
cout << "How would you like search for a book?" << endl;
cout << "1. Author name." << endl;
cout << "2. Title." << endl;
cout << "3. Price." << endl;
cout << "4. Return to main menu." << endl;
cin >> select;
if(select = 1)
{
system("cls");
}
else if(select = 2)
{
system("cls");
}
else if(select = 3)
{
system("cls");
}
else
{
system("cls");
menu();
}
}
else if(choice == 5)
{
cout << "How would you like sort the books?" << endl;
cout << "1. Author name(A-Z)." << endl;
cout << "2. Title(A-Z)." << endl;
cout << "3. Price(greatest to least)." << endl;
cout << "4. Return to main menu." << endl;
cin >> select;
if(select == 1)
{
system("cls");
}
else if(select == 2)
{
system("cls");
}
else if(select == 3)
{
system("cls");
}
else
{
system("cls");
menu();
}
}
else
{
//exit;
}
}
void addEnd ()
{
node *temp, *temp2; // Temporary pointers
// Reserve space for new node and fill it with data
temp = new node;
cout << "Please enter the Title of the book: ";
cin >> temp->title;
cout << "Please enter the Author of the book: ";
cin >> temp->author;
cout << "Please enter the price of the book: ";
cin >> temp->price;
temp->nxt = NULL;
// Set up link to this node
if (start_ptr == NULL)
{
start_ptr = temp;
current = start_ptr;
}
else
{
temp2 = start_ptr;
while (temp2->nxt != NULL)
{
temp2 = temp2->nxt;
}
temp2->nxt = temp;
}
//menu();
}
void addStart()
{
node *temp, *temp2;
temp = new node;
cout << "Please enter the title of the book: ";
cin >> temp->title;
cout << "Please enter the author of the book : ";
cin >> temp->author;
cout << "Please enter the price of the book :$ ";
cin >> temp->price;
if(start_ptr == NULL)
{
start_ptr = temp;
}
}
void deleteEnd()
{
node *temp1, *temp2;
if (start_ptr == NULL)
{
cout << "The list is empty!" << endl;
}
else
{
temp1 = start_ptr;
if (temp1->nxt == NULL)
{
delete temp1;
start_ptr = NULL;
}
else
{
while (temp1->nxt != NULL)
{
temp2 = temp1;
temp1 = temp1->nxt;
}
delete temp1;
temp2->nxt = NULL;
}
}
}
void deleteStart()
{
node *temp;
temp = start_ptr;
start_ptr = start_ptr->nxt;
delete temp;
}
void display()
{
node *temp;
temp = start_ptr;
cout << endl;
if (temp == NULL)
{
cout << "Empty space!" << endl;
}s
else
{ while (temp != NULL)
{ // Display details for what temp points to
cout << "Title : " << temp->title << " ";
cout << "Author : " << temp->author << " ";
cout << "Price :$ " << temp->price;
if (temp == current)
{
cout << " <-- Current node";
cout << endl;
temp = temp->nxt;
}
}
cout << "End of list!" << endl;
}
}