Urgent how to add value into an object

guys i am working on a shopping program , the user data such as id and password are given which is declared in main function and when the user enters his id and password the validity of that entered data is checked , once the user logins in he choose the items (music,electronic,book) . i dont know how to put the selected item into add to cart menu so when user selects the items inside the categories that item should be added and in the view cart it should show the items which has been selected . i have declared an object called item is the class name .. item itemDetailsObject , i have declared class called customerDetails i have declared two objects at end of this class . so when the user selects the item that item should be declared in itemDetailsObject , so how to put a value to an object i am stuck in that .
If you want to store some item in class itemDetailsObject you need to make a vector. You could also use map to store an item with a key if thats what you wanted.
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
class Item{
      private:
         string name;
      
      public:
         string text()
         {
              return name;
         }
         
         Item(string name)
         {
              this->name = name;
         }
};

int main(){
vector< Item> items;
    Item product1("Milk");
    Item product2("Cheese");
    Item product3("Meat");
    
    items.push_back(product1);
    items.push_back(product2);
    items.push_back(product3);
    
    for(int i = 0; i < 3; i++)
    {
         cout << items[i].text() << endl;
    }
}

Last edited on
#include <iostream>


using namespace std;

void validity(string ,string );

class item
{
public:

string name;
int cost;
int sku;

void displayDetails();

};

int ch;

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

}

class customerDetails
{
public:

item obj;

string customerName;
string userID;
string password;

void input();
void validity();

}obj[2];


void input()
{
string id;
string pass;

cout<<"Login ID :"<<endl;
cin>>id;

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

validity(id,pass);

}
void validity(string id,string pass)
{
string correctMatch;

for (int i=0; i<=1; i++)
{
if (id==obj[i].userID && pass==obj[i].password)
{
cout<<"\nWelcome"<<" "<<obj[i].customerName<<endl;
correctMatch=i;
}
}

}


class category
{
public:

item itemobject;

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();
void addToCart();

};

void category::chooseCategory()
{
int select;

cout<<"\nChoose a category\n"<<endl;

cout<<"1.Music"<<endl;
cout<<"2.Electronic"<<endl;
cout<<"3.Books"<<endl;
cout<<"4.Viewcart\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<<"Cart List"<<endl;
this->addToCart();

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;

cin>>options;

switch (options)
{
case 1:
this->addToCart();
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;

cin>>options;

switch (options)
{
case 1:
this->addToCart();
break;

case 2:
this->chooseElectronicItem();
break;

case 3:
this->chooseCategory();
break;

default:
displayElectronicItemDetails();
break;
}

}


void category::chooseBookItem()
{
int ch;

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.Back"<<endl;
cout<<"\n2.MainPage"<<endl;

cin>>options;
switch (options)
{
case 1:
this->chooseBookItem();
break;

case 2:
this->chooseCategory();
break;

default:
displayBookItemDetails();
break;
}

}

void category::addToCart()
{



int selection;

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

cin>>selection;
switch (selection)
{
case 1:
this->chooseCategory();
break;

case 2:
this->displayMusicItemDetails();
break;

default:
break;

}

}

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

category object;


obj[0].customerName="David";
obj[0].userID="dav2";
obj[0].password="abc12";

obj[1].customerName="Rahul";
obj[1].userID="rah45";

obj[1].password="1awe";


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


object.music[0].name="Dark Knight OST";
object.music[0].cost=200;
object.music[0].sku=10246;

object.music[1].name="Rem";
object.music[1].cost=300;
object.music[1].sku=10247;

object.music[2].name="Coldplay";
object.music[2].cost=150;
object.music[2].sku=10248;


object.electronic[0].name="Apple Iphone";
object.electronic[0].cost=32000;
object.electronic[0].sku=10249;

object.electronic[1].name="Nokia 1661";
object.electronic[1].cost=25200;
object.electronic[1].sku=10250;

object.electronic[2].name="Amazon Kindle";
object.electronic[2].cost=10200;
object.electronic[2].sku=10251;

object.electronic[3].name="Rotomac pen";
object.electronic[3].cost=1500;
object.electronic[3].sku=10252;


object.book[0].name="Let us C";
object.book[0].cost=180;
object.book[0].sku=10253;

object.book[1].name="Fountain Head";
object.book[1].cost=150;
object.book[1].sku=10254;

object.book[2].name="Godfather";
object.book[2].cost=700;
object.book[2].sku=10255;

object.book[3].name="Tinkle";
object.book[3].cost=20;
object.book[3].sku=10256;

object.book[4].name="Amar Chitra Katha";
object.book[4].cost=20;
object.book[4].sku=10257;

input();

object.chooseCategory();

}
#include <iostream>


using namespace std;

void validity(string ,string );

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

};

int ch;

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

}

class customerDetails
{
public: 
   
    item obj[50];
  
