File Input Output Streams

I am trying to get this program to take information from a text file and input it into the list at the very beginning. At the end of it I am trying to make it output the information back to the same text file. Both parts seem to not be working. There are some other functions that I have taken out so that the program fits.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <cstdlib>
#include <climits>
#include <fstream>
using namespace std;

class Product {
public:
    Product();
    Product(string name, double price, int qty);
    string getName() const;
    double getPrice() const;
    int getQuantity() const;
    double getValue() const;
    void setName(string newName);
    void setPrice(double newPrice);
    void setQuantity(int newQuantity);
    void print() const;
    void read();
    string toString();
    void read(ifstream& fin);
    void write(ofstream& fout) const;
private:
    string name;
    double price;
    int quantity;
};

const int NAME_WIDTH = 20;
const int PRICE_WIDTH = 10;
const int QTY_WIDTH = 10;
const int VALUE_WIDTH = 14;

double readDouble(string prompt);

void saveData(const vector<Product>& store, string fileName);
Product::Product() {
    price = 0.0;
    quantity = 0;
}

Product::Product(string name, double price, int qty) {
    setName(name);
    setPrice(price);
    setQuantity(qty);
}


void Product::read() {
    cout << "Enter the name of the product: ";
    cin >> ws;
    getline(cin, name);
    // Making use of the extra credit function
    setPrice(readDouble("Enter the price for a " + name + ": "));
    setQuantity((int) readDouble("Enter the initial inventory: "));
}

void Product::read(ifstream& fin) {
    fin >> name;
    fin >> price;
    fin >> quantity;
}

void Product::write(ofstream& fout) const {
    fout.open("products.txt");
    if (fout.fail()) {
        cout << "Output file failed to open.\n";
        exit(-1);
    }
    cout << "Writing this to a file.\n";
    fout << name;
    fout << price;
    fout << quantity;
    fout.close();
}

void listProducts(vector<Product>& store);

void addProduct(vector<Product>& store);

void deleteProduct(vector<Product>& store);

void sellProduct(vector<Product>& store);

void restockProduct(vector<Product>& store);

int getProductIndex(vector<Product>& store);

int readIntInRange(string prompt, int min, int max);

void loadData(vector<Product>& store, string fileName);

void saveData(const vector<Product>& store, string fileName);

const int INVENTORY = 1;
const int ADD_PROD = 2;
const int DEL_PROD = 3;
const int SELL_PROD = 4;
const int RESTOCK = 5;
const int EXIT = 0;

const int MAX_ADD = 1000;


int main() {

    const double MILK_PRICE = 3.95;
    const int MILK_QTY = 40;
    const double BREAD_PRICE = 2.99;
    const int BREAD_QTY = 30;
    const double CHEESE_PRICE = 4.98;
    const int CHEESE_QTY = 20;
    Product milk("Milk", MILK_PRICE, MILK_QTY);
    Product bread("Bread", BREAD_PRICE, BREAD_QTY);
    Product cheese;
    cheese.setName("Cheese");
    cheese.setPrice(CHEESE_PRICE);
    cheese.setQuantity(CHEESE_QTY);

    ifstream fin;
    ofstream fout;
    fin.open("products.txt");
    vector<Product> store;
    store.push_back(milk);
    store.push_back(bread);
    store.push_back(cheese);
    loadData(store, "products.txt");

    cout << "Welcome to the Virtual Store Manager\n";
    int choice = 1;
    while (choice != 0) {
    string msg = "\nPlease choose one of the following operations:\n";
    msg += "0. Exit program\n";
    msg += "1. Report inventory\n";
    msg += "2. Add a new product\n";
    msg += "3. Delete a product\n";
    msg += "4. Sell a product\n";
    msg += "5. Restock a product\n";
    msg += "Choice (0-5): ";
    choice = readIntInRange(msg, 0, 5);
        if (choice == INVENTORY) {
            listProducts(store);
        } else if (choice == ADD_PROD) {
            addProduct(store);
        } else if (choice == DEL_PROD) {
            deleteProduct(store);
        } else if (choice == SELL_PROD) {
            sellProduct(store);
        } else if (choice == RESTOCK) {
            restockProduct(store);
        } else if (choice != EXIT) {
            cout << "\nInvalid choice!\n";
        }
    }
    cout << "\nGoodbye!\n";
    saveData(store, "products.txt");

    fin.close();
    fout.close();

    return 0;
}

