Getting exponential for the calculation

I am trying to get a proper answer from the calculation part.. but in the end it gave me some exponential answer with not the answer I want it. After added customer record.. When i choose View customer record.. the room fees is not in proper way.. but funny thing is when i added the second customer record.. the room fees is in correct way.. any solutions?

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

using namespace std;

struct customer
{
    string name;
    string ic;
    string email;
    int roomtype;
    int night;
    float roomfees;
    string checkindate;


void add_cus();

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 << "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;
cout<<endl;
cout<<"Customer IC: ";
 while (!(cin >> ic) )       // get the input and check it was ok
    {
        cin.clear();            // reset flags
        cin.ignore(1000, '\n'); // empty input buffer
        cout << "Not an integer, please try again: ";
    }
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<<"Wrong room type."<<endl;
}while(roomtype > 3);
cout<<endl;
cout<<"Number of night: ";
cin>>night;
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;
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>>read2;obj.name=read2;
data>>read2;obj.ic=read1;
data>>read2;obj.email=read2;
data>>read2;obj.roomtype=read1;
data>>read2;obj.roomfees=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"){//DELETE
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=obj2[j+1].name;
            obj2[j].ic=obj2[j+1].ic;
            input[j]=input[j+1];

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

           }
}
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 record 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 record 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(unsigned int r=0; r<input.size();r++){
    data<<input[r]<<endl;}

  for(unsigned 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=true;
}
}

else if(option=="7")//exit the program
{
    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 
it gave me some exponential answer with not the answer I want

I scanned your code looking for where you calculate the exponential. I also looked for the pow() function. I can't see either.

At a guess what has happened is that you have some uninitialised floating-point variable. Because the variable was never assigned any value, it contains garbage, whatever happened to be already in the memory location where the variable is stored.

So the advice is this: make sure all variables are given a specific value before attempting to use them either in a calculation, or for output.
Last edited on
Okay.. means that i have to declare back all the variables?
It means either, a variable needs an initial value like this:
 
int count = 5; // initialised at time of declaration. 
or perhaps like this
1
2
int count; // not initialised
count = 5; // assigned value here 


Alternatively, make sure each variable receives a value from somewhere
1
2
3
int count;  // not initialised
cout << "please enter count:";
cin >> count; // assigned value here 


or maybe
1
2
3
4
5
double average; // not initialised
int a = 3;
int b = 5;
int c = 7;
average = (a + b + c) / 3.0; // assigned value here 


These are just examples, I don't know which variable (or variables) in your code is giving you trouble, but you yourself probably have a better idea of that.

Generally speaking, it is a good idea to initialise all variables, even if it is only to set them to zero, at least if the answer displayed is also zero, you will have a clearer idea of where that value came from.
Last edited on
Well, I started trying to test the program, however it seems quite fragile. i chose option 1, "Add Customer", entered the customer name "Fred Bloggs" but immediately hit a problem. However, the data displayed on screen was more or less the same as that saved to the file using option 6, (which also ended the program, which I didn't expect).

However, when I restarted the program and chose option 4, "View All Customer Record", the displayed data was not the same as previously displayed, things were out of sync.

Although the original question was regarding the roomfees, I'm not sure what sequence of operations (which menu choices) and data values were used. I'd focus on getting the core input and output functionality working dependably before turning attention back to the calculation of fees.
I have no idea about the "space" code.. if you type in "FredBloggs" then it might work.. sorry im just a beginner to c++ :(
I admit I got distracted from the original question. Regarding a name containing spaces, you would use getline()
http://www.cplusplus.com/reference/string/string/getline/
That's simple enough used on its own, but does mean a bit of thought when mixed with other input statements. Perhaps you should leave that for now, and consider it later if needed.

Looking at the code, I got a bit lost in trying to understand some of it. The variable z and the vector input were things for which I didn't know the purpose. Use of meaningful names helps a lot in making a program easy to read and understand. Similarly, names like obj and obj2 don't convey anything meaningful to the reader, though I did figure those out.

Also, in the struct customer, member variable ic is declared as a string, but elsewhere it seems to be treated as an integer. Also string checkindate seems to be ignored in much of the code.

What I suggest is to focus on options 1 and 4 first of all. (personally I wouldn't have written any of the remaining sections until those were fully working and tested).

As I said, i got distracted by various other issues, but it looks like there must be a path through the execution of the code where float roomfees may be displayed without having a value previously assigned.
Last edited on
Topic archived. No new replies allowed.