I have a read/write assignment I'm working on and I'm stuck on the part where the books.cpp reads the file and changes data to write to another file (I haven't gotten to the write part yet). What needs to be done is read the book title, author, and price from a file, and change the price by discounting it by 20%. I'm not going to ask for the output help yet, just the read and change data. There are multiple books in the read file.
We are required to use a header file, plus a two other files. Here is what I have so far. I'm getting weird errors on my infile.getline lines but I have no idea what they mean. It's random jibirish! I don't know how I need to properly write my readWrite function.
the txt file reads like:
Title
Author
Price
Title
Author
Price
etc.
See anything wrong with lines 16 and 17 of book.cpp?
Do not define global variables in header files. Including those headers in multiple .cpp files means you'll be attempting to supply multiple definitions for the same variables. Also, it's a very bad practice to put usingnamespace std; in a header file.
I see. I googled solutions on how to do it and I found that one and someone claimed it worked. I have modified it and it now compiles. Unfortunately my results are a butchered mess.
I will post my code and show you what is in the file and what my results show me:
File shows (just the first three books as an example):
The Goldfinch
Donna Tartt
6.99
The Husband's Secret
Liane Moriarty
9.99
All the Light We Cannot See
Anthony Doerr
10.99
Results show:
The Goldfinch
Donna Tartt
0
.99
The Husband's Secret
0
iane Moriarty
9.99
0
ll the light we cannot see
Anthony Doerr
0
0.99
....what a mess! :P What's weird is for the infile >> Book[i].price; line, it initially crashed my program, so I changed the arrows to << and then it gave me the results above. This is my first time really trying to write something with arrays and a file, so I'm struggling just a bit.
And I got rid of the book.cpp in the meantime and put the function into the int main function just to test it because it was causing me more headaches! :(