delete structure

Pages: 12
i'm a newbie in this structure function, string functions, and i have this project of mine due on tuesday, march 17. Our professor wants us to have a book information system with add, delete display and exit . how can i display the information that will be input?? how can it be save? and after it is saved, how can someone delete it?? i already finish the add choice.. please, i need assistance with this.. thank you very much in advance for someone who can help me.. thank you thank you
You'll need to write a structure or class to hold the data in.

You could use vectors to hold the different objects in (if you're already familair with them; if you're not I would avoid them): http://www.cplusplus.com/reference/stl/vector/.

Another way to do this is to determinate a max number of objects, lets say 100, and then create an array from your class with 100 elements. A variable "size" can be used to keep track of the number of objects. A simple function could be used to 'remove' an object by transporting all objects after that object one place forward. Overloading a operator= could be usefull.

[edit]
If you want to save information so it can be used in another run of the program, you'll need a file to store the data in: http://www.cplusplus.com/doc/tutorial/files.html
Last edited on
hi scipio.. could you make it simple?? :] pls??
If you have any specific questions, I would be happy to answer them :)
#include<iostream.h>
#include<conio.h>
#include<complex.h>
#include<process.h>
#include<stdio.h>
#include<string.h>
#include<dos.h>
int a,b,c;
char *ptr;

struct menu
{
char title[150];
char author[150];
int yr_pub;
int no_pg;
int acc_no;
}
s[1];
void main()
{
clrscr();
int opt;
cout<<" BOOK INFORMATION SYSTEM "<<endl;
cout<<endl<<">>>>> MENU<<<<< "<<endl;
cout<<endl<<"1. ADD"<<endl;
cout<<endl<<"2. DELETE"<<endl;
cout<<endl<<"3. DISPLAY"<<endl;
cout<<endl<<"4. EXIT"<<endl;
cout<<endl<<endl<<"OPTION:";
cin>>opt;

if(opt==1){
clrscr();
cout<<" ADD BOOK "<<endl;
cout<<"How many books you want to add?:";
cin>>a;

for(b=0; b<a;b++)
{
cout<<endl;
cout<<"TITLE OF THE BOOK:";
gets(s[a].title);
cout<<"AUTHOR:";
gets(s[a].author);
cout<<"YEAR BPUBLISH:";
cin>>s[a].yr_pub;
cout<<"NUMBER OF PAGES:";
cin>>s[a].no_pg;
cout<<"ACCOUNT NUMBER:";
cin>>s[a].acc_no;
}}
else if(opt==2)
{
cout<<"DELETE BOOK";
cout<<"TITLE OF THE BOOK YOU WANT TO DELETE:";
cin>>c;
ptr=strstr(c,c);



getch();
}


this are my codes so far..:]
how can i use the pointer here for the delete choice?
Last edited on
You can use a struct to hold the data in:

1
2
3
4
5
struct Book
{
    string Title;
    string Author;
}


Then you can create an array to hold the books in (let's say up to 100), and a variable to keep track of the number of books:

1
2
Book book[100];
int size = 0;


Now, adding a book is easy (add 1 to size and store the data into object book[size-1]). To remove an object, you could write a function. Let's say we have three books "a", "b" and "c", and the second book is removed. Now all the data of the objects after the second object, need to be moved one place forward (this can be done with a for-loop), so we'll get "a" and "c" in the first two objects. And of course you should substract one to size.

what i'm trying to ask is, after inputting some data at the add choice, it will be stored. (where will the data be stored?) then after that, for example, i want to display the data that are stored, it should get the data from what is stored from the add choice. and after that i want to delete an item there, after choosing delete, a new screen will appear then ask the title of the book that wants to be deleted. How will the program find that book? argh.. it's driving me crazy.. T.T pls.. thank you thank you
All the data is stored into our array of the structure book, like I showed in my previous post.

You do understand structures, right? You can acces the information without problems to, for example, print it to the screen. http://www.cplusplus.com/doc/tutorial/structures.html

You can find a specific title by using a for-loop to go through all objects until you found a book with that title.

Do you need to save the data to? So you can acces them after you have quit the program and restart it?
yes.. i need to store it. but how will i make a for loop then? in what part in my code should i put the for loop for looking for the book.. :] sorry for being silly.. >.<
Well, in pseudocode:


Delete book:
Ask for title of book that should be deleted
Go through all books to find title (Here you should use a for-loop)
Remove Book
ahmm., i'm really sorry, but how will i put the for loop??:D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Book book[100]; //array of the structure I showed before
int size = 0; //integer to keep track of the number of books

... //add books

string title; //string to store the title of the book that should be removed in

cout<<"Enter the title of the book you want to delete:\n";
getline(cin,title); //get title of book

