I need little help with my homework in C++. My task is to write a function for borowing books form library called borrow(name,surname, student_id, book_id), which will connect information of borrowed book's ID and student's informations in the file.
File looks like this:
1 2 3 4 5 6 7 8
John Jackson 45 0
Michael Gregory 34 56
Ann Cawitch 23 0
Chris Lamb 34 50
...
First two words are name and surname of students. First number represents student's ID and second one represents ID of book. If ID of book is 0, it means that student hasn't borrowed book yet, so he can borrow some book. If ID of book is not 0, it means that student can't borrow a book until he returns the old one. So, when student borrows book, ID of book in file should change from zero to some number
pass name and surname by const-reference.
Changing a value in a file isn't that simple.
I would probally just read the whole thing, change what you want and write everything back out.
Here is how one could do it (however, it is by no means perfect!)
Thanks for help but your code does not compile in VS 2012. The problem is with following line of code istringstream stream{ line };
Probably these curly brackets should be replaced with standard parenthesis.
I tried that and code compiled, but another problem appears. Every time I try to run the program I get message "Invalid file entry". I can't guess why is that so.
Yes, if that doesn't work with your compiler ,the easiest solution is using the standard "()".
If you are interested in this, google: C++11 uniform initialization
You are right, my code is not that great. Remove the following lines and it should work (they are not needed):
1 2
if (line == "") continue; // ignore empty lines
if (!stream) cout << "Invalid file entry:\n" << line << '\n';