Library System problem

hey everyone i have a problem, and it's very urgent here is the code:

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include<conio.h>
#include<iostream>
#include<fstream>
#include<string>
#include<windows.h>

using namespace std;
int status[20];
int sum;

void gotoxy (short row, short col)


{
	COORD coord = {row,col};
	SetConsoleCursorPosition ( GetStdHandle ( STD_OUTPUT_HANDLE ), coord );
}

struct book {
	string bname;
	string aname;
	string bnumber;
};

char c;
int a,b,d;
book p[300];
char character;

int main ()
{
	
	cout<< "\n\n\nWelcome to Library Books Management System\n\n\n";
	system ("pause");
	system ("cls");
mainmenu:
	cout<<"\n--------------------------------------------------------------------------------"<<endl;
	gotoxy (31,2);cout<<"Library Book System"<<endl;
	cout<<"--------------------------------------------------------------------------------"<<endl;
	cout<<"\n[1] Add book\n[2] Borrow book\n[3] Check available books\n[4] Quit";
	cout<<"\n\nSelect Transaction: ";
	cin>>d;
	system ("cls");
	book p[200];
	switch (d) {
	case 1: {
		fstream si;
		si.open("si.txt");
		char buffer [225];
		ifstream fin("si.txt");
		if(fin)
		{
			char ff;
			while(fin.get(ff))
				cout<<ff;
		}
		fin.close();
		ofstream fout("si.txt",ios::app);
		if (!fout)
				{
					cout<<"unable to open for appending.\n";
					return (1);
				}
				cout<<"\n--------"<<endl;
				cout<<"ADD BOOK";
				cout<<"\n--------"<<endl;
				cout<<"How many books will you add?: "<<endl;
				cin>>a;
				for (b=0;b<a;b++){
					cout<<"Enter book name: ";
					do{
						getline (cin,p[b].bname);
					}
					while
						(p[b].bname =="");
					cout<<"Enter author name: ";
					do{
						getline (cin,p[b].aname);
					}
					while
						(p[b].aname =="");
					cout<<"Enter book number: ";
					cin>>p[b].bnumber;
				}
				cout<<"You added "<<a<<" books";
				for (b=0;b<a;b++){
					fout<<endl;
					fout<<endl;
					fout<<endl;
					fout<<"Book name: "<<p[b].bname<<endl;
					fout<<"Author name: "<<p[b].aname<<endl;
					fout<<"Book number: "<<p[b].bnumber<<endl;

				}
				cin.ignore(0,'\n');
				cin.getline(buffer,225);
				fout<<buffer<<"\n";
				fout.close();
				fin.open("si.txt");
				if (!fin)
				{
					cout<<"Unable to open for reading.\n";
					return (1);
				}
				char ff;
				while (fin.get(ff))
					cout<<ff;
				fin.close();
				
				cout<<"\n\nDo you want another transaction? (Y/N) ";
				cin>>character;
				if (character=='y' || character=='Y') {
					system ("cls");
					goto mainmenu; }
				else if (character=='n'||character=='N')
					break;
				else if(character!='y'||character!='Y'||character!='n'||character!='N')
				{
					cout<<"pls..."<<endl<<"just answer yes or no..";
					system("pause");
					goto b;
				}
				else
					exit (0);
				
			
			}
	case 2: goto b;
b:
			{
				ofstream brrowd;
				cout<<"\n--------"<<endl;
				cout<<"BORROW BOOK";
				cout<<"\n--------"<<endl;
				sum=status[0]+status[1]+status[2]+status[3]+status[4]+status[5]+status[6]+status[7]+status[8]+status[9]+status[10]+status[11]+status[12]+status[13]+status[14]+status[15]+status[16]+status[17]+status[18]+status[19];
				brrowd.open("brrowd.txt");
				if (sum==20)
				{
					cout<<"All the books were borrowed"<<endl;
					brrowd<<"All the books were borrowed"<<endl;
				}
				else ;
				cout<<"What would you like to borrow?"<<endl;
				brrowd<<"What would you like to borrow?"<<endl;
				for (b=0;b<20;b++)
				{
					cout<<"Book "<<b+1<<" ("<<p[b].bname<<"): ";
					
					if (status[b]==1){
						cout<<"Borrowed"<<endl;
						brrowd<<"Borrowed"<<endl;
						brrowd<<"Book "<<b+1<<": ";}
					else
						{cout<<"Available"<<endl;
						brrowd<<"Available"<<endl;
						brrowd.close();}
				}
				
				int booknumber;
				cout<<"Borrow book: ";
				cin>>booknumber;
				
			 if (status[booknumber-1]==1)
				{
					system("cls");
					cout<<"Someone has already borrowed that book. Please choose another book"<<endl;
					system("pause");
					system("cls");
					goto b;
				}
				else if
					(status[booknumber-1]==0){
				cout<<"thank you for borrowing!!";
				status[booknumber-1]=1;
				}
				
				
				cout<<"\n\nDo you want another transaction? (Y/N) ";
				cin>>c;
				if (c=='y' || c=='Y') {
					system ("cls");
					goto mainmenu; }
				else if(c!='y'||c!='Y'||c!='n'||c!='N')
				{
					cout<<"pls..."<<endl<<"just answer yes or no..";
					system("pause");
					goto b;
				}
				else
					exit (0);
			}

		case 3: {
			string line;
			ifstream si ("si.txt");
	if (si.is_open())
	{
		while (si.good())
		{
			getline(si,line);
			cout<<line<<endl;
		}
		si.close();
	}
	else 
		{
			cout<<"Unable to open file";
	}
	
			cout<<"\n\nDo you want another transaction? (Y/N) ";
				cin>>c;
				if (c=='y' || c=='Y') {
					system ("cls");
					goto mainmenu; }
				else
					exit (0);	
				
	break;
				}
			

	case 4:
		exit (0);
		}
		
	_getch ();
	return 0;
	}


