Problems related to calculation/cin.getline();

Hi experts,I want to ask is there any mistakes of my calculation part?
(cin>>night; and cout<<roomfees;). This is because when I input the number of nights, the output produced doesn't match with the formula I've created.
Another problem is that why my code (cin>>name; , cin.getline();) the output will miss the first letter of what I input? Thanks in advance

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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <ctime>
#include <windows.h>

using namespace std;

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


void add_cus();

void view_all();

};

void menu()
{

system("cls");
cout<<"======================================\n";
cout<<"     1) Add Customer Record  \n";
cout<<"     2) Edit Customer Record\n";
cout<<"     3) Delete Customer Record\n";
cout<<"     4) View All Customer Record\n";
cout<<"     5) Search Customer Record\n";
cout<<"     6) Save Customer File\n";
cout<<"     7) Exit Program\n";
cout<<"======================================\n";
cout<<"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 << "Check in time: " << 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>>name[100];
    cin.getline(name,100);
    cout<<endl;
    cout<<"IC: ";
while (!(cin >> ic) )       // get the input and check it was ok
    {
        cout<<"Invalid input. Please insert integer numbers.\n"
            <<"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;
    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;
    }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;
    time_date();
    cout<<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<< "Room Fees: "<<roomfees<< endl;
    time_date();
    }
}

int main()
{
    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=read1;
    data>>read2;obj.email=read2;
    data>>read2;obj.roomtype=read1;
    data>>read2;obj.checkindate=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.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();
        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
I have a couple of questions about the following snippet:
1
2
3
   cout<<"Customer name: ";
    cin>>name[100];
    cin.getline(name,100);


First why are you trying to have the user enter the name twice, first with the extraction operator then with getline()?

Second why is name an array of char instead of a std::string?

Third do you realize that in the code above you are accessing your array out of bounds when you try to extract a single character into the 100th element? Remember valid array indexes are 0 to size - 1, 100 is one past the end of the array. Do you realize that that line is actually retrieving a single character? To retrieve C-string you wouldn't use the [100].

You may want to re-examine your code, making sure every beginning brace { has a matching ending brace } and there are no mismatches.

Because of your inconsistent indentation it's hard to read the code so this is all I'll comment about for now, except to notice you have a function named exit(), you shouldn't use this name for your function, there is already a standard function with that name.

Last edited on
Thanks for the reply anyway.
For the first question, I don't really understand what do you mean by "user enter the name twice". Is that "cin>>name;" and "cin.getline(name,100)" having the same function?

Secondly, I used char instead of a std::string because I searched an example from the Internet and it does work. However, the problem happened as what I mentioned in the question.

The third one, I don't really understand about the statement you made since I'm pretty weak in the concept of array. :(
For the first question, I don't really understand what do you mean by "user enter the name twice". Is that "cin>>name;" and "cin.getline(name,100)" having the same function?

Yes both the extraction operator>> and getline() try to retrieve a value from the user to place into a variable, name in this case. You only need one of these lines, I suggest you stick with the getline().

Secondly, I used char instead of a std::string because I searched an example from the Internet and it does work.

What didn't work? You appear to be using std::string elsewhere in your program so what exactly doesn't work in this instance?

The third one, I don't really understand about the statement you made since I'm pretty weak in the concept of array.

This is one of the reasons I asked why you were using a C-string in this instance instead of the std::string you are using elsewhere in your program. You don't seem to understand the difference between an array of char and a single char. When you defined your array you set the maximum size of this array, in this case 100:

char name[100]; // Define an array of char named name.

Now when you tried to use this array in your program you are trying to access one character because you used a number inside brackets.
 
    cin>>name[100];


Look at the other places in your program, do you use these brackets? Also as I have already stated arrays in C++ start at 0 and end at size - 1. Since the size of your array is 100, trying to access the array with an index greater than 99 would be accessing the array out of bounds.

Topic archived. No new replies allowed.