ged.suse@ntlworld.com wrote:
Thanks to all who have helped so far :)
I don't have much time left to get this program running, so I have opted to drop the functions and try and get it running in the main()..... ....however, I still can't get it to do what I want.
Can anyone PLEASE help me with this. I would be very grateful, I need to get this program running.
Email provided by http://www.ntlhome.com/
It ain't fool and bug proof. Try this: #include <fstream> #include <iostream> #include <cstdlib> #include <string.h> #include <stdlib.h> #include <stdio.h> #define MAX_STOCKITEM 125 class StockItem { public: StockItem() {;} StockItem(int c, char *d, float p) {code = c; strcpy(desc, d); price = p;} ~StockItem() {;} void Set_Code(int c) {code = c;} void Set_Desc(char *d) {strcpy(desc,d);} void Set_Price(float p) {price = p;} int Get_Code() {return code;} void Get_Desc(char *d) {strcpy(d,desc);} float Get_Price() {return price;} private: protected: int code; char desc[20]; float price; }; ifstream& operator>>(ifstream &fis, StockItem &stock) { int c; char d[20]; float p; fis >> c; stock.Set_Code(c); fis >> d; stock.Set_Desc(d); fis >> p; stock.Set_Price(p); return fis; } ofstream& operator<<(ofstream &fos, StockItem &stock) { int c; char d[20]; float p; c = stock.Get_Code(); stock.Get_Desc(d); p = stock.Get_Price(); fos << c << std::endl; fos << d << std::endl; fos << p; return fos; } ostream& operator<<(ostream &os, StockItem &stock) { int c; char d[20]; float p; c = stock.Get_Code(); stock.Get_Desc(d); p = stock.Get_Price(); os << "Product Code: " << c << endl; os << "Product Description: " << d << endl; os << "Price: " << p << endl; return os; } istream& operator>>(istream &is, StockItem &stock) { int c; char d[20]; float p; std::cout << "\n\nWhat is the product code: "; is >> c; stock.Set_Code(c); std::cout << "What is the product description: "; is.ignore(); is.getline(d, 20); // a deiscription of 21 will crash program. stock.Set_Desc(d); std::cout << "What is the product price: "; is >> p; stock.Set_Price(p); return is; } void disp_menu() { std::cout << "\n*** The Corner Shop ***\n\n"; std::cout << "Select an option: \n\n"; std::cout << "\t1. Enter new stock \n"; std::cout << "\t2. See current stock \n"; std::cout << "\t3. Save stock to disk \n"; std::cout << "\t4. Load stock from disk \n"; std::cout << "\t5. Exit \n\n"; std::cout << "option> "; } void enter_data(StockItem stock[MAX_STOCKITEM], int index) { std::cin >> stock[index]; } void see_stock(StockItem stock[MAX_STOCKITEM], int num_items) { char ret; std::cout << "\n\nHere is the stock listing:\n\n"; for(int index = 0; index < num_items; index++){ std::cout << "Item " << index << std::endl; std::cout << stock[index]; std::cout << "------------------------------------------------\n"; } std::cout << "Press any key to return to the menu"; std::cin >> ret; } void save_data(StockItem stock[MAX_STOCKITEM], int size) { std::ofstream fout("items.txt"); if(!fout) { std::cout << "\n*** Error opening file ***\n"; } for(int index = 0; index < size; index++) { cout << stock[index]; fout << stock[index]; if (index < (size -1)) fout << std::endl; } fout.close(); } int load_data(StockItem stock[MAX_STOCKITEM]) { int index; std::ifstream fin("items.txt"); if(!fin) { std::cout << "\n*** Error opening file ***\n"; } index = 0; while ((!fin.eof()) && (index < MAX_STOCKITEM)) { fin >> stock[index]; cout << stock[index]; index++; } fin.close(); return index; } int main() { StockItem stock[MAX_STOCKITEM]; int ans; int num_items = 0; do { do { disp_menu(); std::cin >> ans; } while((ans < 1) || (ans > 6)); switch (ans) { case 1: enter_data(stock, num_items); num_items++; ans = 0; break; case 2: see_stock(stock, num_items); ans = 0; break; case 3: save_data(stock, num_items); ans = 0; break; case 4: num_items = load_data(stock); ans = 0; break; default: break; } } while(ans != 5); return 0; }