what i need to do is in case 2, instead of Book 1-20 i need to display ng name of the book that was added previously (in case 1) and i should not display any books other the books added, (because how can you borrow a book if it doesn't exist in the library) hope you understand me :) here is an example:
instead of this...

Book1: Available
Book2: Available
Book3: Available
Book4: Available
Book5: Available
Book6: Available
Book7: Available
Book8: Available
Book9: Available
Book10: Available
Book11: Available
Book12: Available
Book13: Available
Book14: Available
Book15: Available
Book16: Available
Book17: Available
Book18: Available
Book19: Available
Book20: Available


it should look like this

[Book title here]: Available
or
Book1 ([Book Title here]): Available
BUMP! help please :(((
BUMP :(
closed account (S6k9GNh0)
Nobody wants to read 228 lines of rather obfuscated code. I read about 10 lines and I get mad with rage of the bad practice I see.

For better results, try giving a more precise area for us to look into the problem. You need to help us help you but don't expect us to baby you. That's simply too much effort.
Your code is horrendous.

_ Please indent it
_ Use meaningful names. Common, your most important variable is called 'p' (there is a global one that is not being used too)
_ Declare the things as you need. ¿why the hell 'a', 'b', 'd' are globals?
_ Don't use goto, the flow is hard to read. That is the culprit.
_ system("cls") system("pause") *sigh*, learn ncurses. Still, you should not worry so much about the interface, in fact that is making it worse.
_ Use functions. ¿Don't you see repeated code?

The thing is, the only time that you popullate your 'library' is when you add books. At the beggining of the program you should parse the 'si.txt' file.
But still, you may wonder: "if after adding the books the library does contain them, ¿why there is nothing when I try to borrow?"
The thing is that after you end the adding part you goto mainmenu;
1
2
3
mainmenu:
//...
	book p[200]; //declare the array 
So you are constructing it again.
If you move that declaration before the label, it should work. But please, don't do that.
the code contain many out dated syntex , logical error . wrong goto statment.
sorry about the code, actually this is not my code im just helping a friend, wait ill fix it

actually i have another set of codes here, i changed their code to have function but it has LNK2019 error
here is the code anyways.

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include<conio.h>
#include<iostream>
#include<fstream>
#include<string>
#include<windows.h>

using namespace std;
int status[20];
int sum;

void gotoxy (short row, short col)


{
	COORD coord = {row,col};
	SetConsoleCursorPosition ( GetStdHandle ( STD_OUTPUT_HANDLE ), coord );
}

struct book {
	string bname;
	string aname;
	string bnumber;
};

char c;
	int d;

char case1();
char case2();
char case3();
char mainmenu();

int main ()
{
	
	cout<< "\n\n\nWelcome to Library Books Management System\n\n\n";
	system ("pause");
	system ("cls");
	mainmenu();
	


	char case1();{
		{
		fstream si;
		si.open("si.txt");
		char buffer [225];
		ifstream fin("si.txt");
		if(fin)
		{
			char ff;
			while(fin.get(ff))
				cout<<ff;
		}
		fin.close();
		ofstream fout("si.txt",ios::app);
		if (!fout)
				{
					cout<<"unable to open for appending.\n";
					return (1);
				}
				int a,b,c=0;
				cout<<"\n--------"<<endl;
				cout<<"ADD BOOK";
				cout<<"\n--------"<<endl;
				cout<<"How many books will you add?: "<<endl;
				cin>>a;
				book p[300];
				for (b=0;b<a;b++){
					cout<<"Enter book name: ";
					do{
						getline (cin,p[b].bname);
					}
					while
						(p[b].bname =="");
					cout<<"Enter author name: ";
					do{
						getline (cin,p[b].aname);
					}
					while
						(p[b].aname =="");
					cout<<"Enter book number: ";
					cin>>p[b].bnumber;
				}
				cout<<"You added "<<a<<" books";
				for (b=0;b<a;b++){
					fout<<endl;
					fout<<endl;
					fout<<endl;
					fout<<"Book name: "<<p[b].bname<<endl;
					fout<<"Author name: "<<p[b].aname<<endl;
					fout<<"Book number: "<<p[b].bnumber<<endl;

				}
				cin.ignore(0,'\n');
				cin.getline(buffer,225);
				fout<<buffer<<"\n";
				fout.close();
				fin.open("si.txt");
				if (!fin)
				{
					cout<<"Unable to open for reading.\n";
					return (1);
				}
				char ff;
				while (fin.get(ff))
					cout<<ff;
				fin.close();
				
				cout<<"\n\nDo you want another transaction? (Y/N) ";
				char character;
				cin>>character;
				if (character=='y' || character=='Y') {
					system ("cls");
					mainmenu(); }
				else if (character=='n'||character=='N')
					exit(0);
				else if(character!='y'||character!='Y'||character!='n'||character!='N')
				{
					cout<<"pls..."<<endl<<"just answer yes or no..";
					system("pause");
					goto b;
				}
				else
					exit (0);
				
			
			}
		_getch();
return 0;
}

		char case2();{
			goto b;
b:
			{
				ofstream brrowd;
				
				cout<<"\n--------"<<endl;
				cout<<"BORROW BOOK";
				cout<<"\n--------"<<endl;
				sum=status[0]+status[1]+status[2]+status[3]+status[4]+status[5]+status[6]+status[7]+status[8]+status[9]+status[10]+status[11]+status[12]+status[13]+status[14]+status[15]+status[16]+status[17]+status[18]+status[19];
				brrowd.open("brrowd.txt");
				if (sum==20)
				{
					cout<<"All the books were borrowed"<<endl;
					brrowd<<"All the books were borrowed"<<endl;
				}
				else ;
				cout<<"What would you like to borrow?"<<endl;
				brrowd<<"What would you like to borrow?"<<endl;
				for (int x=0;x<20;x++)
				{
					cout<<"Book "<<x+1<<": ";
					
					if (status[x]==1){
						cout<<"Borrowed"<<endl;
						brrowd<<"Borrowed"<<endl;
						brrowd<<"Book "<<x+1<<": ";}
					else
						{cout<<"Available"<<endl;
						brrowd<<"Available"<<endl;
						brrowd.close();}
				}
				
				int booknumber;
				cout<<"Borrow book: ";
				cin>>booknumber;
				
			 if (status[booknumber-1]==1)
				{
					system("cls");
					cout<<"Someone has already borrowed that book. Please choose another book"<<endl;
					system("pause");
					system("cls");
					goto b;
				}
				else if
					(status[booknumber-1]==0){
				cout<<"thank you for borrowing!!";
				status[booknumber-1]=1;
				}
				
				
				cout<<"\n\nDo you want another transaction? (Y/N) ";
				cin>>c;
				if (c=='y' || c=='Y') {
					system ("cls");
					mainmenu(); }
				else if(c!='y'||c!='Y'||c!='n'||c!='N')
				{
					cout<<"pls..."<<endl<<"just answer yes or no..";
					system("pause");
					goto b;
				}
				else
					exit (0);
			}

		_getch();
return 0;
}

			char case3();{
{
			string line;
			ifstream si ("si.txt");
	if (si.is_open())
	{
		while (si.good())
		{
			getline(si,line);
			cout<<line<<endl;
		}
		si.close();
	}
	else 
		{
			cout<<"Unable to open file";
	}
	
			cout<<"\n\nDo you want another transaction? (Y/N) ";
				cin>>c;
				if (c=='y' || c=='Y') {
					system ("cls");
					mainmenu(); }
				else
					exit (0);	
				
	
				}
		_getch();
return 0;
}

			char mainmenu();{
cout<<"\n--------------------------------------------------------------------------------"<<endl;
	gotoxy (31,2);cout<<"Library Book System"<<endl;
	cout<<"--------------------------------------------------------------------------------"<<endl;
	cout<<"\n[1] Add book\n[2] Borrow book\n[3] Check available books\n[4] Quit";
	cout<<"\n\nSelect Transaction: ";
	cin>>d;
	system ("cls");
	book p[200];
	switch (d) {
	case 1: case1();
	case 2: case2();
	case 3: case3();
	case 4: exit (0);	
	break;
		}
	_getch ();
	return 0;
	}
		_getch();
return 0;
}
Last edited on
BUMP :(
okay i decided to use vectors in this here is the code

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include<conio.h>
#include<iostream>
#include<fstream>
#include<string>
#include<windows.h>
#include<vector>

using namespace std;


string line,line1,line3,line4;
string line2;
char c;
int d,j;
int count=0;
vector<string> stringray(100);
void gotoxy (short row, short col)


{
    COORD coord = {row,col};
    SetConsoleCursorPosition ( GetStdHandle ( STD_OUTPUT_HANDLE ), coord );
}

struct book {
    string bname;
    string aname;
    string bnumber;
};



char case1();
char case2();
char case3();
char mainmenu();

int main ()
{

    cout<< "\n\n\nWelcome to Library Books Management System\n\n\n";
    system ("pause");
    system ("cls");
    mainmenu();
    _getch();
    return 0;
}




char case1()
{
    {
        fstream si;
        si.open("si.txt");
        char buffer [225];
        ifstream fin("si.txt");
        if(fin)
        {
            char ff;
            while(fin.get(ff))
                cout<<ff;
        }
        fin.close();
        ofstream fout("si.txt",ios::app);
        if (!fout)
        {
            cout<<"unable to open for appending.\n";
            return (1);
        }
        int a,b,c=0;
        cout<<"\n--------"<<endl;
        cout<<"ADD BOOK";
        cout<<"\n--------"<<endl;
        cout<<"How many books will you add?: "<<endl;
        cin>>a;
        book p[300];
        for (b=0; b<a; b++) {
            cout<<endl;
            cout<<"Enter book name: ";
            do {
                getline (cin,p[b].bname);
            }
            while
            (p[b].bname =="");
            cout<<"Enter author name: ";
            do {
                getline (cin,p[b].aname);
            }
            while
            (p[b].aname =="");
            cout<<"Enter book number: ";
            cin>>p[b].bnumber;
        }
        cout<<"\n\nYou added "<<a<<" books\n\n";
        for (b=0; b<a; b++) {
            fout<<"> Book name: "<<p[b].bname<<endl;
            fout<<"] Author name: "<<p[b].aname<<endl;
            fout<<"# Book number: "<<p[b].bnumber<<endl;
            fout<<endl;


        }
        cin.ignore(0,'\n');
        cin.getline(buffer,225);
        fout<<buffer<<"\n";
        fout.close();
        fin.open("si.txt");
        if (!fin)
        {
            cout<<"Unable to open for reading.\n";
            return (1);
        }
        char ff;
        while (fin.get(ff))
            cout<<ff;
        fin.close();

        cout<<"\n\nDo you want another transaction? (Y/N) ";
        char character;
        cin>>character;
        if (character=='y' || character=='Y') {
            system ("cls");
            mainmenu();
        }
        else if (character=='n'||character=='N')
            exit(0);
        else if(character!='y'||character!='Y'||character!='n'||character!='N')
        {
            cout<<"pls..."<<endl<<"just answer yes or no..";
            system("pause");
        }
        else
            exit (0);


    }
    _getch();
    return 0;
}

char case2()
{
    goto b;
b:
    {

        cout<<"\n--------"<<endl;
        cout<<"BORROW BOOK";
        cout<<"\n--------"<<endl;

        cout<<"What would you like to borrow?\n"<<endl;
        ifstream si ("si.txt");
        if (si.is_open())
        {

            while (si.good())
            {


                getline(si,line1);
                for (size_t i = 0; i<line1.size(); i++) {
                    if (line1[i] == '#')
                        count++;

                }
				
            }
			
            si.close();

        }
        else
        {
            cout<<"There are no books available.";
        }

		ifstream so ("si.txt");
        if (so.is_open())
        {

		getline(so,line2,']');
                cout<<"[1]"<<line2<<endl;
            for (int x=0; x<(count-1); x++){ 
				getline(so,line3,']');
				getline(so,line4,'>');
				int l3=line3.size();
				int l4=line4.size();
				for (l3; l3<l4; l3++){
					cout<<"\b";
				}
					getline(so,line2,']');
					strcpy(stringray[x], line2);
                cout<<"["<<x+2<<"]"<<line2<<endl;
				
				}
			
            so.close();

        }

        if(count==0)
            cout<<"There are no books available.";
        else if(count==1)
            cout<<"There is 1 book available.";
        else
            cout<<"There are "<<count<<" books available.";

        cout<<"\n\nDo you want another transaction? (Y/N) ";
        cin>>c;
        if (c=='y' || c=='Y') {
            system ("cls");
            mainmenu();
        }
        else
            exit (0);


    }
    _getch();
    return 0;
}

char case3()
{
    {

        ifstream si ("si.txt");
        if (si.is_open())
        {
            while (si.good())
            {
                getline(si,line);
                cout<<line<<endl;
            }
            si.close();
        }
        else
        {
            cout<<"Unable to open file";
        }

        cout<<"\n\nDo you want another transaction? (Y/N) ";
        cin>>c;
        if (c=='y' || c=='Y') {
            system ("cls");
            mainmenu();
        }
        else
            exit (0);


    }
    _getch();
    return 0;
}

char mainmenu()
{
    cout<<"\n--------------------------------------------------------------------------------"<<endl;
    gotoxy (31,2);
    cout<<"Library Book System"<<endl;
    cout<<"--------------------------------------------------------------------------------"<<endl;
    cout<<"\n[1] Add book\n[2] Borrow book\n[3] Check available books\n[4] Quit";
    cout<<"\n\nSelect Transaction: ";
    cin>>d;
    system ("cls");
    book p[200];
    switch (d) {
    case 1:
        case1();
    case 2:
        case2();
    case 3:
        case3();
    case 4:
        exit (0);
        break;
    }
    _getch ();
    return 0;
}


the problem is in the case2 function

here are the errors


Error 1 error C2664: 'strcpy' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'char *' c:\users\user\documents\visual studio 2010\projects\hoho\hoho\hihi.cpp 194 1


2 IntelliSense: no suitable conversion function from "std::string" to "char *" exists c:\users\user\documents\visual studio 2010\projects\hoho\hoho\hihi.cpp 194 13


3 IntelliSense: no suitable conversion function from "std::string" to "const char *" exists c:\users\user\documents\visual studio 2010\projects\hoho\hoho\hihi.cpp 194 27


can someone help me? :)
Topic archived. No new replies allowed.