problem in my program

i have added two users details in main function when i execute the program it only adds the total values of 1st customer but when i choose 2nd user it doesnt add total values . i dont know how to do ,as i have initialized no of music items as 3 by using categoryDetailsObject.musicCount as 3 can i do it same way for customers as well but since array is used (customerDetailsObject[2]) like customerDetailsObject[].customerCount or something i dont know how to do it can anyone help me ..



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
class customerDetails
{
public: 
    item itemDetailsObject[50];
    int itemDetailsCount;

    string customerName;
    string userID;
    string password;

    void viewCart();

}customerDetailsObject[2];                                          

void input()
{
    cout<<"\nLogin ID :"<<endl;
    cin>>id;

    cout<<"\nLogin Password :"<<endl;
    cin>>pass;
}

int validity(string id,string pass)
{   
    int correctMatch;

    for (int i=0; i<=1; i++) 
    {
        if (id==customerDetailsObject[i].userID && pass==customerDetailsObject[i].password)
        {
            correctMatch=i;
            return correctMatch;
        }
        else
        {
            correctMatch=-1; 
        }
    }
    return correctMatch;
}

void login()
{
    input();
                                                                    
    matchedAccount=validity(id, pass);      
    if (matchedAccount == -1) 
    {
        login();
    }
}

void customerDetails::viewCart()
{
    for (int i=0; i<itemDetailsCount; i++)
    {
        cout<<"\nName - "<<customerDetailsObject[0].itemDetailsObject[i].name<<endl;

        cout<<"Cost - Rs."<<customerDetailsObject[0].itemDetailsObject[i].cost<<endl;

        cout<<"Sku No - "<<customerDetailsObject[0].itemDetailsObject[i].sku<<endl;

    }

    int totalCost = 0;
    for (int i = 0; i < itemDetailsCount; i++)
    {
        totalCost += itemDetailsObject[i].cost;
    }
    cout<<"\nTotal Cost - Rs."<<totalCost<<endl;

    int selection;

    cout<<"\n1.Checkout"<<endl;
    cin>>selection;

    switch (selection) 
    {
        case 1:
            for (int i=0; i<itemDetailsCount; i++)
            {
                itemDetailsObject[i].name=" ";
                itemDetailsObject[i].sku=0;
                itemDetailsObject[i].cost=0;
                itemDetailsCount=0;
            }
            categoryDetailsObject.chooseCategory();
            break;

        default:viewCart();
            break;
    }  
}

int main (int argc, const char * argv[])
{
    customerDetailsObject[0].customerName="David";
    customerDetailsObject[0].userID="dav2";
    customerDetailsObject[0].password="abc12";
    customerDetailsObject[0].itemDetailsCount=0;


    customerDetailsObject[1].customerName="Rahul";
    customerDetailsObject[1].userID="rah45";
    customerDetailsObject[1].password="1awe";
    customerDetailsObject[1].itemDetailsCount=0;

   
    categoryDetailsObject.musicCount = 3;
    categoryDetailsObject.electronicsCount = 4;
    categoryDetailsObject.bookCount = 5;



Last edited on
I don't understand your question...

Anyway, this can't be right since it's an array
1
2
3
    categoryDetailsObject.musicCount = 3;
    categoryDetailsObject.electronicsCount = 4;
    categoryDetailsObject.bookCount = 5;
where (what struct/class) are musicCount (and the other) declared? They're not part of the class customerDetails
musicCount is in category class its not a full program i tried to add it but it exceed the no of lines required here is the full program the next half is in next reply column

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
358
359
360
361
362
363
364
365
366
367
368
369
370
#include <iostream>
using namespace std;

int validity(string ,string );

class item
{
public:
    string name;
    int cost;
    int sku;
   
   void displayDetails();
   
};


int ch;                                                             	
string id;
string pass;
int matchedAccount;     						.


void item::displayDetails()
{
    cout<<"\n"<<name<<endl;
    cout<<"cost:"<<cost<<endl;
    cout<<"sku:"<<sku<<endl;
}


class customerDetails
{
public: 
       

    item itemDetailsObject[50];
    int itemDetailsCount;
   

    string customerName;
    string userID;
    string password;
  
    void viewCart();
    
}customerDetailsObject[2];                                          
void input()
{
    cout<<"\nLogin ID :"<<endl;
    cin>>id;
  
    cout<<"\nLogin Password :"<<endl;
    cin>>pass;
}

int validity(string id,string pass)
{   
    int correctMatch;
    
    for (int i=0; i<=1; i++)                                        
        if (id==customerDetailsObject[i].userID && pass==customerDetailsObject[i].password)
        {
            correctMatch=i;
            return correctMatch;
        }
        else
        {
            correctMatch=-1;                                        
        }
    }
    return correctMatch;
}


