Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, This is a C++ program and I got a lot of help on the last part of it from a post on Chegg already

Hello,

This is a C++ program and I got a lot of help on the last part of it from a post on Chegg already but the very end of my code (in bold, case 3:) is throwing an error: jump to case label [-fpermissive]. There are a number of files included, one is a CSVparser.cpp and another is CSVparser.hpp that are referenced since there are Excel spreadsheets that holds the bid information. Can you please tell me what is wrong with the code? If cannot help please don't leave a comment, just let someone else answer it because this is all the information I can add.

Thank you.

#include #include #include #include

// FIXME (1): Reference the CSVParser library #include "CSVparser.hpp" #include "CSVparser.cpp"

using namespace std;

//============================================================================

// Global definitions visible to all methods and classes

//============================================================================

// forward declarations

double strToDouble(string str, char ch);

struct Bid {

string title;

string fund;

double amount;

Bid() {

amount = 0.0;

}

};

//============================================================================

// Static methods used for testing

//============================================================================

/**

* Display the bid information

*

* @param bid struct containing the bid info

*/

void displayBid(Bid bid) {

cout << bid.title << " | " << bid.amount << " | " << bid.fund << endl;

return;

}

/**

* Prompt user for bid information

*

* @return Bid struct containing the bid info

*/

Bid getBid() {

Bid bid;

cout << "Enter title: ";

cin.ignore();

getline(cin, bid.title);

cout << "Enter fund: ";

cin >> bid.fund;

cout << "Enter amount: ";

cin.ignore();

string strAmount;

getline(cin, strAmount);

bid.amount = strToDouble(strAmount, '$');

return bid;

}

/**

* Load a CSV file containing bids into a container

*

* @param csvPath the path to the CSV file to load

* @return a container holding all the bids read

*/

vector loadBids(string csvPath) {

// FIXME (2): Define a vector data structure to hold a collection of bids.

cout << "Loading CSV file" << csvPath << endl;

vector bids;

// initialize the CSV Parser using the given path

csv::Parser file = csv::Parser(csvPath);

for (int i = 0; i < file.rowCount(); i++) {

// FIXME (3): create a data structure to hold data from each row and add to vector

Bid bid;

bid.title = file[i][0];

bid.fund = file[i][1];

bid.amount = strToDouble(file[i][2], ',');

bids.push_back(bid);

}

return bids;

}

/**

* Simple C function to convert a string to a double

* after stripping out unwanted char

*

* credit: http://stackoverflow.com/a/24875936

*

* @param ch The character to strip out

*/

double strToDouble(string str, char ch) {

str.erase(remove(str.begin(), str.end(), ch), str.end());

return atof(str.c_str());

}

/**

* The one and only main() method

*/

int main(int argc, char* argv[]) {

// process command line arguments

string csvPath;

switch (argc) {

case 2:

csvPath = argv[1];

break;

default:

csvPath = "eBid_Monthly_Sales_Dec_2016.csv";

}

// FIXME (4): Define a vector to hold all the bids

vector bids;

// FIXME (7a): Define a timer variable

clock_t begin;

int choice = 0;

while (choice != 9) {

cout << "Menu:" << endl;

cout << " 1. Enter a Bid" << endl;

cout << " 2. Load Bids" << endl;

cout << " 3. Display All Bids" << endl;

cout << " 9. Exit" << endl;

cout << "Enter choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "Not currently implemented." << endl;

break;

case 2:

// FIXME (7b): Initialize a timer variable before loading bids

begin = clock();

// FIXME (5): Complete the method call to load the bids

loadBids(csvPath);

// FIXME (7c): Calculate elapsed time and display result

clock_t end = clock();

double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;

cout <<"Elapsed time: " << elapsed_secs;

break;

case 3:

// FIXME (6): Loop and display the bids read

for (int i = 0; i < bids.size(); ++i) {

displayBid(bids[i]); }

cout << endl;

break;

}

}

cout << "Good bye." << endl;

return 0;

}

******************************************

#include #include #include #include "CSVparser.hpp"