    string customerName;
    string userID;
    string password;
    
    void input();
    void validity();
    
}obj[2];


void input()
{
    string id;
    string pass;

    cout<<"Login ID :"<<endl;
    cin>>id;

    cout<<"\nLogin Password :"<<endl;
    cin>>pass;
          
    validity(id,pass);

}
void validity(string id,string pass)
{
    string correctMatch;
    
    for (int i=0; i<=1; i++)
    {
        if (id==obj[i].userID && pass==obj[i].password)
        {
            cout<<"\nWelcome"<<" "<<obj[i].customerName<<endl;
            correctMatch=i;
        }
    }

}


class category
{
public:
    
    item itemobject;
    
    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();
    void addToCart();
      
};

void category::chooseCategory()
{
    int select;
    
    cout<<"\nChoose a category\n"<<endl;

    cout<<"1.Music"<<endl;
    cout<<"2.Electronic"<<endl;
    cout<<"3.Books"<<endl;
    cout<<"4.Viewcart\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<<"Cart List"<<endl;
            this->addToCart();
            
        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;
    
    cin>>options;
    
    switch (options) 
    {
        case 1:
            this->addToCart();
            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;
    
    cin>>options;
    
    switch (options) 
    {
        case 1:
            this->addToCart();
            break;
            
        case 2:
            this->chooseElectronicItem();
            break;
            
        case 3:
            this->chooseCategory();
            break;
            
        default:
            displayElectronicItemDetails();
            break;
    }

}
    

void category::chooseBookItem()
{
    int ch;
    
    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.Back"<<endl;
    cout<<"\n2.MainPage"<<endl;

    cin>>options;
    switch (options) 
    {
        case 1:
            this->chooseBookItem();
            break;
            
        case 2:
            this->chooseCategory();
            break;
            
        default:
            displayBookItemDetails();
            break;
    }

}

void category::addToCart()
{
    
    
    
    int selection;
    
    cout<<"\n1.Checkout"<<endl;
    cout<<"\n2.Back"<<endl;
    
    cin>>selection;
    switch (selection) 
    {
        case 1:
            this->chooseCategory();
            break;
            
        case 2:
            this->displayMusicItemDetails();
            break;
            
        default:
            break;
            
    }  
    
}

int main (int argc, const char * argv[])
{
    
   category object;
    
    
    obj[0].customerName="David";
    obj[0].userID="dav2";
    obj[0].password="abc12";
  
    obj[1].customerName="Rahul";
    obj[1].userID="rah45";

obj[1].password="1awe";
    
    
    object.musicCount = 3;
    object.electronicsCount = 4;
    object.bookCount = 5;
    
    
    object.music[0].name="Dark Knight OST";
    object.music[0].cost=200;
    object.music[0].sku=10246;
    
    object.music[1].name="Rem";
    object.music[1].cost=300;
    object.music[1].sku=10247;
    
    object.music[2].name="Coldplay";
    object.music[2].cost=150;                                                                      
    object.music[2].sku=10248;
    
    
    object.electronic[0].name="Apple Iphone";
    object.electronic[0].cost=32000;
    object.electronic[0].sku=10249;
    
    object.electronic[1].name="Nokia 1661";
    object.electronic[1].cost=25200;
    object.electronic[1].sku=10250;
    
    object.electronic[2].name="Amazon Kindle";
    object.electronic[2].cost=10200;
    object.electronic[2].sku=10251;
    
    object.electronic[3].name="Rotomac pen";
    object.electronic[3].cost=1500;
    object.electronic[3].sku=10252;
  
    
    object.book[0].name="Let us C";
    object.book[0].cost=180;
    object.book[0].sku=10253;
    
    object.book[1].name="Fountain Head";
    object.book[1].cost=150;
    object.book[1].sku=10254;
    
    object.book[2].name="Godfather";
    object.book[2].cost=700;
    object.book[2].sku=10255;
    
    object.book[3].name="Tinkle";
    object.book[3].cost=20;
    object.book[3].sku=10256;
    
    object.book[4].name="Amar Chitra Katha";
    object.book[4].cost=20;
    object.book[4].sku=10257;
        
    input();
    
    object.chooseCategory();
        
}
Last edited on
thats the program i am working on the input and validity function are ok well i have done it in different way my problem is since user can select any number of items to his cart when he selects an item from any category it should be transferred to add to cart menu suppose in music section when user selects 2. details of Rem , sku number and cost should get transferred to add to cart section and when the user selects view cart whatever the user has selected in add to cart should show up in view cart , btw the user can select 2 items from music category , 3 items from electronic and 4 items from book category so when user selects view cart all the items 2+3+4 total 7 items should show up .

i dont know how to add value to itemObj since it is in customerDetails class and how to use that in addToCart function . btw itemObj has 50 elements so that user can select 50 items and put it in his add to cart option .
Topic archived. No new replies allowed.