Inputting from a file with alot of data

Hey i have to create a video store program for college. Im using a struct to store all data. This is my problem.

I have to use a file, the file needs to have all the information on the videos

eg of what the text documents holds
code#Title#copies#onShelf
4444#Alien Vs Predator#4#4

nw my code looks like this so far but it doesnt do what i want.
1
2
3
4
5
6
7
8
9
10
int inInfo (ifstream inFile, videoInfo& dvd) 
{ 
	int numElements = 0 ; 
	while (!inFile) 
	{ 
		inFile >> dvd.code >> dvd.title >> dvd.copies >> dvd.onShelf ; 
		numElements ++ ; 
	} 
	return numElements ; 
} 


but nw the title will only store "Alien", and not " Vs Predator". I knw in java you get the scanner class with delimiter function but how would i go about that in c++, i knw u get inFile.getline (dvd.title, (number of spaces to skip)) is there a way to make it keep on reading the line until it reaches # instead? Because each title will have a different length of characters.

Thanks
Ross
You have to use getline(inFile, dvd.title);


cin>> stops after a space.
Last edited on
but can u use a delimiter in getline?

eg. getline (inFile, dvd.title, '#') ;

would that work?

Hello sighter,

sighter wrote:
getline (inFile, dvd.title, '#') ;

would that work?
that should be the solution for the content of the line but first you have to get the line itself like:
1
2
3
4
5
6
7
#include <sstream>

std::string line;
std::getline(inFile, line);
std::stringstream ss(line);
getline (ss, dvd.title, '#') ;
...
look at this http://www.cplusplus.com/reference/iostream/stringstream/
Last edited on
would this work:
1
2
3
4
5
6
7
8
9
10
void inInfo (ifstream& inFile, int& num) 
{ 
	while (!inFile) 
	{ 
		getline(inFile, dvd[num].code, '#') ; 
		getline(inFile, dvd[num].title, '#') ; 
		cin >> dvd[num].copies >> dvd[num].onShelf ; 
		num ++ ; 
	} 
} 


if not i will try your way
thanks for useful information tho :) help me with another problem i was having aswel
that would work if 'inFile' contains only 'code' and 'title' and the only delimiter is '#' then yes
ok cool, i manipulate the file like that then.
Thanks for the help :)
ok got a new problem with the getline
Here is my 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
void addDvd (int& num)
{ 
	num ++ ; 
	int i = 0 ; 
	bool test = false ; 
	string str ; 

	cout << "Please enter Title of movie: " << endl  ; 
	getline (cin, str) ; // ITS SKIPS HERE
	dvd[num].title = str ; 
	cout << "Please enter a 4-digit code: " ; 
	cin >> dvd[num].code ; 
	cout << "Please enter how many copies you have: " ; 
	cin >> dvd[num].copies ; 
	dvd[num].onShelf = dvd[num].copies ; 
	cout << "Please enter deposit on movie in: R " ;  
	cin >> dvd[num].deposit ; 
	cout << "Please enter cash back when movie is returned: R " ; 
	cin >> dvd[num].cashBack ; 

	while (i < num && test == false) 
	{ 
		if (dvd[num].code == dvd[i].code) 
		{ 
			test = false ; 
			cout << "Dvd code: " << dvd[num].code << " is already in use. " << endl 
				 << "Please enter a different code: " ; 
			cin >> dvd[num].code ; 
			i = 0 ; 
		} 
		else 
		{ 
			i++ ; 
		} 
	} 

	cout << "Movie has been successfully added" << endl ; 
} 


my output looks like this
Please enter title of moive: // Here it wont let me type anything.
Please enter a 4-digit code: 45453

