Hello missyredx,
Reading the directions / instructions or what I think of as specs, (specifications) Part I #1 has always been covered. Where you have missed is Part I #2.
Given
2) Append the following inputted records to the customer.txt file and print all the values in the file to the screen. |
I find this to be a bit confusing, but with all that you have said what I get is the you need to append "customer.txt" with "customer1.txt".
Your first attempt does append the file, but in the wrong way. Since the code is written I would not get rid of it just yet. It may still be useful. Maybe not for this program, but in the future.
The other thing I have a problem with is asking the user for a file name. When you see this on the screen:
Enter filename to append :
|
There is no telling what will be entered.
If you are going to do this then you need to precede it a list of file names to use because any user will have no idea what can be used or they could use a file name that does not exist or misspell the file name. Then you will have a problem when you try to open the file.
It is best to write the program to avoid having the user enter a file name and the way this program is needed to be done there is no need to ask the user to enter a file name.
You have written this:
std::ofstream outFile(fileName.c_str(), std::ios::app);
. If you are compiling to the 2011 standards the ".cstr()" is not needed. From the 2011 standards on a "std::string" will work fine as you did in "main".
The other change I would make is:
1 2 3 4 5 6 7 8 9 10 11
|
std::cout << "Enter Units: ";
std::cin >> units;
std::cout << std::endl;
outFile << first << ' ' << last << ' ' << state << ' ' << salesZ << salesO << salesT << units << std::endl;
std::cout << "\n\tEnter another customer record? (y/n) ";
std::cin >> cont;
std::cout << std::endl;
} while (std::tolower(cont) == 'y');
|
You only need one write to the file line. What you start with will work, but there is no need to write to the output after every input.
This is where it gets interesting.
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
|
bool appendtoFile(std::ifstream& inFileCust1)
{
const std::string outFileName{ "customer.txt" };
std::string first;
std::string last;
std::string state;
int salesZ{};
int salesO{};
int salesT{};
int units{};
std::ofstream outFile(outFileName, std::ios::app);
if (!outFile)
{
std::cout << "\n ERROR: Unable to open file. \"" << outFileName << "\"" << std::endl;
return true;
}
while (inFileCust1 >> first >> last >> state >> salesZ >> salesO >> salesT >> units)
{
outFile << first << ' ' << last << ' ' << state << ' ' << salesZ << ' ' << salesO << ' ' << salesT << ' ' << units << '\n';
}
return false;
}
|
You will notice that I have used the same name for this function with one difference the parameter. This is known as an overloaded function. As long as the parameters are different the compiler will know which function to use based on how you call it.
Line 13 is interesting. back in "main" "customer.txt" was opened as an "ifstream", but in this function you open the file as an "ofstream" This works because of scope. Line 13 is local to the function and has no knowledge of what you did in "main".
The while loop has a short task. All it needs to do is read the input file and write it to the output file. When the function ends and returns to "main" there you need to print the new "customer.txt" file.
With the instructions of:
Part II
1) Create a new file named customer2.txt and input the following records.
2) Read the data from the customer2.txt and print the values to the screen.
3) Read the values from the customer2.txt file and create a CSV file using the data.
|
My understanding here is for #1 you need to read the "customer.txt" and "customer1.txt" files to create the "customer2.txt" file. easy enough to do.
#2 would be to call the print function to print the "customer.txt" file to the screen.
The #3 would be to create the ".csv" file.
Now before you start writing this function you need to make some choices. First you could open "customer2.txt" as an "ofstream" create the file then close the stream and reopen it as an "ifstream". Or define it as a "fstream" and open it for both input and output.
std::fstream(fileName, std::ios::in | std::ios::out);
. As an "fstream" you need to tell it if it for input or output or both. Not having done this in the program I would say try the "fstream" because you have not done this yet.
Your next choice is how to write the CSV file. Do you put a "," after every field, except the last one, or do you put the "," only after the strings? For this program either way would work. How you write the file now can make a difference on how you will need to read the file later.
For what is required you can put a "," after every variable, except the last one, that would be the new line character.
The idea I have for now starts like this:
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
|
bool createCSV(std::ifstream& inFileCust, std::ifstream& inFileCust1)
{
const std::string ioFileName{ "customer2.txt" };
const std::string outFileName{ "customer2.csv" };
std::string first;
std::string last;
std::string state;
int salesZ{};
int salesO{};
int salesT{};
int units{};
char contin = 'y';
/*
Read inFileCust
Write ioFile
Read inFileCust1
Write ioFile
Set ioFile pointer to the beginning
Call print function
Read ioFile
Create CSV file
*/
// End of function
}
|
That is my idea for now. I have partially started working on this function.
Note: "seekg()" is for the input pointer and "seekp()" is for the output pointer, but I have not had much success using "seekp".
Andy