void login()
{
    input();
                                                                    
    matchedAccount=validity(id, pass);                              
    if (matchedAccount == -1) 
    {
        login();
    }
}


class category
{
public:

    item music[50];
    item electronic[50];
    item book[50];
    
    int musicCount;
    int electronicsCount;
    int bookCount;
    
    void chooseCategory();
    void chooseMusicItem();
    void chooseElectronicItem();
    void chooseBookItem();
    void displayMusicItemDetails();
    void displayElectronicItemDetails();
    void displayBookItemDetails();
  
}categoryDetailsObject;                                                               //Global Object


void musicAddToCart()
{
    int newPosition = customerDetailsObject[matchedAccount].itemDetailsCount;
    
    customerDetailsObject[matchedAccount].itemDetailsObject[newPosition]=categoryDetailsObject.music[ch-1];           
    customerDetailsObject[matchedAccount].itemDetailsCount++;
        
    cout<<"\nITEMS DETAILS COUNT INCREMENTER ="<<" "<<customerDetailsObject[matchedAccount].itemDetailsCount<<endl;
}

void electronicAddToCart()
{
    int newPosition = customerDetailsObject[matchedAccount].itemDetailsCount;
    
    customerDetailsObject[matchedAccount].itemDetailsObject[newPosition]=categoryDetailsObject.electronic[ch-1];           
    customerDetailsObject[matchedAccount].itemDetailsCount++;
   
    cout<<"\nITEMS DETAILS COUNT INCREMENTER ="<<" "<<customerDetailsObject[matchedAccount].itemDetailsCount<<endl;
}

void bookAddToCart()
{
    int newPosition = customerDetailsObject[matchedAccount].itemDetailsCount;
    
    customerDetailsObject[matchedAccount].itemDetailsObject[newPosition]=categoryDetailsObject.book[ch-1];           
    customerDetailsObject[matchedAccount].itemDetailsCount++;
    
    cout<<"\nITEMS DETAILS COUNT INCREMENTER ="<<" "<<customerDetailsObject[matchedAccount].itemDetailsCount<<endl;
}


void customerDetails::viewCart()
{
    for (int i=0; i<itemDetailsCount; i++)
    {
        cout<<"\nName - "<<customerDetailsObject[0].itemDetailsObject[i].name<<endl;
        cout<<"Cost - Rs."<<customerDetailsObject[0].itemDetailsObject[i].cost<<endl;
        cout<<"Sku No - "<<customerDetailsObject[0].itemDetailsObject[i].sku<<endl;
    }
    
    
    int totalCost = 0;
    for (int i = 0; i < itemDetailsCount; i++)
    {
        totalCost += itemDetailsObject[i].cost;
    }
    cout<<"\nTotal Cost - Rs."<<totalCost<<endl;
    
    
    int selection;
    
    cout<<"\n1.Checkout"<<endl;
    
    cin>>selection;
    switch (selection) 
    {
        case 1:
            for (int i=0; i<itemDetailsCount; i++)
            {
                itemDetailsObject[i].name=" ";
                itemDetailsObject[i].sku=0;
                itemDetailsObject[i].cost=0;
                itemDetailsCount=0;
            }
            categoryDetailsObject.chooseCategory();
            break;
            
        default:viewCart();
            break;
    }  
}



void category::chooseCategory()
{
    int select;
    
    cout<<"\nWelcome"<<" "<<customerDetailsObject[matchedAccount].customerName<<endl;

    cout<<"\nChoose a category\n"<<endl;
    
    cout<<"1.Music"<<endl;
    cout<<"2.Electronic"<<endl;
    cout<<"3.Books"<<endl;
    cout<<"4.Viewcart"<<endl;
    cout<<"5.Logout\n"<<endl;
    
    cout<<"Enter the choice"<<endl;
    
    cin>>select;
    
    switch (select) 
    {
        case 1:
            cout<<"Music"<<endl;
            this->chooseMusicItem();
            break;
            
        case 2:
            cout<<"Electronic"<<endl;
            this->chooseElectronicItem();
            break;
            
        case 3:
            cout<<"Books"<<endl;
            this->chooseBookItem();
            break;
            
        case 4:
            cout<<"View Cart"<<endl;
            customerDetailsObject->viewCart();
            
        case 5:
            login();
            break;
            
        default:
            chooseCategory();
            break;
    }
}


