C++ Help Needed! Please check requirements. Thanks! we are going to be reading a
ID: 3850793 • 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
#include <iostream>
#include <vector>
#include <fstream>
#include <stdlib.h>
using namespace std;
struct Comic{
string date;
string PUBLISHER;
string TITLE;
string price;
double cost;
};
vector<Comic> V;
Comic csv_to_comic(string str){
Comic c;
string temp = "";
int i;
for(i = 0;i<str.length();i++){
if(str[i]==','){
c.date = temp;
temp = "";i++;
break;
}else{
temp += str[i];
}
}
for(i;i<str.length();i++){
if(str[i]==','){
c.PUBLISHER = temp;
temp = "";i++;
break;
}else{
temp += str[i];
}
}
for(i;i<str.length();i++){
if(str[i]==','){
c.TITLE = temp;
temp = "";i++;
break;
}else{
temp += str[i];
}
}
c.price = str.substr(i);
if(str[i]=='$'){
c.cost = atof(str.substr(i+1).c_str());
}
return c;
}
void print_comic(Comic c){
cout << " " << c.date << "," << c.PUBLISHER << ",";
cout << c.TITLE << "," << c.price << endl;
}
void load(string filename){
ifstream file_stream;
file_stream.open(filename.c_str());
int lines = 0;
string temp;
while(getline(file_stream, temp)){
V.push_back(csv_to_comic(temp));
lines++;
}
cout << " " << "Loaded: " << lines << endl;
file_stream.close();
}
void add(string str){
Comic c = csv_to_comic(str);
V.push_back(c);
print_comic(c);
}
void search_publisher(string publisher){
bool found = false;
for(int i=0;i<V.size();i++){
if(V[i].PUBLISHER==publisher){
found = true; break;
}
}
if(!found){
cout << " " << "Sorry, " << publisher << " not found." << endl;
return;
}
cout << " " << "RELEASE DATE,PUBLISHER,TITLE,PRICE" << endl;
for(int i=0;i<V.size();i++){
if(V[i].PUBLISHER==publisher){
print_comic(V[i]);
found = true;
}
}
}
void search_title(string title){
bool found = false;
for(int i=0;i<V.size();i++){
if(V[i].TITLE.find(title) != string::npos){
found = true; break;
}
}
if(!found){
cout << " " << "Sorry, " << title << " not found." << endl;
return;
}
cout << " " << "RELEASE DATE,PUBLISHER,TITLE,PRICE" << endl;
for(int i=0;i<V.size();i++){
if(V[i].TITLE.find(title) != string::npos){
print_comic(V[i]);
found = true;
}
}
}
void search_error(string term, string query){
cout << " " << "Unknown search term: " << term << endl;
cout << " " << "Sorry, " << query << " not found." << endl;
}
void purchase(string query){
int c;
string title="", nums="";
double numi, sum=0;
int i=0;
while(i<query.length()){
while(i<query.length() && query[i]==' '){i++;}
while(query[i]!=','){
title += query[i];
i++;
}
i++;
while(i<query.length() && query[i]!=' '){
nums += query[i];
i++;
}
numi = atof(nums.c_str());
bool found = false;
for(int j=0;j<V.size();j++){
if(V[j].TITLE == title){
c = j;
found = true;
break;
}
}
if(found){
cout << " " << V[c].date << "," << V[c].PUBLISHER << ",";
cout << V[c].TITLE << "," << V[c].price << "," << nums << ",";
if(V[c].price[0]=='$'){
sum+=numi*V[c].cost;
cout << "$" << numi*V[c].cost << endl;
}else{
cout << V[c].price << endl;
}
title = ""; nums = "";
}else{
cout << " " << "Sorry, " << title << " not found." << endl;
}
}
cout << " " << "Total: $" << sum << endl;
}
int main(){
string command, argument;
while(cin >> command){
cout << "Command: " << command;
if(command=="load"){
getline(cin, argument);
cout << argument << endl;
argument = argument.substr(1);
load(argument);
}else if(command=="add"){
getline(cin, argument);
cout << argument << endl;
argument = argument.substr(1);
add(argument);
}else if(command=="search"){
cin >> argument;
cout << " " << argument;
if(argument=="PUBLISHER"){
getline(cin, argument);
cout << argument << endl;
argument = argument.substr(1);
search_publisher(argument);
}else if(argument=="TITLE"){
getline(cin, argument);
cout << argument << endl;
argument = argument.substr(1);
search_title(argument);
}else{
string argument2;
getline(cin, argument2);
cout << argument2 << endl;
argument2 = argument2.substr(1);
search_error(argument, argument2);
}
}else if(command=="purchase"){
getline(cin, argument);
cout << argument << endl;
argument = argument.substr(1);
purchase(argument);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.