In the following code I am trying to write book information into a .txt file so that I can then read from it later to display all of it. However, I noticed that upon execution of the loop it constantly overwrites the 4 values that are written.
Rather then continuing to write more then 4 entries.
I have searched quite a few places over the internet and have found nothing that specifically relates to this problem. And even if it did, it was usually written in another language or used syntax that I have no experience with and had trouble understanding. If someone could help me out, then that would be awesome and much appreciated.
I have also asked my friends and my lab TA who had issues helping me as well, he also didn't have much time to explain everything as it was the end of class but he did say that writing to a file is really the only way that will accomplish the storing of an unlimited amount of book data. I will talk to him again on Tuesday, and yes I have been working on this for quite some time as I was assigned this on Monday. I figured I might get some pointers leading me in the right direction on this site. I like to finish things early.
Also I will admit that this is a question for homework, I have no interest in receiving help on completing the assignment in its entirety as I feel certain that I know how to read from the file and display it. I just need help with this specific issue, it's been kind of a roadblock for me.
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
|
#include <iostream>
#include <fstream>
using namespace std;
//Function to take values from main, and write them in the .txt file
void getbookInfo(int &prospectCE, int &numBooks, double &bookCode, double &single_CC, ofstream &bookInfoFile)
{
bookInfoFile << bookCode << endl;
bookInfoFile << single_CC << endl;
bookInfoFile << prospectCE << endl;
bookInfoFile << numBooks << endl;
bookInfoFile.close();
}
int main()
{
//Declaration of file, and all needed variables.
ofstream bookInfoFile;
int ceProspect, booksNum;
double codeBook, cc_Single;
char enterBook;
//Explanation statement, and the starting point of the loop.
cout << "This program is going to display the total profit after selling all of the books that you enter." << endl;
cout << "Do you wish to enter a book and it's information? Type Y for yes or N for no." << endl;
cin >> enterBook;
//The loop which collects the book information, then sends it to the function which writes the information for further use.
while (enterBook == 'Y' || enterBook == 'y')
{
bookInfoFile.open("LABPROA3");
cout << "Please enter the unique code for the book." << endl;
cin >> codeBook;
cout << "Please enter the single copy cost for the book." << endl;
cin >> cc_Single;
cout << "Now enter the total number of people participating in the class that this book is needed for." << endl;
cin >> ceProspect;
cout << "Now enter the total number of copies that you have for this book." << endl;
cin >> booksNum;
getbookInfo(ceProspect, booksNum, codeBook, cc_Single, bookInfoFile);
bookInfoFile.close();
cout << "Would you like to enter another book?" << endl;
cin >> enterBook;
}
return 0;
}
|