Basically I'm trying to use an array to store inventory data, and I'm a little (lot) lost with vectors and such. I feel a little guilty asking for help three times in a day, but alas the semester is winding down and I'm in a crunch.
My class (or should I use struct?) :?::
class Inventory {
string ItemName;
int ItemQty;
float ItemPrice;
};
Function to simply add an item, it's a mess because I'm trying everything I can think of:
void AddItem(vector <Inventory> *MyInventory, unsigned &key, int &i) {
// i = number of elements in the item
MyInventory->ItemName = "Blah";
MyInventory[i].ItemQty = 3;
MyInventory[i].ItemPrice = 3.50;
i++;
ClearScreen();
UpdateScreen(key);
}
Here are my errors:
In function `void AddItem(std::vector<Inventory, std::allocator<Inventory> >*, unsigned int&, int&)':
99 'class std::vector<Inventory, std::allocator<Inventory> >' has no member named 'ItemName'
100 'class std::vector<Inventory, std::allocator<Inventory> >' has no member named 'ItemQty'
101 'class std::vector<Inventory, std::allocator<Inventory> >' has no member named 'ItemPrice'
In function `void PerformFunction(unsigned int&, std::vector<Inventory, std::allocator<Inventory> >*, int&)':
132 cannot convert `std::vector<Inventory, std::allocator<Inventory> >**' to `std::vector<Inventory, std::allocator<Inventory> >*' for argument `1' to `void AddItem(std::vector<Inventory, std::allocator<Inventory> >*, unsigned int&, int&)'
I want to add item to the next index.
So how do I accomplish this?
Lastly, I want the screen to update when an item is added - I know I'm doing it wrong, and should probably use some kind of loop, but I'm not sure of pseudocode that would work. Right now it just goes to a function, flushes the screen, and re-displays the menu but that doesn't seem correct.
Thank you for assistance with my frustrations and newbieness :icon_redface:
Full code:
#include <iostream>
#include <conio.h> // for menu selection input
#include <vector> // arrays
using namespace std;
class Inventory {
string ItemName;
int ItemQty;
float ItemPrice;
};
int main() {
vector <Inventory> MyInventory;
int i=0;
unsigned key;
// function declarations
void InventoryOverview();
void MenuOptions(unsigned &key);
void PerformFunction(unsigned &key, vector <Inventory> *, int &i);
void ProgramTitle();
void AddItem(vector <Inventory> *, unsigned &key, int &i);
void ClearScreen();
void UpdateScreen(unsigned &key);
//
i = MyInventory.size();
ProgramTitle();
InventoryOverview();
MenuOptions(key);
PerformFunction(key, &MyInventory, i);
}
// function operations
void ClearScreen() {
system("CLS"); // using system is not good programming practice, but for the sake of this program it's needed
}
void ProgramTitle() {
// just for prettiness
cout << " [Inventory Management System]" << endl << endl;
}
void MenuOptions(unsigned &key) {
cout << "Select an option:" << endl;
cout << "[1] Load new inventory from file" << endl;
cout << "[2] Load additional inventory from file" << endl;
cout << "[3] Add item to inventory" << endl;
cout << "[4] Save inventory" << endl;
cout << "[5] Search inventory" << endl;
cout << "[6] List inventory" << endl;
cout << "[0] Exit";
// loop until correct key is inputted
do {
key = getch();
} while(key < 48 || key > 54);
}
void InventoryOverview() {
cout << "Inventory items: 0" << endl;
cout << "Inventory value: $0.00" << endl << endl;
}
void UpdateScreen(unsigned &key) {
ProgramTitle();
InventoryOverview();
MenuOptions(key);
}
void NewInventoryFromFile() {
}
void AddInventoryFromFile() {
}
void AddItem(vector <Inventory> *MyInventory, unsigned &key, int &i) {
// i = number of elements in the item
MyInventory->ItemName = "Blah";
MyInventory[i].ItemQty = 3;
MyInventory[i].ItemPrice = 3.50;
i++;
ClearScreen();
UpdateScreen(key);
}
void SaveInventory() {
}
void SearchInventory() {
}
void ListInventory() {
}
void PerformFunction(unsigned &key, vector <Inventory> *MyInventory, int &i) {
if(key == 48) { // 0
//cout << "Exit";
exit(1);
}
else if(key == 49) { // 1
//cout << "New inventory from file";
NewInventoryFromFile();
}
else if(key == 50) { // 2
//cout << "Load additional inventory from file";
AddInventoryFromFile();
}
else if(key == 51) { // 3
//cout << "Add item to inventory";
AddItem(&MyInventory, key, &i);
}
else if(key == 52) { // 4
//cout << "Save inventory";
SaveInventory();
}
else if(key == 53) { // 5
//cout << "Search inventory";
SearchInventory();
}
else if(key == 54) { // 6
//cout << "List inventory";
ListInventory();
}
}