My program is supposed to gather input from a .txt and run it through the code and output the information into a output.txt, but when it does read it, it outputs extra information for some reason and I am not sure why it is doing that.
Code:
#include<iostream>
#include<string>
#include<set>
#include<fstream>
#include "ArgumentManager.h"
using namespace std;
class Book{
public:
string genre,title,author,year;
Book(string given_genre, string given_title,string given_author,string given_year)
{
genre = given_genre;
title = given_title;
author = given_author;
year = given_year;
}
};
Book* processData(string dataRow)
{
string genre,title,author,year;
genre = title = author = year = "NULL";
string key = "", value = "";
int i=0;
bool isKey = true;
while(dataRow[i] != '}')
{
if(dataRow[i] == ' ' || dataRow[i] == '{')
{
i++;
continue;
}
if(dataRow[i] == ',')
{
if(key == "genre")
{
genre = value;
}
if(key == "title")
{
title = value;
}
if(key == "author")
{
if(title == "NULL")
{
return NULL;
}
author = value;
}
if(key == "year")
{
if(author == "NULL")
{
return NULL;
}
year = value;
}
key = "";
value = "";
isKey = true;
}
else if(dataRow[i] == ':')
{
isKey = false;
}
else{
if(isKey)
key+=dataRow[i];
else
value+=dataRow[i];
}
i++;
}
if(key == "year")
{
if(author == "NULL")
return NULL;
year = value;
}
if(key == "genre")
{
genre = value;
}
Book *book = new Book(genre,title,author,year);
return book;
}
pair<string,string> processCommand(string command)
{
bool isKey = true;
string key="",value="";
for(int i=0;i<command.length();i++)
{
if(command[i] == ' ')
continue;
if(command[i] == ':')
{
isKey = false;
continue;
}
if(isKey)
{
key+=command[i];
}
else
{
value+=command[i];
}
}
return make_pair(key,value);
}
int main(int argc, char* argv[])
{
ArgumentManager am(argc, argv);
ifstream input;
ofstream output;
string infileName = am.get("input");
string outfileName = am.get("output");
string cmdfilename = am.get("command");
input.open(infileName);
output.open(outfileName);
string dataRow;
int booksSize = 0;
Book* books[10000];
while(getline(input,dataRow))
{
Book* book = processData(dataRow);
if(book != NULL)
{
books[booksSize++] = book;
}
}
ifstream commands("command.txt");
string commandRow;
set<string> genre_bucket,title_bucket,author_bucket,year_bucket;
while(getline(commands,commandRow))
{
pair<string,string> command = processCommand(commandRow);
if(command.first == "genre")
genre_bucket.insert(command.second);
if(command.first == "title")
title_bucket.insert(command.second);
if(command.first == "author")
author_bucket.insert(command.second);
if(command.first == "year")
year_bucket.insert(command.second);
}
Book* selectedBooks[10000];
int selectBooksSize = 0;
for(int i=0; i < booksSize ;i++)
{
if(books[i]->genre !="NULL" && genre_bucket.count(books[i]->genre) == 0 && genre_bucket.size() > 0)
continue;
if(title_bucket.size() > 0 && title_bucket.count(books[i]->title) == 0)
continue;
if(author_bucket.size() > 0 && author_bucket.count(books[i]->author) == 0)
continue;
if(year_bucket.size() > 0 && year_bucket.count(books[i]->year) == 0)
continue;
selectedBooks[selectBooksSize++] = books[i];
}
for(int i=0; i < selectBooksSize ;i++)
{
if(selectedBooks[i]->genre != "NULL")
output<<"{genre:"<<selectedBooks[i]->genre<<","<<"title:"<<selectedBooks[i]->title<<","<<"author:"<<selectedBooks[i]->author<<","<<"year:"<<selectedBooks[i]->year<<"}"<<endl;
else
output<<"{title:"<<selectedBooks[i]->title<<","<<"author:"<<selectedBooks[i]->author<<","<<"year:"<<selectedBooks[i]->year<<"}"<<endl;
}
return 0;
}
the input is:
{genre:fiction,title:Oliver.Twist,author:Cha rles.Dickens,year:18 38}
{genre:child.literature,title:the.cat.in.the. hat,author:Dr.Seus s,year:1957}
{genre:child.lit e rature,ti tle:Green.Eggs.And.H am, author:Dr.Seu ss,year:1960}
{genre:ficti on,title:Great.Expec tations,autho r:Char les.Dickens,year:1861}
{genre:f ict i on, title:A.Chrismas.Carol,author:Charles.D icke ns,year:1843}
{genre:fict ion ,title : The.House.Of.The.Dead,a uthor:Fyod or.Dostoevsky,year:1861}
{genre:fiction,titl e:The.Old.Man.And.The.Sea,author:Ernest.Hemingway,year:1957}
{g e n re: f ic tion , title:Gr eat.Exp ectation s,auth or:Charles.Dickens,year:1861}
{genre:chi ld.literat u r e, ti tl e:G reen.Eggs.A nd.Ha m, auth or:Dr.Seuss,year:1960}
and the output SHOULD be this:
{genre:fiction,title:Great.Expectations,author:Charles.Dickens,year:1861}
{genre:fiction,title:The.House.Of.The.Dead,author:Fyodor.Dostoevsky,year:1861}
{genre:fiction,title:The.Old.Man.And.The.Sea,author:Ernest.Hemingway,year:1957}
{genre:fiction,title:Great.Expectations,author:Charles.Dickens,year:1861}
but instead it keep giving me this output:
{genre:fiction,title:Oliver.Twist,author:Charles.Dickens,year:1838}
{genre:child.literature,title:the.cat.in.the.hat,author:Dr.Seuss,year:1957}
{genre:child.literature,title:Green.Eggs.And.Ham,author:Dr.Seuss,year:1960}
{title:NULL,author:NULL,year:NULL}
{genre:fiction,title:Great.Expectations,author:Charles.Dickens,year:1861}
{genre:fiction,title:A.Chrismas.Carol,author:Charles.Dickens,year:1843}
{genre:fiction,title:The.House.Of.The.Dead,author:Fyodor.Dostoevsky,year:1861}
{genre:fiction,title:The.Old.Man.And.The.Sea,author:Ernest.Hemingway,year:1957}
{title:NULL,author:NULL,year:NULL}
{title:NULL,author:NULL,year:NULL}
{title:NULL,author:NULL,year:NULL}
{title:NULL,author:NULL,year:NULL}
{genre:fiction,title:Great.Expectations,author:Charles.Dickens,year:1861}
{genre:child.literature,title:Green.Eggs.And.Ham,author:Dr.Seuss,year:1960}
Aucun commentaire:
Enregistrer un commentaire