Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8777217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:13:47+00:00 2026-06-13T19:13:47+00:00

So i want to do inventory program that uses a vector of class items

  • 0

So i want to do inventory program that uses a vector of class items in it. So far my code looks something like this:

#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Item{
private:
  string month;
  string name;
  float volume;
  float sales;
public:
  void print();
  void print2();
  void sale();
  void report();
  void getdata();
  void setname (string itemname){name=itemname;}
  void setID(int setID){ID=setID;}
};
void Item::print(){
  cout<<"Name    : "<<name<<endl;
  cout<<"ID      : "<<ID<<endl;
}
void Item::print2(){
  vector<Item>*::iterator i;
    for (i=list.begin(); i !=list.end(); ++i){
      i->print();
  }
/*void Item::sale(){
  vector<Item> }

void Item::report(){
}*/
void Item::getdata(){
  vector<Item> items;
  string name;
  int ID;
  Item *a;
  for (int n=0; n<2; n++){
    cout<<"Enter name:"<<endl;
    getline(cin, name);
    cout<<"Enter ID: "<<endl;
    cin>>ID;
    a = new Item;
    a->setname(name);
    a->setID(ID);
    items.push_back(*a);
    cin.get();
  }
}

int main(){
  cout<<"0. Exit \n1. Item Sale \n2. Daily Report \n3. Weekly Check \n4. Monthly Update \n5. Load Inventory File \n6. S\
ort file by Key \n7. Save inventory to a file \n8. Add a New Item \n9. Edit an Item \n10. Delete an item\n";
  int x;
  cin>>x;
  switch(x){
  case 0:
    exit(0);
    break;
  case 1:
    sale();
    break;
  case 2:
    break;
  case 8:
    getdata();
    break;
  default:
    cout<<"Wrong choice!\n";
    break;
  }

the program doesn’t compile because getdata() in case 8 doesn’t call the function, why is that? Another question is so if i have stored data in getdata() function how can i use that data vector data in other function by dereferencing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:13:48+00:00Added an answer on June 13, 2026 at 7:13 pm

    I would recommend splitting your object design into two parts: one to represent an Item, and one to represent the Inventory.

    Item:

    class Item
    {
    public:
      void print() const;
      void sale();
      void report();
      void setname (string itemname){name=itemname;}
      void setID(int setID){ID=setID;}
    
    private:
      string month;
      string name;
      int ID;
      float volume;
      float sales;
    };
    
    void Item::print() const
    {
      cout<<"Name    : "<<name<<endl;
      cout<<"ID      : "<<ID<<endl;
    }
    

    Inventory:

    class Inventory
    {
    public:
      void print() const;
      void report();
      void getdata();
    
    private:
      std::vector<Item> items;
    };
    
    void Inventory::print() const
    {  
      for (
        vector<Item>::const_iterator iter = list.begin();
        iter != items.end();
        ++iter)
      {
          iter->print();
      }
    }
    

    .

    void Inventory::getdata()
    {
      string name;
      int ID;
      for (int n=0; n<2; n++)
      {
        cout<<"Enter name:"<<endl;
        getline(cin, name);
        cout<<"Enter ID: "<<endl;
        cin>>ID;
        Item item;
        item.setname(name);
        item.setID(id);
        items.push_back(item);
        cin.get();
      }
    }
    

    And here’s what your main would look like:

    int main()
    {
      Inventory inventory;
      cout
          << "0. Exit \n"
          << "1. Item Sale \n"
          << "2. Daily Report \n"
          << "3. Weekly Check \n"
          << "4. Monthly Update \n"
          << "5. Load Inventory File \n"
          << "6. Sort file by Key \n"
          << "7. Save inventory to a file \n"
          << "8. Add a New Item \n"
          << "9. Edit an Item \n"
          << "10. Delete an item\n";
      int x;
      cin>>x;
      switch(x)
      {
      case 0:
        exit(0);
        break;
      case 1:
        sale();
        break;
      case 2:
        inventory.report();
        break;
      case 8:
        inventory.getdata();
        break;
      default:
        cout<<"Wrong choice!\n";
        break;
      }
    }
    

    Notice that you need an object of the class in order to call a member function. getdata() doesn’t mean anything without the object on which it acts.

    Edit: You asked “how would you iterator through the vector to find specific ID for example?”

    We already see how to iterate through to call print on every member. All we really need to do is only call print sometimes.

    First, you need a way to tell the ID of an Item. Add this after setID(…) in Item:

    int getID() const {return ID;}
    

    Now you can check this value while iterating:

    void Inventory::find
    (
      const int ID_
    ) const
    {
      bool found = false;
      for (
        vector<Item>::const_iterator iter = items.begin();
        iter != items.end();
        ++iter)
      {
        if (iter->getID() == ID_)
        {
          cout << "Found item " << ID_ << ": \n";
          iter->print();
          found = true;
        }
      }
      if (!found)
      {
        cout << "No items matching " << ID_ << " found." << endl;
      }
    }
    

    You can see a working example here: http://codepad.org/w1MwGCYo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so i am doing a inventory program. I have a vector of class items
want to rewrite urls like site.com/software to wp-content/themes/dir/software.php and something is not working.. Here's
I want to try making a simple game engine. Just something that handles states,
In my rails app, I have a long Inventory form that I want to
The problem: I have an inventory table, and a table listing items that are
I want to change via web service a remote inventory, I know that via
I want to create an old school simplistic Text based RPG. No inventory, no
want to know why String behaves like value type while using ==. String s1
Want to code a key pad for an calculator. What I want to make
I want to access a inventory system which is accessible through webservice, What is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.