void category::chooseMusicItem()
{
    cout<<"Music Items Details\n"<<endl;
    
    for (int i=0; i<musicCount; i++)
    { 
        cout<<i+1<<"."<<music[i].name<<"  Rs."<<music[i].cost<<endl;
    }
    cout<<musicCount+1<<"."<<"Back"<<endl;
        
    
    cout<<"\nEnter Choice"<<endl;
    cin>>ch;
    
    if (ch<1)
    {
        chooseMusicItem();
    }
    else if (ch<=musicCount)
    {
        displayMusicItemDetails();
    }
    else if (ch==musicCount+1)
    {
        chooseCategory();        
    }
    else 
    {
        chooseMusicItem();
    }
}


void category::displayMusicItemDetails()
{
    

   music[ch-1].displayDetails();
   int options;

   cout<<"\n1.Add to Cart"<<endl;
   cout<<"\n2.Back"<<endl;
   cout<<"\n3.MainPage"<<endl;
   cout<<"\nEnter Choice"<<endl;

    cin>>options;

 
    switch (options) 
    {
        case 1:
            musicAddToCart();
            this->displayMusicItemDetails();
            break;
            
        case 2:
            this->chooseMusicItem();
            break;
            
        case 3:
            this->chooseCategory();
            break;
            
        default:displayMusicItemDetails();
            break;
    }
}


void category::chooseElectronicItem()
{
   
    cout<<"\nElectronic Items Details\n"<<endl;
    
    for (int i=0; i<electronicsCount; i++)
    {
        cout<<i+1<<"."<<electronic[i].name<<"  Rs."<<electronic[i].cost<<endl;
    }
    
    cout<<electronicsCount+1<<"."<<"Back"<<endl;
    
    cout<<"\nEnter Choice"<<endl;
    cin>>ch;
    
    if (ch<1)
    {
        chooseElectronicItem();
    }
    else if (ch<=electronicsCount)
    {
        displayElectronicItemDetails();
    }
    else if(ch==electronicsCount+1)
    {
        chooseCategory();
    }
    else
    {
        chooseElectronicItem();
    }
}


void category::displayElectronicItemDetails()
{
    
    electronic[ch-1].displayDetails();
    
    int options;
    cout<<"\n1.Add to Cart"<<endl;
    cout<<"\n2.Back"<<endl;
    cout<<"\n3.MainPage"<<endl;
    
    cout<<"\nEnter Choice"<<endl;
    cin>>options;
    
    switch (options) 
    {
        case 1:
            electronicAddToCart();
            this->displayElectronicItemDetails();
            break;
            
        case 2:
            this->chooseElectronicItem();
            break;
            
        case 3:
            this->chooseCategory();
            break;
            
        default:
            displayElectronicItemDetails();
            break;
    }
}









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
void category::chooseBookItem()
{

    cout<<"\nBook Items Details\n"<<endl;
    
    for (int i=0; i<bookCount; i++)
    {
        cout<<i+1<<"."<<book[i].name<<"  Rs."<<book[i].cost<<endl;
    }
    
    cout<<bookCount+1<<"."<<"Back"<<endl;
    
    cout<<"\nEnter Choice"<<endl;
    cin>>ch;
    
    if (ch<1)
    {
        chooseBookItem();
    }
    else if (ch<=bookCount)
    {
        displayBookItemDetails();
    }
    else if(ch==bookCount+1)
    {
        chooseCategory();   
    }
    else
    {
        chooseBookItem();
    }
}

void category::displayBookItemDetails()                                                 
{
   book[ch-1].displayDetails();
    
    int options;
    
    cout<<"\n1.Add to Cart"<<endl;
    cout<<"\n2.Back"<<endl;
    cout<<"\n3.MainPage"<<endl;
    
    cout<<"\nEnter Choice"<<endl;
    cin>>options;
    
    switch (options) 
    {
        case 1:
            bookAddToCart();
            this->displayBookItemDetails();
            break;
            
        case 2:
            this->chooseBookItem();
            break;
            
        case 3:
            this->chooseCategory();
            break;
            
        default:
            displayBookItemDetails();
            break;
    }
}


