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;
       }
  }
Member Avatar for r.stiltskin

Notice this statement near the beginning of menu():

if(select = 1)

As soon as your program gets to that point it assigns 1 to select, instead of testing to see if select equals 1. This overrides whatever selection you made from the menu. You have statements similar to that in a number of other places -- if(select = 3), if(select=4), and so on. You will have to replace all of those "=" with "==".

Also, in addStart(), you have

if(start_ptr == NULL)

which is fine, but what if start_ptr != NULL? You haven't finished that function, so it will only work when the list is empty.

I haven't looked any further than that, but if you fix those things it will solve at least some of your problems.

Firstly. You have no function call back to main after you have entered the data. So it probably thinks that the program ends there .

You have commented the menu function in add end and have not mentioned it in addstart.

Secondly.

to add the book to the start of the list is very easy. If the start_ptr is null .. Then you should just initialise start_ptr to the array.

If it is not equal to null. Then you should first point next to the value in which start_ptr is currently pointing to and then you can point start_ptr back to temp.

Please use a proper loop( a do-while should do in this case)
Please use switch case instead of if( it was made for situation like this)
Please do not use system() (it is unportable)
And as stilskin said: if(select = 1) and if(select == 1) has a hell lot of difference.
The former one is ALWAYS true but the latter one is true only if value of select is 1

I just decided to use a switch statement since i was causing such problems but i still have yet to fixed the problem of when i add a new node to the end, after entering it kills the program instead of returning to menu even after i put menu into it.

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

node *start_ptr = NULL;
node *current;


int main(int argc, char *argv[])
{
     char y, Y, n ,N;
     
    menu();


    system("PAUSE");
    return EXIT_SUCCESS;
}

void menu()
{
    int choice, select;
   
    system("cls");
     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;
     
     
     switch(choice)
     {
     case 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();
          }  
     }
     break;
     
     case 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();
              
            menu();
          }
          
          else if(select = 2)
          {
              deleteEnd();
              display(); 
              
           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      
          }  
            
     }
     break;
     
     case 3 :
     {
            display();
            
            system("pause");
                system("cls");
                menu();    
            
     }
     break;
     
     case 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();
          }
            
     }
     break;
     
     case 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();
          }       
     }
     break;
     
     case 6 :
     {
        cout << "Good Bye.";
     }
}
}

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

  }

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

void addStart()
{}
void addChoice()
{}
void addSort()
{}
void deleteChoice()
{}
void deleteObject()
{}
void sort()
{}
void search()
{}
Member Avatar for r.stiltskin

... i still have yet to fixed the problem of when i add a new node to the end, after entering it kills the program instead of returning to menu even after i put menu into it.

That's because you are never actually calling the function addEnd (even though you think that you are), because you still haven't fixed the errors that I pointed out in post #2.

Your code still says

switch(choice)
     {
     case 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();  
          }

No matter what you enter for "select" at the first menu, as soon as your program gets to that "if(select = 1)" it sets select = 1 and calls addStart().

So I'll say again, everyplace where you have "if(select = [something])", you have to change the "=" to "==".

After you fix that, you will actually be able to call the addStart() function.

By the way, what are you planning to do with the global "current" pointer, and the node's "prv" pointer; you never use either of those.

Also, instead of having just a start_ptr pointing to the first node, it would be much better programming style to define a data structure representing the list. In other words, define a "list" struct (just as you defined a "node" struct), and make the start and current pointers members of the list struct. Then, in your main function you would declare a list variable, i.e.:
list my_list;
and then access the nodes using my_list's node pointers, like so:
my_list -> start

Oh, and incidentally, (since you seem to be using C++) you could make all of those functions, addStart(), addEnd(), deleteStart(), deleteEnd(), etc, etc, member functions ("methods") of the list object, and you would then have an object-oriented program.

And please use a loop to get beck to the menu();
Don't call menu() inside menu() it is bad.
Also, please read the posts that we wrote. It surely takes time to type all those stuff.
Moreover, you are again using system("cls") please do not use system() its also very bad. It makes your program unportable. For instance, the program that you gave will give a shell prompt error on my operating system (Linux). Got it?
So DONT USE SYSTEM("CLS")

Member Avatar for r.stiltskin

And please use a loop to get beck to the menu();
Don't call menu() inside menu() it is bad.

Good point. When you put calls to menu() inside menu(), you are inadvertently making unnecessary recursive calls to the menu() function, meaning you will be loading multiple copies of the function in memory.

Instead, as siddhant3s says, you should put the menu in a while loop. To do this, at the beginning of menu(), define a "flag", for example: bool menu_flag = true; and set up the loop with while( menu_flag) { ...[everything in menu except the flag definition]...} and in case 6 add menu_flag = false; This way, the menu will continue to be re-displayed until you select case 6.

Moreover, you are again using system("cls") please do not use system() its also very bad. It makes your program unportable. For instance, the program that you gave will give a shell prompt error on my operating system (Linux). Got it?
So DONT USE SYSTEM("CLS")

Siddhant, your point about portability is valid, but the OP is probably not ready to deal with clearing the screen without using a system call. An alternative is for you to simply add alias cls='clear' to your .bashrc file (assuming you use bash).

>An alternative is for you to simply add alias cls='clear' to your .bashrc file (assuming you use bash).
Ofcourse I know that on Linux the command is clear. I was telling him what does portability actually means. I was encouraging him to write codes that work on multiple platform rather than stick to one.
A general working rule is : Don't use shell command until it is important.
Another thing to add is system() is very well portable, but the command it takes are not.
Read a perfect article on why system() is bad :http://www.webinmind.net/bpc.html#six
It is non-portable, It is unsecured, It is slow.

Member Avatar for r.stiltskin

All of that is true, but it is also very non-trivial for a beginning programmer to write portable code to clear any kind of screen on any kind of computer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.