You've asked a question worth a book's answer.
http://www.cplusplus.com/forum/beginner/160158/ wrote:
I need to create a point of sale system and a inventory. The point of sake system should reduce the inventory when a(n) item/s are purchased. It should save the inventory inputted that when you exit the program it is still there, also it should print(display on screen) a receipt showing the item, quantity and cost of the items/s purchased. |
http://www.cplusplus.com/forum/general/160149/ wrote:
How do you create a point of sale system program that deducts the inventory when one/more items are purchased?
Last edited on Mar 22, 2015 at 10:59am |
You have failed to understand some basic protocol about asking questions on the forum.
Everyone here is doing this out of their free time. Few of us have the desire to analyse twelve hundred line program without any concrete indication of what it is you are looking for.
If you want help, don't just post your homework assignment and expect us to jump to it.
and you guys refuse to reply? |
It looks to me like
TariqNeaj has made a pretty valiant effort at trying to narrow down your question to something reasonably answerable.
It is not just on the forum that you'll find this kind of attitude. When you ask for help, you must provide help yourself. Otherwise I could just go to Google and type "42" and get whatever answer I am looking for.
For your problem, I have not taken too hard a look at your code, but there are a few things you need to work on.
The primary, and most important, is the organization of your data. Each datum should be a record (or
struct) containing information about your inventory items. You have done this partly, but then discovered difficulty in managing all the different genres of inventory types. Why not simply make the genre a part of the inventory?
1 2 3 4 5 6
|
struct MovieItem
{
char genre[ 30 ];
char name[ 30 ];
int number_in_stock;
};
|
Now it is very easy to read and write inventory items from/to file. (Make some functions to do that.)
The next issue I think you ought to consider is that you are using a lot of C I/O stuff when you should be sticking to C++ I/O handling. The C stuff will mess you up. Stick to C++.
It will also help you to use a C++
std::deque to store your inventory.
|
typedef std::deque <MovieItem> MovieInventory;
|
1 2 3 4 5 6 7 8 9 10
|
int main()
{
MovieInventory inventory;
read_inventory_from_file( "movies.txt", inventory );
main_menu( inventory );
save_inventory_to_file( "movies.txt", inventory );
return 0;
}
|
Now you need to choose a file format for your inventory items. How about three lines per item, like:
Cartoon
Cars
12
Action
Mission Impossible
3
Drama
Ex Machina
7 |
Reading and writing an inventory item is now pretty simple:
1 2 3 4 5 6 7 8 9 10
|
void write_inventory_to_file( std::string filename, MovieInventory& inventory )
{
std::ofstream f( filename );
for (auto item : inventory)
{
f << item.genre << "\n"
item.name << "\n"
item.number_in_stock << "\n\n";
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
void read_inventory_from_file( std::string filename, MovieInventory& inventory )
{
MovieItem item;
std::string genre, name;
std::ifstream f( filename );
while (true)
{
std::getline( f, genre );
std::getline( f, name );
f >> item.number_in_stock; f.ignore( 1000, '\n' );
if (!f) break;
if (genre.length() > 29) genre.resize( 29 );
genre.copy( item.genre, genre.length() );
item.genre[ genre.length() ] = '\0';
if (name.length() > 29) name.resize( 29 );
name.copy( item.name, name.length() );
item.name[ name.length() ] = '\0';
inventory.push_back( item );
}
}
|
Now that you have a standard container of movie items, you can easily look through it for any genre, by name, etc.
You can also add and remove items easily enough.
Hope this helps.