int main (int argc, const char * argv[])
{

    customerDetailsObject[0].customerName="David";
    customerDetailsObject[0].userID="dav2";
    customerDetailsObject[0].password="abc12";
    customerDetailsObject[0].itemDetailsCount=0;
    
    
    
    customerDetailsObject[1].customerName="Rahul";
    customerDetailsObject[1].userID="rah45";
    customerDetailsObject[1].password="1awe";
    customerDetailsObject[1].itemDetailsCount=0;

  
    
    categoryDetailsObject.musicCount = 3;
    categoryDetailsObject.electronicsCount = 4;
    categoryDetailsObject.bookCount = 5;
    
    
    categoryDetailsObject.music[0].name="The Dark Knight OST";
    categoryDetailsObject.music[0].cost=200;
    categoryDetailsObject.music[0].sku=10246;
    
    categoryDetailsObject.music[1].name="Rem";
    categoryDetailsObject.music[1].cost=300;
    categoryDetailsObject.music[1].sku=10247;
    
    categoryDetailsObject.music[2].name="Coldplay";
    categoryDetailsObject.music[2].cost=150;                                                                      
    categoryDetailsObject.music[2].sku=10248;
    
    
    categoryDetailsObject.electronic[0].name="Apple Iphone";
    categoryDetailsObject.electronic[0].cost=32000;
    categoryDetailsObject.electronic[0].sku=10249;
    
    categoryDetailsObject.electronic[1].name="Nokia 1661";
    categoryDetailsObject.electronic[1].cost=25200;
    categoryDetailsObject.electronic[1].sku=10250;
    
    categoryDetailsObject.electronic[2].name="Amazon Kindle";
    categoryDetailsObject.electronic[2].cost=10200;
    categoryDetailsObject.electronic[2].sku=10251;
    
    categoryDetailsObject.electronic[3].name="Rotomac pen";
    categoryDetailsObject.electronic[3].cost=1500;
    categoryDetailsObject.electronic[3].sku=10252;
    
    
    categoryDetailsObject.book[0].name="Let us C";
    categoryDetailsObject.book[0].cost=180;
    categoryDetailsObject.book[0].sku=10253;
    
    categoryDetailsObject.book[1].name="Fountain Head";
    categoryDetailsObject.book[1].cost=150;
    categoryDetailsObject.book[1].sku=10254;
    
    categoryDetailsObject.book[2].name="Godfather";
    categoryDetailsObject.book[2].cost=700;
    categoryDetailsObject.book[2].sku=10255;
    
    categoryDetailsObject.book[3].name="Tinkle";
    categoryDetailsObject.book[3].cost=20;
    categoryDetailsObject.book[3].sku=10256;
    
    categoryDetailsObject.book[4].name="Amar Chitra Katha";
    categoryDetailsObject.book[4].cost=20;
    categoryDetailsObject.book[4].sku=10257;
    
    
    login();
    

    categoryDetailsObject.chooseCategory();
     
}



Here how the program works ------ when i run the program it asks for login id and pass so when i give the correct id and pass it shows the list of catgories in that when i choose say music . in that it has 3 items so when i click one of them and add to cart that item will be added into the cart similarly when i choose multiple items and add them they will be added in add to cart , when i click view cart it will show the list of items which has been added and total value .

but my problem is since there are multiple customers when i choose 1st customer that is david the program runs properly ( it shows all the items which have been added and also shows total cost of all the items) but when i choose customer rahul after adding the items into add to cart , in the view cart it doesnt show the list of items and total cost , something needs to be changed in for loop used in viewCart function () .for (int i=0; i<itemDetailsCount; i++) so that current/logged in user choosen items get added in add to cart and the total cost of all the items get added up.

say tomo if i want to add 3 more items in music category i'll have to change categoryDetailsObject.musicCount = 5 and give the data in main function and automatically [b]musicCount variable[/b] is used in for loop in chooseMusicItemDetails function () will get changed to 5

similarly when i want to add 4 more customers i'll have to create customerCount variable but since customerDetailsObject is an array the above step will not work is there any way to do so that when i want to add customers i'll just add how many customers are there in that variable and use that variable in for loop used in validity function() ( (for int i=0 ; i<customerCount ;i++))
Last edited on
in the view cart it doesnt show the list of items and total cost
Yes, that is because in viewCart() you are using customerDetailsObject[0] while you want customerDetailsObject[matchedAccount]

