program will not stay in menu loop

This is a discussion on program will not stay in menu loop within the C++ Programming forums, part of the General Programming Boards category; Hello. I can't seem to figure out why my program is exiting when I try to insert into my linked ...

  1. #1
    Registered User StevenGarcia's Avatar
    Join Date
    Nov 2006
    Posts
    22

    program will not stay in menu loop

    Hello. I can't seem to figure out why my program is exiting when I try to insert into my linked list.
    I'm sure it's something i keep overlooking, but I need a second pair of eyes to help me out. Thanks for any insight.


    This is my test code.

    Code:
    #include <iostream>
    #include "list.h"
    
    using namespace std;
    
    void addinput(list &);
    void removeinput(list &);
    void displayinput(list &);
    
    int main()
    {
      int choice = 0;
      int mainloop = 0;
      list mylist;
    
      while(mainloop == 0)
      {
    	  //display GUI for user
    	  cout << "==============================*" << endl;
    	  cout << "WELCOME STEVEN                *" << endl;
          cout << "==============================*" << endl;
    	  cout << "PRESS 1 TO ADD AN ENTRY       *" << endl;
    	  cout << "PRESS 2 TO REMOVE AN ENTRY    *" << endl;
    	  cout << "PRESS 3 TO DISPLAY ENTRIES    *" << endl;
    	  cout << "PRESS 4 TO EXIT               *" << endl;
    	  cout << "===============================" << endl;
    
    	  cin >> choice;
    	  cin.get();
    
          switch(choice)
    	  {
    	  case 1: addinput(mylist);
    		  break;
    	  case 2: removeinput(mylist);
    		  break;
    	  case 3: displayinput(mylist);
    		  break;
    	  case 4: mainloop++;
    	  }  break;
      }
    
      return 0;
    
    }
    
    void addinput(list & mylist)
    {
    	int numadd = 0;
    
    	cout << "TYPE THE NUMBER YOU WISH TO ADD" << endl;
    
    	cin >> numadd;
    	cin.get();
    
    	mylist.add(numadd);
    
    	return;
    }
    
    void removeinput(list & mylist)
    {
    	int numremove = 0;
    
    	cout << "TYPE THE NUMBER YOU WISH TO REMOVE" << endl;
    
    	cin >> numremove;
    	cin.get();
    
    	mylist.remove(numremove);
    
    	return;
    }
    
    void displayinput(list & mylist)
    {
    	mylist.display();
    
    	return;
    
    }
    Here is my list code.
    Code:
    #include <iostream>
    
    using namespace std;
    
    #include "list.h"
    
    //=========================================================
    
    list::list()
    {
    	num_in_list = 0;
    	head = NULL;
    }
    
    //==========================================================
    
    list::~list()
    {
    	node * temp; //temporary pointer for deleting
    	
        //deletes vacation photo list
        while(head)
        {
    	   temp = head->next;
    	   delete head;
    	   head = temp;
        }
    
    }
    
    
    //==========================================================
      
    int list::add(int & numadd)
    {
        node * newNode;
    
    	newNode = new node;
    
    	newNode->item = numadd;
    
    	// insert at the front of the list
        newNode->next = head;
    
        // move the head of the list to the new node
        head = newNode;
    
    	++num_in_list;
    
        return 0;
    }
    
    //==========================================================
    
    int list::remove(int & numremove)
    {
    return 0;
    }
    
    //==========================================================
    
    int list::display()
    {
    	node * temp;      // our current node (position)
    
      // begin at the head of the list
      temp = head;
    
      // loop until done
      while(temp)
      {
        cout << temp->item << endl;
        temp = temp->next;
      }
    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Code:
    switch(choice)
    	  {
    	  case 1: addinput(mylist);
    		  break;
    	  case 2: removeinput(mylist);
    		  break;
    	  case 3: displayinput(mylist);
    		  break;
    	  case 4: mainloop++;
    	  }  break;
    Take a look you need the break inside the braces. A it is just skipping the loop.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    	  case 4: mainloop++;
    	  }  break;
    The break above is in the wrong place. It should be:
    Code:
    	  case 4: mainloop++;
    	          break;
    	  }

  4. #4
    Registered User StevenGarcia's Avatar
    Join Date
    Nov 2006
    Posts
    22
    Doh! Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C keep a loop going whilst program continues on
    By fortune2k in forum C Programming
    Replies: 6
    Last Post: 03-11-2009, 08:44 AM
  2. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  3. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  4. how to loop a program from the start
    By thestien in forum C++ Programming
    Replies: 13
    Last Post: 10-04-2006, 01:22 AM
  5. Help me in while loop with case menu
    By playboy1620 in forum C Programming
    Replies: 1
    Last Post: 02-20-2002, 10:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21