Question
Language in C++ Assignment: Write a program that reads a text file containing a list of books. Each line in the file contains tile, author
Language in C++
Assignment:
Write a program that reads a text file containing a list of books. Each line in the file contains tile, author and genre of a specific book. Each field is separated by comma. Print out book titles sorted alphabetically.
Detailed specifications:
- File processing:
- Program should try to open a file using a default file path first, then check if it was successful, if not, ask the user for a different file name/path and try again until it succeeds. Do not try to process a file that you could not open. Remember to close the file when done reading.
- Read each line from the file using getline: getline (ifstream input, string line)
- Store the book titles in a vector (not an array!).
- To separate the title from the line, you can make the line a stream:
- stringstream lineStream(line);
- then use getline again to take the title: getline(lineStream, title, ',');
- To add values to a vector, use function push_back.
- To separate the title from the line, you can make the line a stream:
- Sort the data using the sort function
- Define a separate function to print the required info on the screen:
- This function should receive the vector as a parameter.
- Use a range-based for loop or iterator to print the titles.
- Do not use global variables! Do not use goto!
- Document your source code following the guidelines
Above function headers
Write a comment above each function header explaining what the function does and the purpose of each parameter and return type.
Example:
/*
This function adds the amount passed as parameter to the balance
in the account and returns whether the operation was successful.
amount: the amount to be deposited
Return value: true means the deposit was successful,
false means the deposit was unsuccessful
*/
bool deposit (double amount)
{
...
}
Next to variable declarations
Write a comment next to variables that are not self-explanatory.
For example, a variable named interestRate doesn't need an explanation if you are using it to store interest rate, but if you have a variable named x, you need to explain what you use it for.
Comments are supposed to add meaning to your code. The following are examples of comments that do NOT have any purpose in a program because they are too obvious, and therefore must be avoided:
// variable declarations
int x; // integer variable
void printReport(); // function prototype
- Attach the output of your program as a comment at the end of your source code.
- Input file
Starting out with c++, Tony Gaddis, technical Fundamentals of Database Systems, Elmarsi & Navathe, technical The C++ Programming Language, Bjarne Stroustrup, technical The Pillars of the Earth, Ken Follett, historical fiction Fall of Giants, Ken Follet, historical fiction Mindset: The New Psychology of Success, Carol Dweck, psychology One Hundred Years of Solitude, Gabriel Garcia Marquez, fiction Ashes, Kenzo Kitakana, fiction The Dark Forest, Liu Cixin, science fiction Replay, Ken Grimwood, fantasy
The Output:
The output should have titles only, one title per line and they should be in alphabetical order.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started