Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

I'm having a little trouble with the find bid. At the menu I do option 2, correctly loads the csv file and 179 bids. Then

I'm having a little trouble with the find bid. At the menu I do option 2, correctly loads the csv file and 179 bids. Then option 3, 4, & 5(deletes bid) all work correctly but once I try option 4 again to find the bid I just removed it crashes instead of saying bidKey not found. Any help would be grateful.

Current warnings

Warning C26812 The enum type 'csv::DataType' is unscoped. Prefer 'enum class' over 'enum' (Enum.3). 67

Warning C28182 Dereferencing NULL pointer. 'current' contains the same NULL value as 'head' did. 168

Warning C4715 'LinkedList::Search': not all control paths return a value LinkedList 209

#include

#include #include

#include "CSVparser.hpp"

using namespace std;

//============================================================================ // Global definitions visible to all methods and classes //============================================================================

// forward declarations double strToDouble(string str, char ch);

// define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() { amount = 0.0; } };

//============================================================================ // Linked-List class definition //============================================================================

/** * Define a class containing data members and methods to * implement a linked-list. */ class LinkedList {

private: // FIXME (1): Internal structure for list entries, housekeeping variables struct Node { Bid bid; Node* next;

//default constructor Node() { next = nullptr; }

//initialize a node with a bid Node(Bid aBid) { bid = aBid; next = nullptr; } };

Node* head; Node* tail; int size = 0;

public: LinkedList(); virtual ~LinkedList(); void Append(Bid bid); void Prepend(Bid bid); void PrintList(); void Remove(string bidId); Bid Search(string bidId); int Size(); };

/** * Default constructor */ LinkedList::LinkedList() { // FIXME (2): Initialize housekeeping variables head = tail = nullptr; }

/** * Destructor */ LinkedList::~LinkedList() { }

/** * Append a new bid to the end of the list */ void LinkedList::Append(Bid bid) { // FIXME (3): Implement append logic Node* node = new Node(bid);

if (head == nullptr) { head = node; } else { if (tail != nullptr) { tail->next = node;

} } // new node is always the tail tail = node;

size++;

}

/** * Prepend a new bid to the start of the list */ void LinkedList::Prepend(Bid bid) { // FIXME (4): Implement prepend logic Node* node = new Node(bid);

if (head != nullptr) { node->next = head;

}

head = node;

size++; }

/** * Simple output of all bids in the list */ void LinkedList::PrintList() { // FIXME (5): Implement print logic Node* current = head; //loop over each node looking for a match while (current != nullptr) { cout << current->bid.bidId << ": " << current->bid.title << " | " << current->bid.amount << " | " << current->bid.fund << endl; current = current->next; } }

/** * Remove a specified bid * * @param bidId The bid id to remove from the list */ void LinkedList::Remove(string bidId) { // FIXME (6): Implement remove logic if (head != nullptr) { if (head->bid.bidId.compare(bidId) == 0) { Node* tempNode = head->next; delete head; head = tempNode; }

} Node* current = head;

//loop over each node looking for a match while (current ->next != nullptr) { if (current->next ->bid.bidId.compare(bidId) == 0) { //save the next node (one to be removed Node* tempNode = current->next; //make current node point beyond the next one (to be removed) current->next = tempNode->next;

//now delete temp node

delete tempNode;

//reduce count size--;

return;

} current = current->next; }

}

/** * Search for the specified bidId * * @param bidId The bid id to search for */ Bid LinkedList::Search(string bidId) { // FIXME (7): Implement search logic Node* current = head; //loop over each node looking for a match while (current != nullptr) { if (current->bid.bidId.compare(bidId) ==0) { return current -> bid; } current = current->next; } }

/** * Returns the current size (number of elements) in the list */ int LinkedList::Size() { return size; }

//============================================================================ // Static methods used for testing //============================================================================

/** * Display the bid information * * @param bid struct containing the bid info */ void displayBid(Bid bid) { cout << bid.bidId << ": " << 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 Id: "; cin.ignore(); getline(cin, bid.bidId);

cout << "Enter title: "; 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 LinkedList * * @return a LinkedList containing all the bids read */ void loadBids(string csvPath, LinkedList* list) { cout << "Loading CSV file " << csvPath << endl;

// initialize the CSV Parser csv::Parser file = csv::Parser(csvPath);

try { // loop to read rows of a CSV file for (unsigned int i = 0; i < file.rowCount(); i++) {

// initialize a bid using data from current row (i) Bid bid; bid.bidId = file[i][1]; bid.title = file[i][0]; bid.fund = file[i][8]; bid.amount = strToDouble(file[i][4], '$');

//cout << bid.bidId << ": " << bid.title << " | " << bid.fund << " | " << bid.amount << endl;

// add this bid to the end list->Append(bid); } } catch (csv::Error & e) { std::cerr << e.what() << std::endl; } }

double strToDouble(string str, char ch) { str.erase(remove(str.begin(), str.end(), ch), str.end()); return atof(str.c_str()); }

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

// process command line arguments string csvPath, bidKey; switch (argc) { case 2: csvPath = argv[1]; bidKey = "98109"; break; case 3: csvPath = argv[1]; bidKey = argv[2]; break; default: csvPath = "eBid_Monthly_Sales_Dec_2016.csv"; bidKey = "98109"; }

clock_t ticks;

LinkedList bidList;

Bid bid;

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 << " 4. Find Bid" << endl; cout << " 5. Remove Bid" << endl; cout << " 9. Exit" << endl; cout << "Enter choice: "; cin >> choice;

switch (choice) { case 1: bid = getBid(); bidList.Append(bid); displayBid(bid);

break;

case 2: ticks = clock();

loadBids(csvPath, &bidList);

cout << bidList.Size() << " bids read" << endl;

ticks = clock() - ticks; // current clock ticks minus starting clock ticks cout << "time: " << ticks << " milliseconds" << endl; cout << "time: " << ticks * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;

break;

case 3: bidList.PrintList();

break;

case 4: ticks = clock();

bid = bidList.Search(bidKey);

ticks = clock() - ticks; // current clock ticks minus starting clock ticks

if (!bid.bidId.empty()) { displayBid(bid); } else { cout << "Bid Id " << bidKey << " not found." << endl; }

cout << "time: " << ticks << " clock ticks" << endl; cout << "time: " << ticks * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;

break;

case 5: bidList.Remove(bidKey);

break; } }

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

return 0; }

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_2

Step: 3

blur-text-image_step3

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

Visual Basic6 Database Programming

Authors: John W. Fronckowiak, David J. Helda

1st Edition

ISBN: 0764532545, 978-0764532542

More Books

Students explore these related Databases questions