Urgent!! Shopping program

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

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

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

    [b]cout<<"\nTotal Cost "<<itemDetailsObject[0].cost+itemDetailsObject[1].cost+itemDetailsObject[2].cost<<endl;[/b]

    int selection;

    cout<<"\n1.Checkout"<<endl;
    cout<<"\n2.Back"<<endl;

    cin>>selection;
    switch (selection) 

    {
        case 1:
            categoryDetailsObject.chooseCategory();
            break;
        case 2:
            categoryDetailsObject.displayMusicItemDetails();
            break;
        default:viewCart();
            break;
    }  
}



this is part of a shopping program which i have written i wanted to calculate the total cost in view cart part so i couldnt figure out how to add the total cost ,there can be any number of items which could be added in add to cart menu btw itemDetailsObject[50] the code which i have blocked is one way but if there are 50 items to be added that method wont be ideal .
Last edited on
You should read up on loops:
http://www.cplusplus.com/doc/tutorial/control/

However, I see a for loop in your code, so you seem to already know about them.
What's the problem?
the problem is i have to add up the total cost of the items one way is the way which i have added the cost of all the items ..but when there are 30 items costs to be added the method which i have done will be too lengthy .
i know its simple one but i am so stressed out dont know how to do it ..
Create a variable for the total, go through the items using a for loop and add their prices to the total.
1
2
3
4
5
6
int i = 0;
    for (int i= 0; i< itemDetailsCount; i++)
    {
        total_cost += itemDetailsObject[i].cost;
    }
    cout<<"\nTotal Cost "<<total_cost<<endl;




like this ?
Topic archived. No new replies allowed.