please help me :( i knw its most prob a small problem but i cant find out what is wrong and ive looked everywhere on the net

Never mind found out what the problem was.
Last edited on
Bump**

I am in need of some help, im still struggling to read the information from the file.
It wont read anything into to struct

struct:
1
2
3
4
5
6
7
8
9
struct videoInfo
{ 
	string code ; 
	string title ; 
	int copies ; 
	int onShelf ; 
	float deposit ; 
	float cashBack ;
} ; 


my function to read the data from the file:
1
2
3
4
5
6
7
8
9
10
11
void inInfo (ifstream& inFile, int& num) 
{  
	string line ;
	while (!inFile) 
	{ 
		getline (inFile, dvd[num].code, '#') ;  
		getline (inFile, dvd[num].title, '#') ; 
		cin >> dvd[num].copies >> dvd[num].onShelf ;
		num++ ; 
	}
} 


The text file will look like this: eg
4444#Harr Potter#4 4
CODE#TITLE#NUM OF COPIES NUM OF SHELF
CODE#TITLE#NUM OF COPIES NUM OF SHELF
CODE#TITLE#NUM OF COPIES NUM OF SHELF

Please can someone help me, i tried the previous example above
1
2
3
4
5
6
7
#include <sstream>

std::string line;
std::getline(inFile, line);
std::stringstream ss(line);
getline (ss, dvd.title, '#') ;
...


but it wont work or either im doing it wrong dont knw which one.
Thanks Ross
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
void inInfo (ifstream& inFile, int& num) 
{  
	string line ;
	while (!inFile) 
	{ 
                std::getline(inFile, line);
		getline (line, dvd[num].code, '#') ;  
		getline (line, dvd[num].title, '#') ; 
		line >> dvd[num].copies >> dvd[num].onShelf ;
		num++ ; 
	}
}
that's supposed to work
did that, it wont work with "line", gotta parse it through a stringstream.
But i made the function print out how many times the while loop ran and i either get 0 or 1 but ive added 5 records to the text doc. Im so confused :(

would it help if i post my main method for u to have a look at?
sorry my code was wrong
1
2
3
4
5
6
7
8
9
10
11
12
13
void inInfo (ifstream& inFile, int& num) 
{  
	while (inFile.good())  // <--- that looks more plausible
	{ 
                string line ;
                std::getline(inFile, line);
                std::stringstream ss(line);
		getline (ss, dvd[num].code, '#') ;  
		getline (ss, dvd[num].title, '#') ; 
		ss >> dvd[num].copies >> dvd[num].onShelf ;
		num++ ; 
	}
}
i checked it and it works for me. But if that works depends also on what the parameter num contains.

i'd say that it's better this way
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int inInfo (ifstream& inFile) 
{  
    int i = 0;
	for(; inFile.good(); ++i)
	{ 
                string line ;
                std::getline(inFile, line);
                std::stringstream ss(line);
		getline (ss, dvd[i].code, '#') ;  
		getline (ss, dvd[i].title, '#') ; 
		ss >> dvd[i].copies >> dvd[i].onShelf ;
	}
    return i;
}
and check whether 'i' goes out of bounds
i tried both ways and still it only returns 1. I think i might have a problem in my main ()

this is my whole program... so sorry to be doing this to u, i knw it must be annoying and i apologize and i really appreciate all your help
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <iostream> 
#include <string> 
#include <iomanip> 
#include <fstream> 
#include <sstream> 

using namespace std ; 

const int MAX_VIDEOS = 200 ; // Total videos allowed at the store

//<!--struct for Dvd / video information--> 

struct videoInfo
{ 
	string code ; 
	string title ; 
	int copies ; 
	int onShelf ; 
	float deposit ; 
	float cashBack ;
} ; 

videoInfo dvd [MAX_VIDEOS] ; 

//<!--Prototypes --> 

int inInfo (ifstream&) ; 
void outInfo (ofstream&, int) ; 
void rentDvd (string, int) ; 
void returnDvd (string code, int num) ; 
void addDvd (int& num) ; 
void editDvd () ; 
void deleteDvd () ; 
void listAllDvd () ; 
void listAvailableDvd () ; 
void listUnavailableDvd () ; 
void detailDvd () ; 

//<!-- end of prototypes --> 

//<!-- start of main --> 

int main () 
{ 
	char ans ;
	string code ; 
	int numElements = 0 ; 
	bool test = false ; 

	ifstream inFile  ; 
	inFile.open("VideoDetails.txt") ; 
	ofstream outFile  ; 

	numElements = inInfo (inFile) ; 
	cout << numElements << endl ; 
	if (!inFile) 
	{ 
		cout << "Could not connect to file" << endl ; 
	} 
	else 
	{ 
		cout << "Connection to file made" << endl ; 
	} 

	cout << "WELCOME TO ""BEST VIDEOS 'n DVDs" << endl 
		<< "************************************" << endl ; 

	do
	{

		cout << "Menu" << endl 
			<< "A. Rent a Dvd" << endl 
			<< "B. Return a Dvd" << endl 
			<< "C. Add a new Dvd" << endl 
			<< "D. Edit an existing Dvd" << endl 
			<< "E. Delete an existing Dvd" << endl 
			<< "F. List all Dvds" << endl 
			<< "G. List all AVAILABLE Dvds" << endl 
			<< "H. List all UNAVAILABLE Dvds" << endl 
			<< "I. Display all details of a Dvd" << endl
			<< "Q. Quit Application" << endl << endl 
			<< "Option: " ; 
		cin >> ans ;  
		ans = toupper(ans) ; 
		switch (ans) 
		{ 
		case 'A': 
			cout << "Enter dvd code: " ; 
			cin >> code ; 
			rentDvd (code, numElements) ; 
			test = false ; 
			break ; 
		case 'B':
			cout << "Enter dvd code: " ; 
			cin >> code ; 
			returnDvd (code, numElements) ; 
			test = false ; 
			break ; 
		case 'C': 
			addDvd (numElements) ; 
			test = false ; 
			break ; 
		case 'D': 
			cout << "Enter dvd code: " ; 
			cin >> code ; 
			editDvd () ; 
			test = false ; 
			break ; 
		case 'E': 
			cout << "Enter dvd code: " ; 
			cin >> code ; 
			deleteDvd () ; 
			test = false ; 
			break ; 
		case 'F': 
			listAllDvd () ;
			test = false ; 
			break ; 
		case 'G': 
			listAvailableDvd () ;
			test = false ; 
			break ; 
		case 'H': 
			listUnavailableDvd () ; 
			test = false ; 
			break ; 
		case 'I': 
			cout << "Enter dvd code: " ; 
			cin >> code ; 
			detailDvd () ;
			test = false ; 
			break ; 
		case 'Q': 
			outFile.open("VideoDetails.txt") ; 
			outInfo (outFile, numElements) ; 
			test = true ; 
			break ; 
		default: 
			cout << "Incorrect option, please try again: " ; 
			cin >> ans ; 
			test = false ; 
			break ; 
		} 
	}while (test==false) ; 

	inFile.close() ; 
	outFile.close () ; 
	return 0 ; 


} 

//<!-- end of main --> 

//<!-- start of functions --> 

  
/*void inInfo (ifstream& inFile, int& num) 
{  
	while (inFile.good()) 
	{ 
		string line ; 
		std::getline(inFile, line) ; 
		std::stringstream ss (line) ; 
		std::getline (ss, dvd[num].code, '#') ; 
		std::getline (ss, dvd[num].title, '#') ; 
		ss >> dvd[num].copies >> dvd[num].onShelf ; 
		num ++ ; 
	}
	cout << num << endl ; 
}*/

int inInfo (ifstream& inFile) 
{  
    int i = 0;
	for(; inFile.good(); ++i)
	{ 
        string line ;
        std::getline(inFile, line);
        std::stringstream ss(line);
		getline (ss, dvd[i].code, '#') ;  
		getline (ss, dvd[i].title, '#') ; 
		ss >> dvd[i].copies >> dvd[i].onShelf ;
	}
    return i;
}
void outInfo (ofstream& outFile, int num) 
{ 
	for (int i = 0 ; i < num ; i++) 
	{ 
		outFile << dvd[i].code <<"#"<< dvd[i].title <<"#"<< dvd[i].copies <<" "<< dvd[i].onShelf ; 
	}
} 

void rentDvd (string code, int num) 
{ 
	int i = 0 ; 
	char ans ; 
	bool test = false ; 
	while (test == false) 
	{ 
		if (dvd[i].code == code) 
		{ 
			test = true ; 
			if (dvd[i].onShelf < 1) 
			{ 
				cout << "Movie: " << dvd[i].title << " is not on the shelf at the moment" << endl 
					<< "Try another movie?: " ; 
				cin >> ans ; 
				ans = toupper(ans) ; 
				do
				{
					switch (ans)
					{ 
					case 'Y': 
						cout << "Enter dvd code: " ; 
						cin >> code ; 
						i = 0 ; 
						test = false ; 
						break ;
					case 'N': 
						cout << "Loading menu..." << endl ; 
						test = true ; 
						break ; 
					default: 
						cout << "Incorrect option, either Y or N: " ; 
						cin >> ans ; 
						break ;
					}
				} while (ans!='N') ; 

			} 
			else 
			{ 
				test = true ; 
				dvd[i].onShelf -- ; 
			} 
		} 
		else 
		{ 
			test = false ; 
			i ++ ;
			if (i > num) 
			{ 
				cout << "Movie could not be found" << endl ; 
				cout << "Would you like to search again for another movie Y or N: " ; 
				cin >> ans ; 
				ans = toupper(ans) ; 
				switch (ans) 
				{ 
				case 'Y': 
					cout << "Enter dvd code: " ; 
					cin >> code ; 
					test = false ; 
					i = 0 ; 
					break ;
				case 'N': 
					test = true ; 
					break ; 
				} 
			} 

		} 
	} 
}

void returnDvd (string code, int num)
{ 
	char ans ; 
	int i = 0 ; 
	bool test = false ; 
	while (test == false) 
	{ 
		if (dvd[i].code == code) 
		{ 
			test = true ; 
			if (dvd[i].copies <= dvd[i].onShelf) 
			{ 
				test = false ; 
				cout << "Movie: " << dvd[i].title << " is all ready full on the shelf and no more can be added" << endl 
					<< "Try another movie?: " ; 
				cin >> ans ; 
				ans = toupper(ans) ; 
				do
				{
					switch (ans)
					{ 
					case 'Y': 
						cout << "Enter dvd code: " ; 
						cin >> code ; 
						i = 0 ; 
						test = false ; 
						break ;
					case 'N': 
						cout << "Loading menu..." << endl ; 
						test = true ; 
						break ; 
					default: 
						cout << "Incorrect option, either Y or N: " ; 
						cin >> ans ; 
						test = false ; 
						break ;
					}
				} while (ans!='N') ; 
			}
			else 
			{ 
				dvd[i].onShelf ++ ; 
				test = true ; 
			} 
		} 
		else 
		{ 
			i++ ; 
			test = false ; 
			if (i > num) 
			{ 
				cout << "Movie could not be found" << endl ; 
				cout << "Would you like to search again for another movie Y or N: " ; 
				cin >> ans ; 
				ans = toupper(ans) ; 
				switch (ans) 
				{ 
				case 'Y': 
					cout << "Enter dvd code: " ; 
					cin >> code ; 
					test = false ; 
					i = 0 ; 
					break ;
				case 'N': 
					test = true ; 
					break ; 
				} 
			} 
		}
	}
}

void addDvd (int& num)
{ 
	int i = 0 ; 
	bool test = false ; 
	string str ; 

	cout << "Please enter Title of movie: " ; 
	cin.ignore() ; 
	getline (cin, dvd[num].title) ;  
	cout << "Please enter a 4-digit code: " ; 
	cin >> dvd[num].code ; 
	cout << "Please enter how many copies you have: " ; 
	cin >> dvd[num].copies ; 
	dvd[num].onShelf = dvd[num].copies ; 
	cout << "Please enter deposit on movie in: R " ;  
	cin >> dvd[num].deposit ; 
	cout << "Please enter cash back when movie is returned: R " ; 
	cin >> dvd[num].cashBack ; 

	while (i < num && test == false) 
	{ 
		if (dvd[num].code == dvd[i].code) 
		{ 
			test = false ; 
			cout << "Dvd code: " << dvd[num].code << " is already in use. " << endl 
				 << "Please enter a different code: " ; 
			cin >> dvd[num].code ; 
			i = 0 ; 
		} 
		else 
		{ 
			i++ ; 
		} 
	} 
	num ++ ; 
	cout << "Movie has been successfully added" << endl ; 
} 

did you write the file with this function?
1
2
3
4
5
6
7
void outInfo (ofstream& outFile, int num) 
{ 
    for (int i = 0 ; i < num ; i++) 
    { 
        outFile << dvd[i].code <<"#"<< dvd[i].title <<"#"<< dvd[i].copies <<" "<< dvd[i].onShelf ; 
    }
}
then it's no wonder that you read just 1 line because it misses an endl (or "\n"):
1
2
3
4
5
6
7
8
9
10
void outInfo (ofstream& outFile, int num)
{
    for (int i = 0 ; i < num ; i++)
    {
        outFile << dvd[i].code << "#";
        outFile << dvd[i].title << "#";
        outFile << dvd[i].copies << " ";
        outFile << dvd[i].onShelf << std::endl;
    }
}
no i hard code the data into the file first, then when the program quites it must rewrite the information
u were right, changed that and it worked, didnt think it would effect the program because it was called in the switch statement.

Thank you so much for the help
i love u haha
Really appreciate all the help :)
Topic archived. No new replies allowed.