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,