C++ Help Needed! Please check requirements. Thanks! we are going to be reading a
ID: 3849958 • Letter: C
Question
C++ Help Needed! Please check requirements. Thanks!
we are going to be reading and storing information about comic books. This data comes from http://www.comiclist.com (Links to an external site.)Links to an external site. and is just a copy and paste of their text listing of new comics. So you’ll be reading the data related to comic book and storing that data in an array of that struct type. For full credit you’ll use a dynamic array of structs that “grows” when it’s full. There is a provision to allow for the use of a static array for a passing grade, just not full credit.
Input
The input to the program will be a series of commands. The commands will be detailed below. The basic ideas are to load in a set of comic information, then be able to search, add or “purchase” the comics.
Commands
load - this command will be followed by a filename. The filename is the list of comics to read and store in the array. This may or may not be the first command and it could come more than once.
search - this command will be followed by one of two search terms. The two search terms are PUBLISHER and TITLE. If another search term is given then an error is output.
If the PUBLISHER search term is given then we are searching for a complete match of the publisher for the comic. (e.g use == to compare). If the publisher is found, then the information for that comic is displayed.
If the TITLE search term is given then we are searching for a partial match of the given title. (e.g. use find on the title.). If the title is found, then information about the comic is displayed.
In any case, if the title or publisher isn’t found, then a message is displayed about not finding what was searched for.
add - this command will be followed by a new comic book to be added to the list of comics. It is added at the end.
purchase - this command attempts to find the given title, by a complete match, and then if the comic has a value for its cost that isn’t AR, then a cost is computed. The number after the title is how many of that comic you want to purchase. In the output you will list the comic information, the number purchased and what that cost would be. After the last purchased comic you list a total. If an individual title isn’t found an error is displayed.
Here’s a sample of what that could look like:
A note about the purchase command the title for each comic to be purchased if ended with a comma. You can use this with the getline to be able to read the title. Then after the comma is a number indicating how many you want to purchase. Then there will be a space.
Each of the commands is a single word, you should use extraction to read them. After each command is a single space, in many of the cases you’ll need to ignore that space to make the command be able to find. For example, “Title” is not the same as “ Title”.
The format for the comic after the add command is the same format as the file you load, a function to read a single comment is a good idea.
Each command is terminated with a newline character.
Output
Each command will have output. Firstly each command is “echoed” so that you can see what the command was. The only exception to this is the purchase command. The echoing of that command is mixed in with the results for the command.
If the command file above is run this is the output:
The end of this shows the negative results for each of the commands. Again, spacing on a line doesn’t matter. As long as you have at least a space where I do, you’re fine. If you have space where I don’t or if you don’t have space where I do, then you’re not fine.
Requirements
You must declare the function void comicList( string input, string output ); in a file called comicList.h
You must create a struct for the comic book information.
You must create an array of that struct you made for requirement 1.
If you want full credit, the array of that struct must be dynamically allocated using a pointer.
When the array is full, you will double it before adding new data to the array.
You may not use smart pointers.
You may not use vector, list, or other STL storage container classes.
You may not use classes.
You must write functions for the major commands and other functions as appropriate.
You may not use global variables.
Explanation / Answer
Well, I wrote the core
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
struct Comic
{
string name;
float price;
};
void loadComics(string, vector<Comic>);
void readAComic(ifstream &, Comic &);
void searchByName(string, vector<Comic>, Comic&);
int main()
{
string comics = "comics.txt";
vector<Comic> comics_array{};
Comic c0{};
// Load
loadComics(comics, comics_array);
// Find
string searchFor = "Jackpot";
searchByName(searchFor, comics_array, c0);
cout << "Name: " << c0.name
<< ", Price: $" << c0.price << endl;
return 0;
}
void loadComics(string filename, vector<Comic> comics_arr)
{
ifstream in_file(filename);
Comic c0{};
// File exists validation
if (! in_file.is_open())
cerr << "Failed to open " << filename
<< "!" << endl;
else
cout << "Opened " << filename
<< " for reading." << endl;
while (! in_file.eof())
{
readAComic(in_file, c0);
if (in_file.gcount() == 0)
break;
//else
// cout << "Lemme read another comic." << endl;
comics_arr.push_back(c0);
}
};
void readAComic(ifstream &inf, Comic &c0)
{
string temp{};
char delim = ',';
// Get name
getline(inf, temp, delim);
//temp.erase(remove(temp.begin(), temp.end(), ' '), temp.end());
c0.name = temp;
// Get price
// Ignore ' $'
inf.ignore(2);
inf >> c0.price;
cout << "Read a comic: " << c0.name << endl;
};
void searchByName(string keyword, vector<Comic> comics_arr, Comic &c0)
{
vector<Comic>::iterator it = comics_arr.begin();
for (; it < comics_arr.end(); ++it)
{
size_t matches = (it->name).find(keyword);
if (matches == string::npos)
continue;
else
{
c0.name = it->name;
c0.price = it->price;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.