You should search for all 'customerDetailsObject' and make sure that you're using 'matchedAccount' with it.

If you want a dynamic array for your customer you may use std::vector that holds the size()/number of elements. The same applies to the items. A push_back() automatically increases the size() but behaves otherwise like an array
i tried using matchedAccount in customerDetailsObject[matchedAccount] , it didnt worked out

in view cart it shows the list of items thats why i have used for loop

in main function if i wanted to add few more customers how to do it well can i do same way where i have written categoryItemsDetails.musicCount = 3

is there a way like that to do for customerDetails object since its in array i am not able to do like that
i tried using matchedAccount in customerDetailsObject[matchedAccount] , it didnt worked out
What does it mean? Line 138 does the right thing but line 146 doesn't when you use customerDetailsObject[matchedAccount]...?


Wait... viewCart() is part of customerDetails so:
1
2
3
4
5
6
    for (int i=0; i<itemDetailsCount; i++)
    {
        cout<<"\nName - "<<itemDetailsObject[i].name<<endl; // no customerDetailsObject[0]. necessary
        cout<<"Cost - Rs."<<itemDetailsObject[i].cost<<endl; // neither here
        cout<<"Sku No - "<<itemDetailsObject[i].sku<<endl; // neither here
    }
This should work

in main function if i wanted to add few more customers how to do it well can i do same way where i have written categoryItemsDetails.musicCount = 3
Yes, but you have to increase the size of customerDetailsObject as well.

If you want an unlimited amount you should use std::vector.

All in all your structure is rather inconvenient/not really intuitive. Like try to incorporate functions like 'musicAddToCart()', 'electronicAddToCart()', ... into the class customerDetails. E.g. class customerDetails { ... AddMusicToCart(item) ... };

Avoid using global variables (especially with names like ch). My idea of a structure would be like that:
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
class CItem
{
....
};

class CCustomer
{
...
void TakeItem(CItem items[], int &count) // one of the items music etc from categoryDetailsObject
{
  cout << "What item";
  cin >> item_idx;

  m_Cart[m_Count] = items[item_idx];
  ++m_Count;
  --count;
}

// Details
...
CItem m_Cart[...];
int m_Count;
};

class CShop
{
...
void login();
...

CItem m_Items[...];
int m_ItemCount;

CCustomer m_Customers[...];
int m_CustomerCount;
int m_CurrentCustomer; // Or better: CCustomer *m_CurrentCustomer
};
Why not u make this program in cases.

If you done it may you solve your problem.
yes in line 138 it works idk why its not working in 146 cause when user Rahul is selected it adds up and even in the incrementer it increments every time i add an item but its not showing in view cart function that is line 146 .

in line 146 u say that customerDetailsObject isnt required but u should know which customer has logged has added the items in add to cart () . ??
because when it is customerDetailsObject[0]. that is when user is david ,, when the user adds the items in add to cart () in the viewCart () it shows up all the items that user has selected ..
but its not happening when user Rahul is logged in it doesnt show up in viewCart ().

johnlever ... cases ?
Last edited on
yes in line 138 it works idk why its not working in 146 cause when user Rahul is selected it adds up and even in the incrementer it increments every time i add an item but its not showing in view cart function that is line 146 .
You implemented the choosing mechanism with recursions. That's not good. You need to get out of the function when the user selects "Back" instead of going 'deeper' in your display...() functions. You need a loop in your display...() functions if the user chooses the ...AddToCart() function (return if "Back" or "MainPage"). In case of "MainPage" the display...() functions should return false so that the the caller function can return as well (false) to really get to the chooseCategory().

in line 146 u say that customerDetailsObject isnt required but u should know which customer has logged has added the items in add to cart () . ??
Sure. You're already 'inside' the customerDetailsObject. Nothing more has to be done. You should ban the global variables anyway

conclusion: Get rid of the recusion (the function that calls itself) and get more into OOP
.. were u able to alter the program and execute it ?
The next couple of days I'm not online. On monday I can show you a working version.
ok .
I promised a working version. Here it is. Not everything might be ok though
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
358
#include <iostream>
#include <string>
using namespace std;

int validity(string ,string );

class item
{
public:
    string name;
    int cost;
    int sku;
   