int getProductIndex(vector<Product>& store) {
    listProducts(store);
    int pos = readIntInRange("Enter the number of the product: ",
        1, store.size());
    return pos - 1;
}

void sellProduct(vector<Product>& store) {
    cout << "\nSelling a product:";
    int pos = getProductIndex(store);
    Product prod = store[pos];
    int inventory = prod.getQuantity();
    if (inventory > 0) {
        prod.setQuantity(inventory - 1);
        store[pos] = prod;
    } else {
        cout << "Sorry, " << prod.getName() << " is sold out.\n";
    }
}

void restockProduct(vector<Product>& store) {
    cout << "\nRestocking a product:";
    int pos = getProductIndex(store);
    Product prod = store[pos];
    int inventory = prod.getQuantity();
    int numItems = readIntInRange("Number of items to add: ", 0, MAX_ADD);
    inventory += numItems;
    prod.setQuantity(inventory);
    store[pos] = prod;
}

void deleteProduct(vector<Product>& store) {
    cout << "\nDeleting a product:";
    int pos = getProductIndex(store);
    for (unsigned i = pos; i < store.size() - 1; i++) {
        store[i] = store[i + 1];
    }
    store.pop_back();
}

void loadData(vector<Product>& store, string filename) {
    ifstream fin(filename.c_str());
    if (fin.fail()) {
        cout << "Input file failed to open.\n";
        exit(-1);
    }
    string line;
    fin >> ws;
    while(getline(fin, line)) {
        Product temp;
        temp.read(fin);
        if (fin.good()) {
            store.push_back(temp);
        }
    }
    fin.close();
}

void saveData(const vector<Product>& store, string filename) {
    ofstream fout(filename.c_str());

    if (fout.fail()) {
        cout << "Output file failed to open.\n";
        exit(-1);
    }
    for (unsigned int i = 0; i < store.size(); i++) {
        store[i].write(fout);
    }
    fout.close();
}

void addProduct(vector<Product>& store) {
    cout << "\nAdding a new product:\n";
    Product prod;
    prod.read();
    store.push_back(prod);
}


void listProducts(vector<Product>& store) {
    const int HASH_WIDTH = 3;
    cout << endl;
    cout << setw(NAME_WIDTH + HASH_WIDTH) << left << " # Name"
         << setw(PRICE_WIDTH) << right << "Price"
         << setw(QTY_WIDTH) << right << "Qty"
         << setw(VALUE_WIDTH) << "Value" << endl;
    for (unsigned num = 0; num < store.size(); num++) {
        cout << setw(2) << right << (num + 1) << " ";
        store[num].print();
    }
}

double readDouble(string prompt) {
    double input = 0.0;
    bool more = true;
    while (more) {
        cout << prompt;
        cin >> input;
        if (cin.fail()) {
            cout << "Error: enter numbers only!\n";
            cin.clear();
            cin.ignore(INT_MAX, '\n');
        } else {
            more = false;
        }
    }

    return input;
}
int readIntInRange(string prompt, int min, int max) {
    int input = 0;
    bool more = true;
    while (more) {
        cout << prompt;
        cin >> input;
        if (cin.fail()) {
            cout << "Error: enter integer numbers only!\n";
            cin.clear();
            cin.ignore(INT_MAX, '\n');
        } else if (input < min || input > max) {
            cout << "Error: enter integer numbers between " << min
                 << " and " << max << ".\n";
        } else {
            more = false;
        }
    }
    return input;
}


Topic archived. No new replies allowed.