A fragile program

Guys, are there any solutions to make my program not so fragile that easy crash? And by the way, why am I getting a weird number such as 0,1,1 and so on in my CustRecord text files? is there any problem at option 6? Thanks for the help in advance.
P.S: Appreciate so much from Chervil's help in previous post.. Muacks! Thank you!
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <iostream> //cin , cout
#include <string> // string
#include <iomanip>
#include <fstream> //save/load files
#include <cstdlib>
#include <vector> //vectors included
#include <cmath> //multiplication
#include <ctime> //current date and time
#include <windows.h> //background and words colour


using namespace std;

struct customer
{
    char name[100];
    string ic, email, check_in_date;
    int roomtype, night;
    double roomfees;


void add_cus(); //show t

void view_all();

};

void menu()
{
    system("cls");
    cout<<"======================================\n"
        <<"     1) Add Customer Record\n"
        <<"     2) Edit Customer Record\n"
        <<"     3) Delete Customer Record\n"
        <<"     4) View All Customer Record\n"
        <<"     5) Search Customer Record\n"
        <<"     6) Save Customer File\n"
        <<"     7) Exit Program\n"
        <<"======================================\n"
        <<"Please select an option: ";
}

void room()
{
    system("cls");
    cout<<"Please choose a room: \n"
        <<"==============================\n"
        <<"   1.) Single Room(single)\n"
        <<"       Price: RM30 per night\n"
        <<"   2.) Deluxe Room\n"
        <<"       Price: RM50 per night\n"
        <<"   3.) Double Room\n"
        <<"       Price: RM80 per night\n"
        <<"==============================\n";
}

void time_date()
{
   time_t now = time(0);// current date/time based on current system
   char* dt = ctime(&now);

   cout << "You exit the program at: " << dt << endl;
}

void exit()
{
    string msg = "Thanks for using our program.";
    system("color 1A");
    for(int i=0; i<msg.length(); i++)
    {
      cout << msg[i] ;
      cout<<"\a";

      Sleep(250);
   }
   Sleep(250);
}

void customer::add_cus()
{
    cout<<"Customer name: ";
    cin.ignore();
    cin.getline(name,100);
    cout<<endl;
    cout<<"Customer IC: ";
while (!(cin >> ic))// get the input and check it was ok
    {
        cout<<"Invalid input. Please insert integer numbers.\n"
            <<"Customer IC: ";
        cin.clear();            // reset flags
        cin.ignore(1000, '\n'); // empty input buffer
    }
    cout<<endl;
    cout<< "Customer Email address: ";
    cin>>email;
    cout<<endl;
    room();

    do{
    cout<<"Customer's room type choice: ";
    cin>>roomtype;
    }while(roomtype > 3);
    cout<<endl;
    cout<<"Number of night(s): ";
    while(!(cin>>night))
    {
        cout<<"Invalid input. Please insert an integer.\n"
            <<"Number of night(s): ";
        cin.clear();
        cin.ignore(1000,'\n');
    }
    cout<<endl;
    cout<<"Check in Date(dd/mm/yyyy): ";
    cin>>check_in_date;
    cout<<endl;
    if(roomtype == 1 )
        roomfees = night * 30;
    else if(roomtype == 2)
        roomfees = night * 50;
    else if(roomtype == 3)
        roomfees = night * 80;
    else
        cout<<"Invalid input. Please choose a room type from 1-3."<<endl;
}

void customer::view_all()
{
    {
    cout<< "Name: "<< name << endl;
    cout<< "IC: "<< ic << endl;
    cout<< "Email address: "<< email << endl;
    cout<< "Room type: "<< roomtype << endl;
    cout<< "Number of night(s): "<< night << endl;
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout<< "Room Fees: RM"<< roomfees << endl;
    cout<< "Check in Date: "<< check_in_date <<endl;
    }
}