//go through all books and find the book with the tilte
for (int i=0;i<size;i++)
{
    if (book[i].Title == title) //if this is the book
    {
        //go through all books and place them one place back
        for (int j=i;j<size-1;j++)
        {
            book[j].Title = book[j+1].Title;
            book[j].Author = book[j+1].Author;
        }
        size--; //one book is deleted; substract one from size
        break; //break out the loop
    }
}
thank you very very much,, but what if i'm using the old fashioned c++?? i'm with the blue scren, and the directives has like #include<iostream.h)
sory sory(gomenasai)>.<
Last edited on
Well, it will probably work the same. But I recommend you to use the new version of the headers, and to download the newest version of your compiler. (I have never worked with the old headers, I don't know what differences there are)
thank you thank you very much, i'm currently editing my work right now.. can i tell you later what will be the result of my editing this time?? so that i can get your opinion?? thank you..:] :D
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<iostream.h>
#include<conio.h>
#include<complex.h>
#include<process.h>
#include<stdio.h>
#include<string.h>

Book_book[100];
int size=0;
int a,b,c;
char title;
struct book
{
 char title[150];
 char author[150];
 int yr_pub;
 int no_pg;
 int acc_no;
}
s[1];
void main()
{
clrscr();
int opt;
	cout<<" BOOK INFORMATION SYSTEM "<<endl;
	cout<<endl<<">>>>> MENU<<<<<           "<<endl;
	cout<<endl<<"1. ADD"<<endl;
	cout<<endl<<"2. DELETE"<<endl;
	cout<<endl<<"3. DISPLAY"<<endl;
	cout<<endl<<"4. EXIT"<<endl;
	cout<<endl<<endl<<"OPTION:";
	cin>>opt;

if(opt==1)
{

clrscr();
cout<<" ADD BOOK "<<endl;
cout<<"How many books you want to add?:";
cin>>a;


for(b=0; b<a;b++)
{
cout<<endl;
cout<<"TITLE OF THE BOOK:";
gets(s[a].title);
cout<<"AUTHOR:";
gets(s[a].author);
cout<<"YEAR BPUBLISH:";
cin>>s[a].yr_pub;
cout<<"NUMBER OF PAGES:";
cin>>s[a].no_pg;
cout<<"ACCOUNT NUMBER:";
cin>>s[a].acc_no;
}
}

if(opt==2)
{
clrscr();
cout<<"Enter the title of the book you want to delete:";
getline(cin,"title");
for(int i=0;i<size;i++)
{
if(s[1].title==title)
{
for(int j=1;j<size-1;j++)
{
s[j].Title=s[j+1].title;
s[j].Author=s[j+1].author;
}
size--;
break;
}
}
if(opt==3)
{
clrscr();
cout<<"DISPLAY BOOK: "<<endl;
cout<<"1.By Author"<<endl;
cout<<"2.List"<<endl;
cout<<"Display it by:";
cin>>c;
if(c==1)
{
for(b=0; b<c; b++)
clrscr();
cout<<"Enter Author: ";
gets(s[c].author);
clrscr();
cout<<"THERE ARE NO BOOKS WRITTEN BY "<<s[c].author<<" .";
}
if(c==2)
{
clrscr();
cout<<"Lists"<<endl;
}
}
if(opt==4)
{
clrscr();
cout<<"GOODBYE!!";
exit(0);

}


getch();
}
}

this is not right, right?? it won't run.. argh.. i think i can't make it.. T.T pls help me..
The declaration of struct Book (line 12) should be before the declaration of the array (line 8). s (line 20) is missing a data-type.

Probably not nessecary for your compiler, but official C++: add using namespace std; after the headers, make main of type integer and use the modern headers (<iostream>, not <iostream.h>), replace clrscr() with system("cls");, replace exit(0); with return 0;

I haven't looked at the logic behind your program, but this are the most obvious errors.
thank you.. :]
but when i changed exit(0); to return(0); my program got more errors than before.. by the way, what is the prototype for getline? isnt it sys/type.h or geline.h?? and their asking me for an Lvalue?? what's that?? sorry sorry..>.< and may i ask what will be the output of your code?? :]
Last edited on
excuse me, but.. how will i return to the main page. like for example, i finished adding books in the add page, how will i go back to the main page without terminating the program and run it again? pls pls help... >.<
Add a loop similar to this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()//Notice that main should be int, not void
{
    int opt;
    while (true)
    {
        cout<<" BOOK INFORMATION SYSTEM "<<endl;
        //...
        if(opt==4)
        {
            cout<<"GOODBYE!!";
            return 0;
        }
    }
}
It will stop only when the program returned 0
Last edited on
Pages: 12