   void displayDetails();
   
};


int ch;                                                             	
string id;
string pass;
int matchedAccount;


void item::displayDetails()
{
    cout<<"\n"<<name<<endl;
    cout<<"cost:"<<cost<<endl;
    cout<<"sku:"<<sku<<endl;
}


class customerDetails
{
public: 
       

    item itemDetailsObject[50];
    int itemDetailsCount;
   

    string customerName;
    string userID;
    string password;
  
    void viewCart();
    
}customerDetailsObject[2];                                          
void input()
{
    cout<<"\nLogin ID :"<<endl;
    cin>>id;
  
    cout<<"\nLogin Password :"<<endl;
    cin>>pass;
}

int validity(string id,string pass)
{   
    int correctMatch;
    
    for (int i=0; i<=1; i++)
    {
        if (id==customerDetailsObject[i].userID && pass==customerDetailsObject[i].password)
        {
            correctMatch=i;
            return correctMatch;
        }
        else
        {
            correctMatch=-1;                                        
        }
    }
    return correctMatch;
}


void login()
{
    input();
                                                                    
    matchedAccount=validity(id, pass);                              
    if (matchedAccount == -1) 
    {
        login();
    }
}


class category
{
public:

    item music[50];
    item electronic[50];
    item book[50];
    
    int musicCount;
    int electronicsCount;
    int bookCount;
    
    void chooseCategory();
    bool chooseMusicItem();
    bool chooseElectronicItem();
    bool chooseBookItem();
    bool displayMusicItemDetails();
    bool displayElectronicItemDetails();
    bool displayBookItemDetails();
  
}categoryDetailsObject;                                                               //Global Object


void musicAddToCart()
{
    int newPosition = customerDetailsObject[matchedAccount].itemDetailsCount;
    
    customerDetailsObject[matchedAccount].itemDetailsObject[newPosition]=categoryDetailsObject.music[ch-1];           
    customerDetailsObject[matchedAccount].itemDetailsCount++;
        
    cout<<"\nITEMS DETAILS COUNT INCREMENTER ="<<" "<<customerDetailsObject[matchedAccount].itemDetailsCount<<endl;
}

void electronicAddToCart()
{
    int newPosition = customerDetailsObject[matchedAccount].itemDetailsCount;
    
    customerDetailsObject[matchedAccount].itemDetailsObject[newPosition]=categoryDetailsObject.electronic[ch-1];           
    customerDetailsObject[matchedAccount].itemDetailsCount++;
   
    cout<<"\nITEMS DETAILS COUNT INCREMENTER ="<<" "<<customerDetailsObject[matchedAccount].itemDetailsCount<<endl;
}

void bookAddToCart()
{
    int newPosition = customerDetailsObject[matchedAccount].itemDetailsCount;
    
    customerDetailsObject[matchedAccount].itemDetailsObject[newPosition]=categoryDetailsObject.book[ch-1];           
    customerDetailsObject[matchedAccount].itemDetailsCount++;
    
    cout<<"\nITEMS DETAILS COUNT INCREMENTER ="<<" "<<customerDetailsObject[matchedAccount].itemDetailsCount<<endl;
}


void customerDetails::viewCart()
{
  bool again;
  do
  {
    again = false;
    for (int i=0; i<itemDetailsCount; i++)
    {
        cout<<"\nName - "<<itemDetailsObject[i].name<<endl;
        cout<<"Cost - Rs."<<itemDetailsObject[i].cost<<endl;
        cout<<"Sku No - "<<itemDetailsObject[i].sku<<endl;
    }
    
    
    int totalCost = 0;
    for (int i = 0; i < itemDetailsCount; i++)
    {
        totalCost += itemDetailsObject[i].cost;
    }
    cout<<"\nTotal Cost - Rs."<<totalCost<<endl;
    
    
    int selection;
    
    cout<<"\n1.Checkout"<<endl;
    
    cin>>selection;
    switch (selection) 
    {
        case 1:
            for (int i=0; i<itemDetailsCount; i++)
            {
                itemDetailsObject[i].name=" ";
                itemDetailsObject[i].sku=0;
                itemDetailsObject[i].cost=0;
                itemDetailsCount=0;
            }
            break;
            
        default:
          again = true;
            break;
    }
  }
  while(again);
}