namespace csv {

Parser::Parser(const std::string &data, const DataType &type, char sep) : _type(type), _sep(sep) { std::string line; if (type == eFILE) { _file = data; std::ifstream ifile(_file.c_str()); if (ifile.is_open()) { while (ifile.good()) { getline(ifile, line); if (line != "") _originalFile.push_back(line); } ifile.close();

if (_originalFile.size() == 0) throw Error(std::string("No Data in ").append(_file)); parseHeader(); parseContent(); } else throw Error(std::string("Failed to open ").append(_file)); } else { std::istringstream stream(data); while (std::getline(stream, line)) if (line != "") _originalFile.push_back(line); if (_originalFile.size() == 0) throw Error(std::string("No Data in pure content"));

parseHeader(); parseContent(); } }

Parser::~Parser(void) { std::vector::iterator it;

for (it = _content.begin(); it != _content.end(); it++) delete *it; }

void Parser::parseHeader(void) { std::stringstream ss(_originalFile[0]); std::string item;

while (std::getline(ss, item, _sep)) _header.push_back(item); }

void Parser::parseContent(void) { std::vector::iterator it; it = _originalFile.begin(); it++; // skip header

for (; it != _originalFile.end(); it++) { bool quoted = false; int tokenStart = 0; unsigned int i = 0;

Row *row = new Row(_header);

for (; i != it->length(); i++) { if (it->at(i) == '"') quoted = ((quoted) ? (false) : (true)); else if (it->at(i) == ',' && !quoted) { row->push(it->substr(tokenStart, i - tokenStart)); tokenStart = i + 1; } }

//end row->push(it->substr(tokenStart, it->length() - tokenStart));

// if value(s) missing if (row->size() != _header.size()) throw Error("corrupted data !"); _content.push_back(row); } }

Row &Parser::getRow(unsigned int rowPosition) const { if (rowPosition < _content.size()) return *(_content[rowPosition]); throw Error("can't return this row (doesn't exist)"); }

Row &Parser::operator[](unsigned int rowPosition) const { return Parser::getRow(rowPosition); }

unsigned int Parser::rowCount(void) const { return _content.size(); }

unsigned int Parser::columnCount(void) const { return _header.size(); }

std::vector Parser::getHeader(void) const { return _header; }

const std::string Parser::getHeaderElement(unsigned int pos) const { if (pos >= _header.size()) throw Error("can't return this header (doesn't exist)"); return _header[pos]; }

bool Parser::deleteRow(unsigned int pos) { if (pos < _content.size()) { delete *(_content.begin() + pos); _content.erase(_content.begin() + pos); return true; } return false; }

bool Parser::addRow(unsigned int pos, const std::vector &r) { Row *row = new Row(_header);

for (auto it = r.begin(); it != r.end(); it++) row->push(*it); if (pos <= _content.size()) { _content.insert(_content.begin() + pos, row); return true; } return false; }

void Parser::sync(void) const { if (_type == DataType::eFILE) { std::ofstream f; f.open(_file, std::ios::out | std::ios::trunc);

// header unsigned int i = 0; for (auto it = _header.begin(); it != _header.end(); it++) { f << *it; if (i < _header.size() - 1) f << ","; else f << std::endl; i++; } for (auto it = _content.begin(); it != _content.end(); it++) f << **it << std::endl; f.close(); } }

const std::string &Parser::getFileName(void) const { return _file; } /* ** ROW */

Row::Row(const std::vector &header) : _header(header) {}

Row::~Row(void) {}

unsigned int Row::size(void) const { return _values.size(); }

void Row::push(const std::string &value) { _values.push_back(value); }

bool Row::set(const std::string &key, const std::string &value) { std::vector::const_iterator it; int pos = 0;

for (it = _header.begin(); it != _header.end(); it++) { if (key == *it) { _values[pos] = value; return true; } pos++; } return false; }

const std::string Row::operator[](unsigned int valuePosition) const { if (valuePosition < _values.size()) return _values[valuePosition]; throw Error("can't return this value (doesn't exist)"); }

const std::string Row::operator[](const std::string &key) const { std::vector::const_iterator it; int pos = 0;

for (it = _header.begin(); it != _header.end(); it++) { if (key == *it) return _values[pos]; pos++; } throw Error("can't return this value (doesn't exist)"); }

std::ostream &operator<<(std::ostream &os, const Row &row) { for (unsigned int i = 0; i != row._values.size(); i++) os << row._values[i] << " | ";

return os; }

std::ofstream &operator<<(std::ofstream &os, const Row &row) { for (unsigned int i = 0; i != row._values.size(); i++) { os << row._values[i]; if (i < row._values.size() - 1) os << ","; } return os; } }

****************************************************************************************

#ifndef _CSVPARSER_HPP_

# define _CSVPARSER_HPP_

# include

# include

# include

# include

# include

namespace csv

{

class Error : public std::runtime_error

{

public:

Error(const std::string &msg):

std::runtime_error(std::string("CSVparser : ").append(msg))

{

}

};

class Row

{

public:

Row(const std::vector &);

~Row(void);

public:

unsigned int size(void) const;

void push(const std::string &);

bool set(const std::string &, const std::string &);

private:

const std::vector _header;

std::vector _values;

public:

template

const T getValue(unsigned int pos) const

{

if (pos < _values.size())

{

T res;

std::stringstream ss;

ss << _values[pos];

ss >> res;

return res;

}

throw Error("can't return this value (doesn't exist)");

}

const std::string operator[](unsigned int) const;

const std::string operator[](const std::string &valueName) const;

friend std::ostream& operator<<(std::ostream& os, const Row &row);

friend std::ofstream& operator<<(std::ofstream& os, const Row &row);

};

enum DataType {

eFILE = 0,

ePURE = 1

};

class Parser

{

public:

Parser(const std::string &, const DataType &type = eFILE, char sep = ',');

~Parser(void);

public:

Row &getRow(unsigned int row) const;

unsigned int rowCount(void) const;

unsigned int columnCount(void) const;

std::vector getHeader(void) const;

const std::string getHeaderElement(unsigned int pos) const;

const std::string &getFileName(void) const;

public:

bool deleteRow(unsigned int row);

bool addRow(unsigned int pos, const std::vector &);

void sync(void) const;

protected:

void parseHeader(void);

void parseContent(void);

private:

std::string _file;

const DataType _type;

const char _sep;

std::vector _originalFile;

std::vector _header;

std::vector _content;

public:

Row &operator[](unsigned int row) const;

};

}

#endif /*!_CSVPARSER_HPP_*/

****************************************************

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

More Books

Students also viewed these Databases questions

Question

List the primary tools of consumer and trade promotions.

Answered: 1 week ago

Question

What are the roles and responsibilities for SITE accountability?

Answered: 1 week ago