Saving c++ into a .txt file

Pages: 12
complete code:
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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

struct Supplies
{
	string supplyName;
	string supplyAmount;
};



void add ( Supplies & thisSupply );
bool find ( string & supplyName );

void printinventory ( void );



const int items = 4;
Supplies Book [ items ] ;
int arrayIndex;
int Inventory = 0;



int main ( )
{

	arrayIndex = 0;
	Supplies temp;
	char choice;
	string supplyName;

	do
	{
		cout << endl << endl << endl;
		cout << "Welcome to your lemonade stand!!" << endl;
		cout << "What would you like to do?" << endl << endl;
		cout << "1. Order supplies" << endl;
		cout << "2. Check supplies" << endl;
		cout << "3. Check ordered supplies" << endl;
		cout << "4. Delete all ordered supplies" << endl;
		cout << "5. Delete an ordered supply" << endl;
		cout << "6. Quit" << endl << endl << endl;

		cout << endl << "Enter number...." << endl;


		cin >> choice;
		while ( choice != '1' && choice != '2' && choice != '3' && choice != '4' && choice != '5' && choice != '6' )
		{
			cout << "Bad choice ( 1,2,3,4,5,6 ) , enter choice : ";
			cin >> choice;
		}

		// Switch statement
		switch ( choice )
		{
			case '1':
			{
				ofstream outfile("Orders.txt", ios::out | ios::app);

				cout << endl;
				cout << "Enter supply name: ";
				cout << endl << "Lemons" << endl;
				cout << "Ice" << endl;
				cout << "Sugar" << endl;
				cout << "Cups" << endl;
				cin >> temp.supplyName;
				outfile << temp.supplyName << " ";
				cout << "Enter amount: ";
				cin >> temp.supplyAmount;
				outfile << temp.supplyAmount << endl;

				outfile.close();

				add ( temp );
			}	
			break;

			case '2':
			{
				cout << "Enter supply name: ";
				cout << endl << "Lemons" << endl;
				cout << "Ice" << endl;
				cout << "Sugar" << endl;
				cout << "Cups" << endl;
				cin >> supplyName;
				
				if ( find ( supplyName ) )
					cout << supplyName << " is in the inventory." << endl;
				else
					cout << supplyName <<" is not in the inventory." << endl;
			}
			break;

			case '3':
			{
				string line;
				fstream outfile ("Orders.txt", ios::in | ios::out);
				if (outfile.is_open())
				{
					while (! outfile.eof() )
					{
						getline (outfile,line);
						cout << line << endl << " ";
					}

					outfile.close();
				}
				else
					cout << "Unable to open file";
			}
			break;

		case '4':		
		{
			ofstream outfile("Orders.txt", ios::trunc);
			outfile.close();			
		}
		break;

		case '5':
		{
			cin.sync();
			vector<string> file;
			string temp;

			ifstream infile("Orders.txt");

			while( !infile.eof() )
			{
				getline(infile, temp);
				file.push_back(temp);
			}
			// done reading file
			infile.close();

			string item;

			cout << "Enter an item to delete from the order: ";
			getline(cin, item);

			for(int i = 0; i < (int)file.size(); ++i)
			{
				if(file[i].substr(0, item.length()) == item)
				{
					
					file.erase(file.begin() + i);
					cout << "Order erased!"<< endl;
					i = 0; // Reset search
				}
			}

			//write new order list back out
			ofstream out("Orders.txt", ios::out | ios::trunc);

			for(vector<string>::const_iterator i = file.begin(); i != file.end(); ++i)
			{
				out << *i << endl;
			}
			out.close();
		}
		break;




		}; // end of switch

	}while ( choice != '6' );

	return 0 ;
}



//FUNCTIONS.....

void add ( Supplies & thisSupply )
{
	if ( arrayIndex >= items )
	arrayIndex = 0;

	if ( Inventory < items )
		Inventory++;



	Book [ arrayIndex ].supplyName = thisSupply.supplyName;
	Book [ arrayIndex ].supplyAmount = thisSupply.supplyAmount;
	arrayIndex++;

	return;
}



//
bool find( string & supplyName )
{
	int i = 0;

	while ( i < Inventory )
	{
		if ( supplyName == Book [ i ].supplyName )
		return true;

		i++;
	}

	return false;
}



//
void printinventory ( void )
{
	int i = 0;
	while ( i < Inventory && i < items )
	{
		cout << i << ". " << Book [ i ].supplyName<< " " << Book [ i ].supplyAmount << endl;

		i++;
	}
return;
}
Thank you very much sir eker676 and for the rest of those who helped me.. =)
Topic archived. No new replies allowed.
Pages: 12