Reputation: 15
so I'm working on a project and I can't figure out what my issue is. I'm new to using and learning pointers, so it's probably something obvious I'm just not seeing, but when I run the print method it shows every item in the list with the same value as the last item. I'm not understanding why it's not only adding to the list but overwriting previous entries. (I haven't finished the rest of the functions so I didn't include them, everything under main was given as a template. Just trying to get over this hurdle currently as I can't seem to find answers online.)
#include <algorithm>
#include <iostream>
#include <time.h>
#include "CSVparser.hpp"
using namespace std;
double strToDouble(string str, char ch);
struct Bid {
string bidId; // unique identifier
string title;
string fund;
double amount;
Bid() {
amount = 0.0;
}
Bid* nextNode = 0;
};
class LinkedList {
private:
Bid* head;
Bid* tail;
int size;
public:
LinkedList();
virtual ~LinkedList();
void Append(Bid* bid);
void PrintList();
};
LinkedList::LinkedList() {
this->head = 0;
this->tail = 0;
this->size = 0;
return;
}
LinkedList::~LinkedList() {
}
void LinkedList::Append(Bid* bid) {
if (this->head == 0)
{
this->head = bid;
this->tail = bid;
}
else
{
this->tail->nextNode = bid;
this->tail = bid;
}
size += 1;
return;
}
void LinkedList::PrintList() {
Bid* currBid = this->head;
for (int i = 0; i < size; i++)
{
cout << currBid->bidId << ": " << currBid->title << " | " << currBid->amount << " | " << currBid->fund << endl;
currBid = currBid->nextNode;
}
return;
}
int main(int argc, char* argv[]) {
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;
}
Upvotes: 1
Views: 228
Reputation: 60238
You only have one Bid
object called bid
in the main
function. Every time you pass &bid
to the Append
function, you are passing the address of the same object. So your linked list contains a bunch of pointers that are all pointing to the same memory.
One way to fix that would be to allocate new memory within Append
, like this:
void LinkedList::Append(Bid* bid_arg) {
Bid *bid = new Bid{*bid_arg}; // allocate memory with the value
// pointed at by the pointer that is passed in
// append bid to the linked list
}
Upvotes: 2