URGENT=How to delete an character array?

my school project is to make a personal library.
it will show me 5 catergories,math,physics,chemistry and C++ and EXIT.
inside each catergories,it contains 5 books.
after the books is selected,it will remove from the list.
book will be count in the front menu and book titles being shown.
So far i have done all the instruction except remove the book from the list after being selected. T.T

char book[6][50]={
"1. C++ for beginners",
"2. Advanced C++",
"3. C++ for Engineers",
"4. Problem-based C++",
"5. C++ for Dummies",
"6. Return to Main Library Menu"
};

after i select book 1,
i wan to remove "1. C++ for beginners", from the list,anyone have any idea how to do tat?
it will helps me a lot,thankx.
HEY! What happened to "The Complete Idiot's Guide to C++"!?!

Jokes aside, the only sane way to do that while preserving your array is to delete everything in part you're deleting, and then get the next element, reassign it to the one you just deleted the contents of, reassign the next one as well, etc...

The only 100% sane way to do that is to use a vector or deque.
http://cplusplus.com/reference/stl/vector/
http://cplusplus.com/reference/stl/deque/

And can you use strings? If so, it's a better solution than a char[].
http://cplusplus.com/reference/string/string/

EDIT: We are of course assuming the standard library only.

-Albatross

Last edited on
Show some code. If you don't want to use vectors or not familiar with it. I suggest you assign null to the selected books then an if statement should do the rest.
erm i haven study vector yet,jus start study in college for 3 months ago.
tis is my code for the library


#include <cstdlib>
#include <iostream>

using namespace std;
void clear ( );
void lbt();
void menu();
void choose1();
void choose2();
void choose3();
void choose4();
void choose5();
int out();
int choose;
int num1=0, n1=1;
char name[50];
char bt[40][500];
char temp[40][100]={" "};

int main(int argc, char *argv[])
{
cout<<"Please Enter Your Name:";
cin.getline(name, 100);
menu();
system("cls");
system("PAUSE");
return EXIT_SUCCESS;
}

void menu()
{
system("cls");
cout<<"Welcome To My Personal Library"<<name<<endl;
cout<<"Please Select Book Classification:"<<endl;
cout<<"1. Mathematics"<<endl;
cout<<"2. Physics"<<endl;
cout<<"3. Chemistry"<<endl;
cout<<"4. C++"<<endl;
cout<<"5. Exit"<<endl;
cout<<"Total Books Chosen:"<<num1<<endl;
cout<<"Book Titles"<<endl;// another string have to put here
lbt();
cin>>choose;
switch(choose)
{
case 1:
{
choose1();
break;
}
case 2:
{
choose2();
break;
}
case 3:
{
choose3();
break;
}
case 4:
{
choose4();
break;
}
case 5:
{
out();
break;
}
default:
{
cout<<"Please Enter 1 to 5 only."<<endl;
break;
}
}
}
void choose1()
{
system("cls");
char cppbook1[1][30]={"1. Mathematics for beginners",};
char cppbook2[1][30]={"2. Advanced Mathematics "};
char cppbook3[1][30]={"3. Mathematics for Engineers"};
char cppbook4[1][30]={"4. Problem-based Mathematics"};
char cppbook5[1][30]={"5. Mathematics for Dummies"};
char cppbook6[1][40]={"6. Return to Main Library Menu"};
cout<<"Choose The C++ Books From The List Below:"<<endl<<endl;
cout <<cppbook1[0]<<endl;
cout <<cppbook2[0]<<endl;
cout <<cppbook3[0]<<endl;
cout <<cppbook4[0]<<endl;
cout <<cppbook5[0]<<endl;
cout <<cppbook6[0]<<endl;
cin>>choose;
switch(choose)
{
case 1:
{
system("cls");
strcpy (bt[n1], cppbook1[0]);
strcpy (cppbook1[1], temp[0]);
n1++;
num1++;
char *cppbook1=new char[0];
choose1();
delete [] cppbook1;
break;
}
case 2:
{
strcpy (bt[n1], cppbook2[0]);
strcpy (cppbook2[1], temp[0]);
n1++;
num1++;
choose1();
break;
}
case 3:
{
strcpy (bt[n1], cppbook3[0]);
strcpy (cppbook3[1], temp[0]);
n1++;
num1++;
choose1();
break;
}
case 4:
{
strcpy (bt[n1], cppbook4[0]);
strcpy (cppbook4[1], temp[0]);
n1++;
num1++;
choose1();
break;
}
case 5:
{
strcpy (bt[n1], cppbook5[0]);
strcpy (cppbook5[1], temp[0]);
n1++;
num1++;
choose1();
break;
}
case 6:
{
menu();
break;
}
default:
{
cout<<"Please Enter 1 to 6 only."<<endl;
choose1();
break;
}
}
}

void choose5()
{
system("cls");
}



int out()
{
return 0;
}

void lbt()
{
for(int y=0;y!=10;y++)
{
cout<<bt[y]<<endl;
}
}

i change some of my codes instead of wat i shown tis morning after trying and trying;
it still works the same;

it run well,except to delete the books being selected. how to use NULL and if in it?sorry..kinda blur~


BTW is it possible to use string in tis?thankx for the reply.
Last edited on
Please use [code] [/code] tags.

And try to properly write the words out.
Topic archived. No new replies allowed.