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.
#include <iostream>
#include <fstream>
usingnamespace 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;
}
You are always writing at the beginning of output file instead of appending text to the end of it.
Try to initialize bookIntoFile with ios::app parameter: ofstream bookInfoFile("LABPROA3", ios::app); and close it after you write all data to it (after that while loop)
open the file once (before while loop) and close if once (after while loop) and you dont have to worry about append mode.
if you want to keep your code as it is, do what mobotus says.
Append mode means, the data to be written will be written below the data already in the file.
So its like adding items to a list.
#include <iostream>
#include <fstream>
usingnamespace 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;
bookInfoFile.open("LABPROA3");
//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')
{
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);
cout << "Would you like to enter another book?" << endl;
cin >> enterBook;
}
bookInfoFile.close();
return 0;
}
The code is now this, rich1 was correct but he had the ios::app in the wrong spot. The mode is not used after the initialization but when you open the function with: bookInfoFile.open("LABPROA3", ios::app); on line 36