File streaming, creating file problem

Hey there, i'm about to finish my program but i only have one problem,

first 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
char case2()
{
    goto b;
b:
    count=0;

    {

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

        ifstream si ("books.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.";
        }
        sum=0;
        for(j=1; j<count+1; j++) {
            sum+=status[j];
        }

        if (sum==count)
        {
            cout<<"All the books were borrowed"<<endl;
            cout<<"\n\nDo you want another transaction? (Y/N) ";
            cin>>c;
            if (c=='y' || c=='Y') {
                system ("cls");
                mainmenu();
            }
            else
                exit (0);
        }
        else {
            cout<<"What would you like to borrow?\n"<<endl;
            ifstream so ("books.txt");
            if (so.is_open())
            {

                x=0;
                getline(so,line2[1],']');
                ifstream borrowed;
                borrowed.open(line2[1]+".txt");
                if (borrowed.is_open())
                {
                    while (borrowed.good())
                    {
                        status[1]=1;
                    }
                    borrowed.close();
                }
                else
                {
                    status[1]=0;
                }
                borrowed.close();
                if (status[1]==1) {
                    cout<<"[1]"<<line2[1]<<" - Borrowed"<<endl;
                }
                else
                {   cout<<"[1]"<<line2[1]<<" - Available"<<endl;
                }

                for (x=2; x<count+1; x++) {
                    getline(so,line3,'#');
                    getline(so,line4,'>');
                    getline(so,line2[x],']');
                    ifstream borrowed;
                    borrowed.open(line2[x]+".txt");
                    if (borrowed.is_open())
                    {
                        while (borrowed.good())
                        {
                            status[x]=1;
                        }
                        borrowed.close();
                    }
                    else
                    {
                        status[x]=0;
                    }
                    borrowed.close();
                    if (status[x]==1) {
                        cout<<"["<<x<<"]"<<line2[x]<<" - Borrowed"<<endl;
                    }
                    else
                    {   cout<<"["<<x<<"]"<<line2[x]<<" - Available"<<endl;
                    }


                }

                so.close();

            }

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




            int booknumber;
            cout<<"Borrow book: ";
            cin>>booknumber;


            if(booknumber<1||booknumber>count) {
                cout<<"There is no such book in the system";
                cout<<"\n\nDo you want another transaction? (Y/N) ";
                cin>>c;
                if (c=='y' || c=='Y') {
                    system ("cls");
                    mainmenu();
                }
                else
                    exit (0);
            }
            else {
                system("cls");
                if (status[booknumber]==1)
                {

                    cout<<"Someone has already borrowed that book. Please choose another book."<<endl;
                    cout<<"\n\nDo you want another transaction? (Y/N) ";
                    cin>>c;
                    if (c=='y' || c=='Y') {
                        system ("cls");
                        mainmenu();
                    }
                    else
                        exit (0);
                }

                else if
                (status[booknumber]==0) {
                    cout<<"Thank you for borrowing!!\n";
                    status[booknumber]=1;
                    cout<<"\nBorrower's Name: ";
                    do {
                        getline(cin, p1.Name);
                    } while(p1.Name == "");
                    cout<<"\nEnter student Number: ";
                    do {
                        getline(cin, p1.Number);
                    } while(p1.Number == "");
                    cout<<"\nEnter student Course: ";
                    do {
                        getline(cin, p1.Course);
                    } while(p1.Course == "");
                    cout<<"\nEnter student Year: ";
                    do {
                        getline(cin, p1.Year);
                    } while(p1.Year == "");
                    ofstream filestream;
                    filestream.open(line2[booknumber]+".txt");
                    filestream<<"This book is borrowed by...\n";
                    filestream<<"\nBorrower's Name: "<<p1.Name;
                    filestream<<"\nStudent Number: "<<p1.Number;
                    filestream<<"\nStudent Course: "<<p1.Course;
                    filestream<<"\nStudent Year: "<<p1.Year;

                    filestream.close();
                }
            }
        }
        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;
}


(pasted only a portion of the program because it wouldn't fit, ill post the whole code in the next post)

all of the problems i encountered is in case 2,
my last problem is on line 180, when i run the program it does not make the txt file which will be a problem because if i run the program again, it will check/search the folder for the similarly named text file and if it finds one that particular booked will be marked as borrowed, it has one instance that it creates a text file though, that is when this is the content of the books.txt

  Book name : Book Name 1
] Author name : Author 1
# Book number : 1

> Book name : Book Name 2
] Author name : Author 2
# Book number : 2


> Book name : Book Name 3
] Author name : Author 3
# Book number : 3

> Book name : Book Name 4
] Author name : Author 4
# Book number : 4

> Book name : Book Name 5
] Author name : Author 5
# Book number : 5


but when it creates the file, it will get "Book name : Book name 1" but since windows doesn't accept the : character in filenames, it will stop making the file. when i change the : character it won't create the text file.
help :(
Last edited on
remaining 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
#include<conio.h>
#include<iostream>
#include<fstream>
#include<string>
#include<windows.h>
#include<vector>

using namespace std;


string line,line1,line3,line4;
char c;
int d,j,x;
int count=0;
vector<string> line2(100);
int status[100];
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;
};
struct stud
{
    string Name;
    string Number;
    string Course;
    string Year;
};

stud p1;


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("books.txt");
        char buffer [225];
        ifstream fin("books.txt");
        if(fin)
        {
            char ff;
            while(fin.get(ff))
                cout<<ff;
        }
        fin.close();
        ofstream fout("books.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("books.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;
}

//INSERT CASE2 CODE HERE//

char case3()
{
    {

        ifstream si ("books.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;
}
BUMP :( Please help me my deadline is on January 5 :)
Last edited on
Topic archived. No new replies allowed.