int main()
{
    int night;
    double roomfees;
    string option;//choice
    bool check;
    customer obj;//object
    int customerNo=0,z=0;//initiate
    vector<int>input;
    vector<customer>obj2;
    ifstream data;
    char set;

    data.open("CustRecord.txt",ios::in);//txt files

    if(data.is_open())//if file found

{

    int size1,read1;
    string read2;
    char read3;
    data>>read1;
    size1=read1;
    for(int r=0; r<size1;r++){
    data>>read1;
    input.push_back(read1);z++;}//pushback at last line



    for(int r=0; r<size1;r++){
    data>>read3;obj.name[100]=read3;
    data>>read2;obj.ic=read2;
    data>>read2;obj.email=read2;
    data>>read2;obj.roomtype=read1;
    data>>read1;obj.roomfees=read1;
    data>>read1;obj.night=read1;
    data>>read2;obj.check_in_date=read2;
    obj2.push_back(obj);
    }
};


do{
    menu();
    cin>>option;

if(option=="1")
{
    system("cls");
    obj.add_cus();
    obj2.push_back(obj);
    if(z>0){customerNo=input[z-1];}++customerNo;
    input.push_back(customerNo);
    system("pause");
    check=false;
}

else if(option=="2")
{
    system("cls");
    if(input.size()>0){
    int found =-1,value;
    while (cout <<"Customer No. ( refer at View All ): " && !(cin >> value))

    {
    cin.clear();
    cin.ignore();
    }cin.ignore();

    int arraySize = sizeof(input)/sizeof(int);
    int i=0;

    while(found ==-1 &&i<arraySize){
       if (input[i] ==value)
            found =i;
       i++;}
       if(found>=0){
       obj2[found].add_cus();
       }
    }else cout <<"No customer record available\n";
    system("pause");//pause
    check=false;
}

else if(option=="3")
{
    system("cls");
    if(input.size()>0){
    int b=0,ans;
    for(unsigned int i=0;i<obj2.size();i++){
    cout<<"\n";
    cout<<"Index :"<<b+1<<endl;
    obj2[i].view_all();b++;}
    while ( cout <<"Which customer record do you want to delete: " && !(cin >> ans))
            {
            cin.clear();
            cin.ignore();
            }
            cin.ignore();
           if(ans<=b&&ans>0){
            for (int j=ans-1;j<obj2.size()-1;j++){
            obj2[j].name[100]=obj2[j+1].name[100];
            obj2[j].ic=obj2[j+1].ic;
            input[j]=input[j+1];

           }
           obj2.pop_back();
           input.pop_back();//remove last

           }
    }
    else cout<<"No customer records available\n";
    system("pause");
    check=false;
}

else if(option=="4")//view all
{
    system("cls");
    if(input.size()>0){
        for(int i=0;i<obj2.size();i++){
            cout<<"\nCustomer : "<<input[i]<<endl;
            obj2[i].view_all();}cout<<endl<<endl;
      }
        else cout <<"No customer records available\n";
    system("pause");
    check=false;
}

else if(option=="5")//search
{
    system("cls");
    if(input.size()>0){
    int found =-1,value;
    while (cout <<"Customer No. ( refer at View All ): " && !(cin >> value))

    {
    cin.clear();
    cin.ignore();
    }cin.ignore();

    int arraySize = sizeof(input)/sizeof(int);
    int i=0;

    while(found ==-1 &&i<arraySize){
       if (input[i] ==value)
            found =i;//i'th position
       i++;}
       if(found>=0){obj2[found].view_all();}
           }
else cout <<"No customer records available\n";
           system("pause");
            check=false;
}

else if(option=="6")
{
    ofstream data;
    data.open("CustRecord.txt",ios::out);
    if(data.is_open()){
    data<<input.size()<<endl;
    for(int r=0; r<input.size();r++){
    data<<input[r]<<endl;}

    for(int r=0; r<input.size();r++){
    data<<"\n";

    data<<"Customer Name: "<<obj2[r].name<<endl;
    data<<"Customer IC: "<<obj2[r].ic<<endl;
    data<<"Customer Email address: "<<obj2[r].email<<endl;
    data<<"Customer Room Type Choice: "<<obj2[r].roomtype<<endl;
    data<<"Customer Room Fees: "<<obj2[r].roomfees<<endl;
    data<<"Number of Night: "<<obj2[r].night<<endl;
    data<<"Check in Date: "<<obj2[r].check_in_date<<endl;
    }
    data.close();
    check=false;
    system("pause");
    }
}

else if(option=="7")//exit the program
{
    string quit;
    system("cls");
    cout<<"Are you sure you want exit the program?\n"
        <<"Press Y to exit.\n";
    cin>>quit;
    if(quit != "Y" && quit != "y")
    {
        system("cls");
        check=false;
    }

    else
    {
        system("cls");
        exit();
        cout<<endl;
        time_date();
        return 0;
    }
}

else
{
    cout <<"Invalid selection. Please enter a valid selection (1-7)\n";
    system("pause");
    check=false;
}

}while(!check);
return  0;
}//end of project
Last edited on
Topic archived. No new replies allowed.