void category::chooseCategory()
{
  bool again;
  do
  {
    again = true;
    int select;
    
    cout<<"\nWelcome"<<" "<<customerDetailsObject[matchedAccount].customerName<<endl;

    cout<<"\nChoose a category\n"<<endl;
    
    cout<<"1.Music"<<endl;
    cout<<"2.Electronic"<<endl;
    cout<<"3.Books"<<endl;
    cout<<"4.Viewcart"<<endl;
    cout<<"5.Logout\n"<<endl;
    
    cout<<"Enter the choice"<<endl;
    
    cin>>select;
    
    switch (select) 
    {
        case 1:
            cout<<"Music"<<endl;
            this->chooseMusicItem();
            break;
            
        case 2:
            cout<<"Electronic"<<endl;
            this->chooseElectronicItem();
            break;
            
        case 3:
            cout<<"Books"<<endl;
            this->chooseBookItem();
            break;
            
        case 4:
            cout<<"View Cart"<<endl;
            customerDetailsObject->viewCart();
            
        case 5:
          again = false;
            break;
            
        default:
          again = false;
            break;
    }
    }
    while(again);
}


bool category::chooseMusicItem()
{
  bool back = true;
  bool again;
  do
  {
    again = false;
    cout<<"Music Items Details\n"<<endl;
    
    for (int i=0; i<musicCount; i++)
    { 
        cout<<i+1<<"."<<music[i].name<<"  Rs."<<music[i].cost<<endl;
    }
    cout<<musicCount+1<<"."<<"Back"<<endl;
        
    
    cout<<"\nEnter Choice"<<endl;
    cin>>ch;
    
    if (ch<1)
    {
      again = true;
    }
    else if (ch<=musicCount)
    {
        back = displayMusicItemDetails();
        again = back;
    }
  }
  while(again);
  return back;
}


bool category::displayMusicItemDetails()
{
  bool back = true;
  bool again;
  do
  {
    again = false;
   music[ch-1].displayDetails();
   int options;

   cout<<"\n1.Add to Cart"<<endl;
   cout<<"\n2.Choose music item"<<endl;
   cout<<"\n3.Back"<<endl;
   cout<<"\n4.MainPage"<<endl;
   cout<<"\nEnter Choice"<<endl;

    cin>>options;

 
    switch (options) 
    {
        case 1:
            musicAddToCart();
            again = true;
            break;
            
        case 2:
            back = this->chooseMusicItem();
            again = back;
            break;
            
        case 3:
            break;
            
        default:
          back = false;
            break;
    }
  }
  while(again);
  return back;
}


bool category::chooseElectronicItem()
{
  bool back = true;
  bool again;
  do
  {
    again = false;
    cout<<"\nElectronic Items Details\n"<<endl;
    
    for (int i=0; i<electronicsCount; i++)
    {
        cout<<i+1<<"."<<electronic[i].name<<"  Rs."<<electronic[i].cost<<endl;
    }
    
    cout<<electronicsCount+1<<"."<<"Back"<<endl;
    
    cout<<"\nEnter Choice"<<endl;
    cin>>ch;
    
    if (ch<1)
    {
        again = true;
    }
    else if (ch<=electronicsCount)
    {
        back = displayElectronicItemDetails();
        again = back;
    }
  }
  while(again);
  return back;
}
2. part:
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
bool category::displayElectronicItemDetails()
{
  bool back = true;
  bool again;
  do
  {
    again = false;
    electronic[ch-1].displayDetails();
    
    int options;
    cout<<"\n1.Add to Cart"<<endl;
    cout<<"\n2.Choose electronic item"<<endl;
    cout<<"\n3.Back"<<endl;
    cout<<"\n4.MainPage"<<endl;
    
    cout<<"\nEnter Choice"<<endl;
    cin>>options;
    
    switch (options) 
    {
        case 1:
            electronicAddToCart();
            again = true;
            break;
            
        case 2:
            back = this->chooseElectronicItem();
            again = back;
            break;
            
        case 3:
            break;
            
        default:
          back = false;
            break;
    }
  }
  while(again);
  return back;
}

bool category::chooseBookItem()
{
  bool back = true;
  bool again;
  do
  {
    again = false;
    cout<<"\nBook Items Details\n"<<endl;
    
    for (int i=0; i<bookCount; i++)
    {
        cout<<i+1<<"."<<book[i].name<<"  Rs."<<book[i].cost<<endl;
    }
    
    cout<<bookCount+1<<"."<<"Back"<<endl;
    
    cout<<"\nEnter Choice"<<endl;
    cin>>ch;
    
    if (ch<1)
    {
        again = true;
    }
    else if (ch<=bookCount)
    {
        back = displayBookItemDetails();
        again = back;
    }
  }
  while(again);
  return back;
}

