Thread: Why <complete program> doesn't works right?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    3

    Question Why <complete program> doesn't works right?

    Hi, I have this issue with my program, the insert function doesn't works right. It copyes itself to the other element and I don't know why . And I have rewrited the code so many times and can't figure out why it doesn't works right (im using linked-lists).

    Can you help me?
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    // The type used for every list of the menu
    struct type
    	{
    		char *name;
    		short index;
    		type *next;
    	};
    	
    class menu
    {
      private:
    	type *first;
      public:
    	menu (char *nam)		// Class constructor
       {
    	first = new type;
    	first->name = new char[strlen(nam)+1];
    	first->name = nam;
    	first->index = 1;
    	first->next = 0;
    	}
    	type* last(void);
    	void index_check(void);
    	int choose();
    	void insert(char *nam);
    	void del(int opt);
    	~menu(void);
    };
    
    type* menu::last()  //Returns the last argument
    {
    	type *aux = first;
    	while (aux->next != 0)
    		aux = aux->next;
    	return aux;
    }
    
    void menu::index_check(void)
    {
    	short cont = 0;
    	for (type *finder = first; finder != 0; finder = finder->next)
    	{
    		cont++;
    		finder->index = cont;
    	}
    }
    
    int menu::choose()		// Chooser (shows the menu, and asks the user for input)
    {
    	int counter = 0;
    	int option;
    	type *finder = first;
    	cout<<endl<<"\"Menu\""<<endl;
    	while(finder != 0)
    	{
    		cout<<(finder->index)<<" : "<<(finder->name)<<endl;
    		finder = finder->next;
    		counter++;
    	}
    	cout<<"Enter your choice: ";
    	cin>>int(option);
    	if (option > 0 && option <= counter)
    		return option;
    	else
    		return -1;
    }
    
    
    
    void menu::insert(char *nam)  // Inserter (inserts a new item to the end of the menu)
    {
    	type *finder;
    	if (first == 0)
    	{
    		first = new type;
    		first->name = new char[strlen(nam)+1];
    		first->name = nam;
    		first->index = 1;
    		first->next = 0;
    	}
    	else
    	{
    		finder = first->next;
    		if (finder == 0)
    		{
    			first -> next = new type;
    			first->name = new char[strlen(nam)+1];
    			finder = first->next;
    			finder->name = nam;
    			finder->next = 0;
    		}
    		else
    		{
    			while (finder->next != 0)
    			{
    				type *aux = finder->next;
    				finder = aux;
    			}
    			finder->next = new type;
    			first->name = new char[strlen(nam)+1];
    			finder->next->name = nam;
    			finder->next->next = 0;
    		}
    	}
    	index_check();
    }
    
    
    
    void menu::del(int opt)		//Deleter (deletes and option asked to the user. Note: uses the choose member)
    {
    	type *finder = first;
    	type *last = 0;
    	if (!opt)
    		int opt = choose();
    	while (finder != 0 && opt != -1)
    	{
    		if (finder->index == opt)
    		{
    			if(finder == first)
    			{
    				first = finder->next;
    				delete finder;
    				finder = 0;
    			}
    			else if (finder->next != 0)
    			{
    				last->next = finder->next;
    				delete finder;
    				finder = 0;
    			}
    			else
    			{
    				delete finder;
    				finder = 0;
    			}
    		}
    		else
    		{
    			last = finder;
    			finder = finder->next;
    		}
    	}
    	index_check();
    }
    
    
    menu::~menu(void)			//Class destroyer (deletes every entry)
    {
    	while (first != 0)
    	{
    		del(1);
    	}
    }
    
    
    
    
    //MAIN PROGRAM
    int main()
    {
    	char* temp;
    	temp = new char;
    	int opt, aux;
    	menu *user_menu;
    	
    	cout<<"Hi, this program will let you build a menu."<<endl;
    	cout<<"Please enter the first element of the menu:";
    	cin>>temp;
    	
    	user_menu = new menu(temp);
    	
    	cout<<"Ok, you have created your menu..."<<endl;
    	do
    	{
    		cout<<"1. Insert member?"<<endl;
    		cout<<"2. Delete member?"<<endl;
    		cout<<"3. Choose from the menu?"<<endl;
    		cout<<"4. Delete the whole menu and exit?"<<endl;
    		cin>>opt;
    		switch (opt)
    		{
    			case 1 :
    			{
    				cout<<"Type the name of the member to insert: ";
    				cin>>temp;
    				user_menu->insert(temp);
    				cout<<"Item inserted!!!"<<endl;
    				break;
    			}
    			case 2 :
    			{
    				user_menu->del(0);
    				break;
    			}
    			case 3 :
    			{
    				aux = user_menu->choose();
    				if (aux != -1)
    					cout<<"Good!! You selecter option: "<<aux<<endl;
    				else
    					cout<<"Sorry, that's not a valid option"<<endl;
    				break;
    			}
    			case 4 :  break;
    			default :
    			{
    				cout<<"That's not a valid option! :("<<endl;
    				opt = 1;
    				break;
    			}
    		}
    		
    	} while(opt > 0 && opt < 4);
    	
    	delete user_menu;
    	cout<<"Good bye!!"<<endl;
    	cin.get();
    }
    Thanks,

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> first->name = nam

    I don't know if this is your problem, but you cannot assign C style strings like this. You should be using C++ strings, which can be assigned that way. If you stick with the C style strings, you need to use strcpy to copy the name.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    thnx, I changed all those so I'm using strcpy. But still it doesn't works, It keeps changing all the element on the list.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    char* temp;
    temp = new char;
    cin>>temp;

    Try allocating more than one char for a name.
    Pick a better name than temp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anybody show me why this works??
    By tzuch in forum C Programming
    Replies: 2
    Last Post: 03-29-2008, 09:03 AM
  2. explanation of how this works
    By c++.prog.newbie in forum Linux Programming
    Replies: 3
    Last Post: 09-27-2004, 05:59 PM
  3. Works in MSVC++ 6.0 but not in Dev-C++
    By smitsky in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2004, 08:07 PM
  4. fprintf works in one function but not in the other.
    By smegly in forum C Programming
    Replies: 11
    Last Post: 05-25-2004, 03:30 PM