I'm currently trying to improve my knowledge of C++ and writing as many programs as I can. My latest program has given me a problem and I can't seem to overcome it. My initial program has come from a book I was reading and I am building on it to add more features. It consists of a an ordering system using structures for stock. At present everytime I start the program I have to populate the stock from scratch. Idealy I want to save the stock onto disk and load it everytime the program starts. I am starting by populating by hand and then saving this to disk as I assumed this would be the easier bit, however I am having a little troube with this function. After this, I am proposing to load the file back into the program within the structures and need help with this too although I have not yet made a start on this function. I have search all my books and the net, but am strugling to find information to help me with this. Perhaps it is there but I am not sure of what I am looking for. Hopefully someone here can help. For interest, here is my program at present: #include <iostream> #include <fstream> using namespace std; struct StockItem { int code; char desc[20]; float price; }; /*struct bill_line { StockItem product; float weight; float cost; }; struct bill { bill_line items[20]; int num_lines; };*/ void disp_menu(void); struct StockItem enter_data(); void see_stock(StockItem stock[125], int num_items); void save_data(StockItem save); main() { StockItem stock[125]; int ans; int num_items = 0; do { do { disp_menu(); cin >> ans; } while ((ans<1) || (ans>5)); switch (ans) { case 1: stock[num_items] = enter_data(); num_items++; break; case 2: see_stock(stock, num_items); break; case 3: save_data(*stock); break; default: break; } }while (ans!=6); return 0; system("PAUSE"); return 0; } void disp_menu() { cout << "\n*** The Corner Shop ***\n\n"; cout << "Select an option: \n\n"; cout << "\t1. Enter new stock " << endl; cout << "\t2. See current stock " << endl; cout << "\t3. Exit \n" << endl; cout << "option> "; } StockItem enter_data() { StockItem stock_item; cout << "\n\nWhat is the product code: "; cin >> stock_item.code; cout << "What is the product description: "; fflush(stdin); cin.getline(stock_item.desc, 20); cout << "What is the product price: "; cin >> stock_item.price; return (stock_item); } void see_stock(StockItem stock[125], int num_items) { int ctr; char ret; cout << "\n\nHere is the stock listing:\n\n"; for (ctr = 0; ctr < num_items; ++ctr) { cout << "Item " << ctr+1 << endl; cout << "Product Code: " << stock[ctr].code << endl; cout << "Product Description: " << stock[ctr].desc << endl; cout << "Price: " << stock[ctr].price << endl; cout << "------------------------------------------------\n"; } cout << "Press any key to return to the menu"; if (cin >> ret) //how do you make this work on a keystroke, and not requiring input then enter?? return; } //Problem section void save_data(StockItem save) { fstream fp; fp.open("items.txt", ios::in | ios::out); if (!fp) { cout << "\n*** Error opening file ***\n"; exit(0); } fp << save; fp.close(); } ----------------------------------------- Email provided by http://www.ntlhome.com/
I'd like to suggest that you "serialize" objects by overloading the stream operators << and >>. Although this might be regarded as a somewhat advanced approach, it works as a general design principle. Let me show a simple example: class A { private: int i, j, k; std::string foo; // Declare operators as friends, in order to give them // access to private members of A friend ostream& operator<< (ostream&, const A&); friend istream& operator>> (istream&, A&); public: // ... // // In general, implement classes rather than structs, // reflecting the basic priciple of encapsulation in // object oriented system development. Provide public // member functions that access private data. }; ostream& operator<< (ostream& os, const A& a) { return (os << i << ':' << j << ':' << k << ':' << foo); } istream& operator>> (istream& is, A& a) { // basically the inverse of operator<<, // often more difficult } Now, you can use these operators to save to and load from a file stream, like int main() { // ... A something; { std::ifstream file(FILENAME); file >> something; } // change somthing in something { std::ofstream file(FILENAME); file << something; } // ... } On Friday 19 December 2003 13:49, ged.suse@ntlworld.com wrote:
I'm currently trying to improve my knowledge of C++ and writing as many programs as I can. My latest program has given me a problem and I can't seem to overcome it.
My initial program has come from a book I was reading and I am building on it to add more features. It consists of a an ordering system using structures for stock.
At present everytime I start the program I have to populate the stock from scratch. Idealy I want to save the stock onto disk and load it everytime the program starts.
I am starting by populating by hand and then saving this to disk as I assumed this would be the easier bit, however I am having a little troube with this function.
After this, I am proposing to load the file back into the program within the structures and need help with this too although I have not yet made a start on this function.
I have search all my books and the net, but am strugling to find information to help me with this. Perhaps it is there but I am not sure of what I am looking for. Hopefully someone here can help.
For interest, here is my program at present:
#include <iostream> #include <fstream>
using namespace std;
struct StockItem { int code; char desc[20]; float price; };
/*struct bill_line { StockItem product; float weight; float cost; };
struct bill { bill_line items[20]; int num_lines; };*/
void disp_menu(void); struct StockItem enter_data(); void see_stock(StockItem stock[125], int num_items); void save_data(StockItem save);
main() { StockItem stock[125]; int ans; int num_items = 0;
do { do { disp_menu(); cin >> ans; } while ((ans<1) || (ans>5));
switch (ans) { case 1: stock[num_items] = enter_data(); num_items++; break; case 2: see_stock(stock, num_items); break; case 3: save_data(*stock); break; default: break; } }while (ans!=6);
return 0;
system("PAUSE"); return 0; }
void disp_menu() { cout << "\n*** The Corner Shop ***\n\n"; cout << "Select an option: \n\n"; cout << "\t1. Enter new stock " << endl; cout << "\t2. See current stock " << endl; cout << "\t3. Exit \n" << endl; cout << "option> "; }
StockItem enter_data() { StockItem stock_item;
cout << "\n\nWhat is the product code: "; cin >> stock_item.code; cout << "What is the product description: "; fflush(stdin); cin.getline(stock_item.desc, 20); cout << "What is the product price: "; cin >> stock_item.price;
return (stock_item); }
void see_stock(StockItem stock[125], int num_items) { int ctr; char ret;
cout << "\n\nHere is the stock listing:\n\n"; for (ctr = 0; ctr < num_items; ++ctr) { cout << "Item " << ctr+1 << endl; cout << "Product Code: " << stock[ctr].code << endl; cout << "Product Description: " << stock[ctr].desc << endl; cout << "Price: " << stock[ctr].price << endl; cout << "------------------------------------------------\n"; } cout << "Press any key to return to the menu"; if (cin >> ret) //how do you make this work on a keystroke, and not requiring input then enter?? return; }
//Problem section void save_data(StockItem save) { fstream fp;
fp.open("items.txt", ios::in | ios::out); if (!fp) { cout << "\n*** Error opening file ***\n"; exit(0); } fp << save; fp.close(); }
----------------------------------------- Email provided by http://www.ntlhome.com/
-- Ch
participants (2)
-
Christian Andersson
-
ged.suse@ntlworld.com