Simple reading from txt file

I need some help I have to write a program:
[I am running a store and want to computerize the inventory]

1. Create a .txt file [store.txt]
2. declare a stream
3. open your text file
4. check that the open did not fail,
5. display the name of the store
6. read the inventory from a file into arrays-the item number, quanitity of the item, and name of the item. The program should read the whole file, no matter how many items are in it.
7.In a loop until the user enters 'STOP'
1. Display the item number and name on the screen
2. Ask the user what he wants to buy and how many. If the user orders more than in inventory - error message
8. close the stream and print the final inventory.


So basically I have this txt file
1
2
3
4
5
//item number, quantity of item, name of item
105 10 bread
109 11 butter
115 6 fish
122 4 milk


I have to basically make a program that reads from the text file, and removes what the user wants while printing what they want.

If anyone can help that would be great, thanks a lot.
What do you have so far? (Program coding wise)
Last edited on
I don't have anything, I don't know how to start this.
So far I have this

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
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <fstream>

using namespace std;


void Datain(ifstream &inData, char Item[], double &Begin_qty, char Name[]);
void Dataout(ofstream &outData, char Item[], double &Begin_qty, char Name[]);


int main()

{

	ifstream inData;
	char Item[13];
	double Begin_qty;
	char Name[10];
	ofstream outData;

	char file[15];
	cout << "Please enter Filename :";
	cin >> file;
	inData.open(file);

	char ofile[15];
	cout << "Please enter Filename to save :";
	cin >> ofile;
	outData.open(ofile);
	system("cls");

	while (!inData.fail())
	{
		Datain(inData, Item, Begin_qty, Name);
		if (!inData.fail())
			Dataout(outData, Item, Begin_qty, Name);
	}

	inData.close();
	outData.close(); //flush output file to disk.

	return 0;
}



void Datain(ifstream &inData, char month[], char Item[], double &Begin_qty, char Name[])
{
	inData >> Item >> Begin_qty >> Name;
}


void Dataout(ofstream &outData, char month[], char Item[], double &Begin_qty, char Name[])
{

	outData << left << setw(15) << "ITEM" << setw(11) << "BEGIN_QTY" << setw(12) << "NAME" << setw(12) << endl;
	outData << setw(15) << Item << setw(11) << Begin_qty << setw(12) << Name << setw(12)<< endl;

}
I tried to google to find some help and all I could do is this:

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
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

struct InventoryRecord
{
	string name;   // inventory item name
	int qty;       // how many are in stock
	int number;   // the item number
};

// const for the max size of the record array
const int MAX_SIZE = 9;

// function prototypes
void addData(InventoryRecord list[], int& size);
void dispData(const InventoryRecord list[], int size);
void saveFile(const InventoryRecord list[], int size);
void openFile(InventoryRecord list[], int& size);
void buyItem(InventoryRecord list[], int& size);
char getMenuResponse();


int main(int argc, char *argv[])
{
	InventoryRecord recList[MAX_SIZE];
	int numOfRecs = 0;
	bool run = true;
	do
	{
		cout << "Mike's Fish Market - " << numOfRecs << " items in stock" << endl;
		switch (getMenuResponse())
		{
		//case 'A': addData(recList, numOfRecs); break;
		case 'D': dispData(recList, numOfRecs); break;
		case 'O': openFile(recList, numOfRecs); break;
		case 'B': buyItem(recList, numOfRecs); break;
		case 'F': saveFile(recList, numOfRecs); break;
		case 'S': run = false; break;
		default: cout << "That is NOT a valid choice" << endl;
		}
	} while (run);
	cout << endl << "Program Terminated" << endl;

	// system("PAUSE"); // Program exits immediatly upon "Quit" if commented out
	return EXIT_SUCCESS;
}

// Task:     Allow data entry of one inventory item
// Accepts:  References to the inventory array and its size
// Returns:  Nothing
// Modifies: The array and size 'actual parameter'
// NOTE:     Could be modified to allow entry of more than one item
void addData(InventoryRecord list[], int& size)
{
	InventoryRecord tmp; // declare a temp item that we will load before putting in the array
	char response;
	char str[256]; // needed for cin.getline; we are going to use a char array
	if (size < MAX_SIZE) {
		system("cls");
		cout << "Enter Inventory Records" << endl << endl;
		cout << "Name:     ";
		// Get up to 256 characters from the keyboard including white space.
		// Stop reading if encounter the \n first. If there's any chance of 
		// more than 256 characters you will have to clean up cin with
		// cin.ignore before the next input.
		cin.getline(str, 256, '\n'); // for char arrays; different from the other getline
		tmp.name = str;
		cout << "Quantity: ";
		cin >> tmp.qty;
		cout << "Item Number:     ";
		cin >> tmp.number;
		cout << endl;
		// see if this record should be added to the array
		cout << "Add the record to inventory? (y/n) ";
		cin >> response;
		if (toupper(response) == 'Y')
			list[size++] = tmp;
	}
	else {
		cout << "Inventory at full; cannot enter more unit." << endl;
		system("pause");
	}
	system("cls");
}