bool category::displayBookItemDetails()                                                 
{
  bool back = true;
  bool again;
  do
  {
    again = false;
   book[ch-1].displayDetails();
    
    int options;
    
    cout<<"\n1.Add to Cart"<<endl;
    cout<<"\n2.Choose book item"<<endl;
    cout<<"\n3.Back"<<endl;
    cout<<"\n4.MainPage"<<endl;
    
    cout<<"\nEnter Choice"<<endl;
    cin>>options;
    
    switch (options) 
    {
        case 1:
            bookAddToCart();
            again = true;
            break;
            
        case 2:
            back = this->chooseBookItem();
            again = back;
            break;
            
        case 3:
            break;
            
        default:
          back = false;
            break;
    }
  }
  while(again);
  return back;
}


int main (int argc, const char * argv[])
{

    customerDetailsObject[0].customerName="David";
    customerDetailsObject[0].userID="dav2";
    customerDetailsObject[0].password="abc12";
    customerDetailsObject[0].itemDetailsCount=0;
    
    
    
    customerDetailsObject[1].customerName="Rahul";
    customerDetailsObject[1].userID="rah45";
    customerDetailsObject[1].password="1awe";
    customerDetailsObject[1].itemDetailsCount=0;

  
    
    categoryDetailsObject.musicCount = 3;
    categoryDetailsObject.electronicsCount = 4;
    categoryDetailsObject.bookCount = 5;
    
    
    categoryDetailsObject.music[0].name="The Dark Knight OST";
    categoryDetailsObject.music[0].cost=200;
    categoryDetailsObject.music[0].sku=10246;
    
    categoryDetailsObject.music[1].name="Rem";
    categoryDetailsObject.music[1].cost=300;
    categoryDetailsObject.music[1].sku=10247;
    
    categoryDetailsObject.music[2].name="Coldplay";
    categoryDetailsObject.music[2].cost=150;                                                                      
    categoryDetailsObject.music[2].sku=10248;
    
    
    categoryDetailsObject.electronic[0].name="Apple Iphone";
    categoryDetailsObject.electronic[0].cost=32000;
    categoryDetailsObject.electronic[0].sku=10249;
    
    categoryDetailsObject.electronic[1].name="Nokia 1661";
    categoryDetailsObject.electronic[1].cost=25200;
    categoryDetailsObject.electronic[1].sku=10250;
    
    categoryDetailsObject.electronic[2].name="Amazon Kindle";
    categoryDetailsObject.electronic[2].cost=10200;
    categoryDetailsObject.electronic[2].sku=10251;
    
    categoryDetailsObject.electronic[3].name="Rotomac pen";
    categoryDetailsObject.electronic[3].cost=1500;
    categoryDetailsObject.electronic[3].sku=10252;
    
    
    categoryDetailsObject.book[0].name="Let us C";
    categoryDetailsObject.book[0].cost=180;
    categoryDetailsObject.book[0].sku=10253;
    
    categoryDetailsObject.book[1].name="Fountain Head";
    categoryDetailsObject.book[1].cost=150;
    categoryDetailsObject.book[1].sku=10254;
    
    categoryDetailsObject.book[2].name="Godfather";
    categoryDetailsObject.book[2].cost=700;
    categoryDetailsObject.book[2].sku=10255;
    
    categoryDetailsObject.book[3].name="Tinkle";
    categoryDetailsObject.book[3].cost=20;
    categoryDetailsObject.book[3].sku=10256;
    
    categoryDetailsObject.book[4].name="Amar Chitra Katha";
    categoryDetailsObject.book[4].cost=20;
    categoryDetailsObject.book[4].sku=10257;
    
    while(true)
    {
    login();
    

    categoryDetailsObject.chooseCategory();
    }
}
its still the same its not showing anything in the view cart , after i choose the items from the list
It took me more than 1/2 hour to revamp your menu. Do you understand what I did and why i did it.

No I didn't touch your card problem. Like I mentioned before your ...AddToCart() functions can be easily incorporated into customerDetails like viewCart() alread is.
Topic archived. No new replies allowed.