The compiler is right. You declare copies without initializing it, then immediately try to print its value. That's undefined behavior, and constitutes a bug.
deceptikon
Challenge Accepted
3,456 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57
The operations in addbook() (saving in file) and the operation in display() (loading from file) are not the inverse of each other, and thus, the loading will fail. Here is what I mean, say you have the following values for your variables:
string bookname = "C++ Primer";
string publisher = "Addison-Wesley Professional";
string author = "Stanley B. Lippman, Josée Lajoie, Barbara E. Moo";
int copies = 42;
int price = 45;
int edition = 5;
After you run the following code:
input << bookname << publisher << author << copies << price << edition;
The file will contain the following:
C++ PrimerAddison-Wesley ProfessionalStanley B. Lippman, Josée Lajoie, Barbara E. Moo42455
At this point, there is no way to extract the information back from the file because it is all jumbled up. You are going to need separators to mark where each field begins and where they end. One such marker is simply a new-line. If you output the data to the file in this format:
input << bookname << endl
<< publisher << endl
<< author << endl
<< copies << endl
<< price << endl
<< edition << endl;
it will be a lot easier because the content of the file will now be:
C++ Primer
Addison-Wesley Professional
Stanley B. Lippman, Josée Lajoie, Barbara E. Moo
42
45
5
which is a lot easier to load, simply reading it line-by-line (see std::getline()).
mike_2000_17
21st Century Viking
3,140 posts since Jul 2010
Reputation Points: 2,062
Solved Threads: 625
Skill Endorsements: 41