void dispData(const InventoryRecord list[], int size)
{
	system("cls");
	int number = 0;

	if (size < 1) {
		cout << "Nothing to display" << endl;

	}
	else {
		cout << "Mike's Fish Market" << endl << endl;
		cout << "All Inventory item has been shown" << endl << endl;
		cout << fixed << setprecision(2);
		cout << "Item Name              Quanitity     Item Number" << endl;
		cout << "~~~~~~~~~~~~~~~~~~" << endl;

		cout << left;
		for (int i = 0; i < size; i++) {
			cout << setw(21) << list[i].name << right
				<< setw(4) << list[i].qty
				<< setw(10) << list[i].number << left << endl;
			//number = number + list[i].number * list[i].qty;
		}

		cout << "~~~~~~~~~~~~~~~~~~~" << endl;
		cout << right << setw(3) << size;
		cout << " items listed" <<endl;
		//cout << right << setw(19) << number << endl << endl;
	}

	system("PAUSE");
	system("cls");
}

// Buy item
void buyItem(const InventoryRecord list[], int size) {
	system("cls");
	int number = 0;

	if (size < 1) {
		cout << "Nothing to display" << endl;

	}
	else {
		cout << "Mike's Fish Market" << endl << endl;
		cout << "Enter the Item number and quanitity of the item you want to buy" << endl << endl;

		InventoryRecord buy; // declare a temp item that we will load before putting in the array
		InventoryRecord tmp;
		char response;
		char str[256];
		if (size < MAX_SIZE) {
			system("cls");
			cout << "Item Number:     ";
			cin >> tmp.number;
			cout << "Quantity: ";
			cin >> (tmp.qty = (buy.qty - tmp.qty));
			saveFile;
			cout << endl;
		}
	}
}

// Save records to disc
void saveFile(const InventoryRecord list[], int size) {
	ofstream outfi("store.txt");

	// make sure the file stream is open before doing IO
	if (!outfi.fail()) {
		system("cls");
		cout << "Saving inventory to the disc ";

		for (int i = 0; i < size; i++) {
			outfi << list[i].name << ';'
				<< list[i].qty << ';'
				<< list[i].number;
			// Start a new line after all but the last record
			// Simplifies reading the file as EOF is at end of last line
			if (i < size - 1) outfi << endl;
		}
		cout << endl << size << " records writen to the disc." << endl;
		outfi.close();
		system("PAUSE");
		system("cls");
	}
	else {
		cout << "ERROR: problem with file" << endl;
		system("PAUSE");
		system("cls");
	}
}

// Open file and load array
void openFile(InventoryRecord list[], int& size)
{
	ifstream infi("store.txt");
	string str;
	stringstream strstrm;

	// make sure the file stream is open before doing IO
	if (!infi.fail()) {

		system("cls");
		cout << "Reading inventory from the disc ";

		size = 0; // overwrite any existing records
		while (!infi.eof() && size < MAX_SIZE) {
			// get and store the name
			getline(infi, str, ';');
			list[size].name = str;

			// get, convert and store the quantity
			getline(infi, str, ';');
			strstrm.str(""); strstrm.clear(); // empty and clear the stringstream
			strstrm << str;
			strstrm >> list[size].qty;

			// get, convert and store the cost
			getline(infi, str);
			strstrm.str(""); strstrm.clear(); // empty and clear the stringstream
			strstrm << str;
			strstrm >> list[size++].number;
		}
		cout << endl << size << " records read from the disc." << endl;

		system("PAUSE");
		system("cls");
	}
	else { // something went wrong with opening the file
		cout << "ERROR: problem with file" << endl;
		system("PAUSE");
		system("cls");
	}

}

char getMenuResponse()

{
	char response;
	cout << endl << "Make your selection" << endl
		<< "(O)pen Store, (D)isplay Items, (B)uy Item, (F)ile Save, (S)top" << endl
		<< "> ";
	cin >> response;
	cin.ignore(256, '\n');
	// clean-up up to 256 chars including the delimiter specified (\n, the endl) 
	// OR stop when the \n is encountered after removing it.
	return toupper(response);
	// note the use of toupper, why?
}


I don't think I'm doing it right...
Topic archived. No new replies allowed.