I have some C++ code dealing with a card game and it's segfaulting and I can't figure out why. I tried running it in MSVS 2012 to get a more clear error and it threw a "Vector is not dereferencable." I tried doing some research but I'm lost on where the error is coming from. I'm guessing the bug isn't in the push_back but rather somewhere else as the problem line works most of the time. The error occurs in Table::hit(). After looking through the code I think the bug may possibly lie in Hand::split() but I am not sure and I am even less sure how to fix it. Thanks!
class Hand
{
public:
std::vector<Card> hand;
bool active, doubled, isBlackjack, busted;
Hand() : active(true), doubled(false), isBlackjack(false), busted(false) { }
Hand(const Hand& _hand);
void removeCard(Card card) { hand.erase( hand.begin() ); }
Card split();
void clear();
int cards() const { return hand.size(); }
int getHandValue() const;
Card at(int pos) { return hand[pos]; }
friend std::ostream& operator<< (std::ostream& out, const Hand& hand);
friend std::ostream& operator<< (std::ostream& out, const std::vector<Hand>& hands);
};
Hand::Hand(const Hand& _hand)
: hand(_hand.hand), active(_hand.active), doubled(_hand.doubled), isBlackjack(_hand.isBlackjack), busted(_hand.busted)
{ }
Card Hand::split()
{ //COULD THE BELOW CODE BE CAUSING ISSUES?
Card card = hand[0];
hand.erase( hand.begin() );
return card;
}
void Hand::clear()
{
active = true;
hand.clear();
}
int Hand::getHandValue() const
{
int value(0), aces(0);
for(std::vector<Card>::const_iterator it = hand.cbegin(); it != hand.cend(); ++it)
{
value += getCardValue(it->getRank());
if(it->getRank() == ACE)
++aces;
}
while(aces > 0 && value > 21)
{
value -= 10;
--aces;
}
return value;
}
std::ostream& operator<< (std::ostream& out, const Hand& hand)
{
for(std::vector<Card>::const_iterator it = hand.hand.begin(); it != hand.hand.end(); ++it)
{
out << *it << " ";
}
out << hand.getHandValue();
return out;
}
std::ostream& operator<< (std::ostream& out, const std::vector<Hand>& hands)
{
for(std::vector<Hand>::const_iterator it = hands.begin(); it != hands.end(); ++it)
{
out << *it << " ";
}
return out;
}
class Table
{
protected:
Shoe shoe;
Strategy strat;
Hand dealer;
std::vector< Hand > player;
double balance, blackjackPayoff;
bool hitSoft17;
void deal();
void hit(int split=0);
void split(int split=0);
double stand();
Action getUserAction();
bool isCurrentGame();
public:
Table(int decks, int _balance);
double bet;
void play();
void print();
void simulate(std::string fileName, int hands);
};
Table::Table(int decks, int _balance)
: shoe(decks), balance(_balance), blackjackPayoff(1.5), hitSoft17(true), bet(10)
{}
bool Table::isCurrentGame()
{
for(std::vector<Hand>::iterator it = player.begin(); it != player.end(); ++it)
{
if(it->active)
return true;
}
return false;
}
void Table::deal()
{
dealer.clear();
player.clear();
dealer.addCard( shoe.pullCard(true) );
dealer.addCard( shoe.pullCard() );
player.push_back(Hand());
player[0].addCard( shoe.pullCard() );
player[0].addCard( shoe.pullCard() );
if(player[0].getHandValue() == 21 && dealer.getHandValue() == 21)
{
player[0].active = false;
}
else if(player[0].getHandValue() == 21)
{
player[0].active = false;
}
else if(dealer.getHandValue() == 21)
{
player[0].active = false;
}
}
void Table::hit(int split)
{
player[split].hand.push_back( shoe.pullCard() ); //SEGFAULT HERE
if(player[split].getHandValue() > 21)
{
player[split].active = false;
}
}
void Table::split(int split)
{
player.push_back(Hand());
player[player.size() - 1].hand.push_back( player[split].split() );
player[split].hand.push_back( shoe.pullCard() );
player[player.size() - 1].hand.push_back( shoe.pullCard() );
if(player[split].getHandValue() == 21)
{
player[split].active = false;
}
else if(player[player.size() - 1].getHandValue() == 21)
{
player[player.size() - 1].active = false;
}
}
double Table::stand()
{
double winnings(0.0);
bool allNats(true);
//dealer.push_back( shoe.pullCard() );
for(std::vector<Hand>::iterator it = player.begin(); it != player.end(); ++it)
{
if(it->cards() == 2 && it->getHandValue() == 21)
{
winnings += bet * blackjackPayoff;
it->isBlackjack = true;
}
else
{
allNats = false;
if(it->getHandValue() > 21)
it->busted = true;
}
}
if(!allNats)
{
while(dealer.getHandValue() <= 17)
{
if(dealer.getHandValue() == 17)
{
//dealer has 17 and doesnt hit soft 17s so hes not going to draw anymore cards
if( !hitSoft17 || dealer.cards() != 2 || !(dealer.hand.at(0) == ACE || dealer.hand.at(1) == ACE) )
break;
}
dealer.addCard( shoe.pullCard() );
}
}
for(std::vector<Hand>::iterator it = player.begin(); it != player.end(); ++it)
{
if(!it->isBlackjack)
{
if(it->busted)
{
if(it->doubled)
winnings -= 2.0 * bet;
else
winnings -= bet;
}
else if(dealer.getHandValue() > 21 || dealer.getHandValue() < it->getHandValue())
{
if(it->doubled)
winnings += 2.0 * bet;
else
winnings += bet;
}
else if(dealer.getHandValue() > it->getHandValue())
{
if(it->doubled)
winnings -= 2.0 * bet;
else
winnings -= bet;
}
}
}
//only other scenario is a push which we dont do anythign for
return winnings;
}
void Table::simulate(std::string fileName, int hands)
{
int i(0), countHL;
double trueCountHL;
std::ofstream ofs(fileName);
Action action;
while(i < hands)
{
deal();
VisibleCards vc(player[0].at(0), player[0].at(1), dealer.at(0));
while(isCurrentGame())
{
int j(0), q(1);
countHL = shoe.getCountHL();
trueCountHL = shoe.getTrueCountHL();
for(int l = 0; l < q; ++l)
{
while(player[l].active)
{
if(player[l].cards() == 2)
action = getBasicStrategy(player[l].getHandValue(), dealer.at(0).getCardValue(),
player[l].at(0).getCardValue(), player[l].at(1).getCardValue());
else
action = getBasicStrategy(player[l].getHandValue(), dealer.at(0).getCardValue());
switch(action)
{
case HIT:
hit(j);
break;
case DOUBLE:
hit(j);
if(player[l].cards() == 3)
{
player[l].doubled = true;
player[l].active = false;
}
break;
case SPLIT:
if(player.size() <= 4)
{
split(j);
++q;
}
else
hit(j);
break;
case STAND:
player[l].active = false;
break;
}
++j;
}
}
}
double winnings = stand();
balance += winnings;
ofs << std::endl << std::fixed << vc << "," << winnings << "," << std::setprecision(2) << trueCountHL
<< "," << balance << "," << std::setprecision(0) << countHL << "," << shoe.getRemainingDecks();
ofs << std::endl << 1;
//BlackjackData bjd(winnings, balance, shoe.getTrueCountHL(), shoe.getCountHL(), HIT);
//strat.addData(vc, bjd);
++i;
}
strat.print(fileName);
}
Looks like you're trying to get the first card from hand with hand[0] and hand.erase(hand.begin()) without first checking that there IS a first card. If hand is empty (as is the case with a newly created Hand from the constructor), this causes undefined behavior, which may well manifest as a crash at some later time.
I stared at this for a day and couldn't figure it out, stepped out and realize I had an old variable "j" that I was using to access the vector instead of "l" when I was calling the "hit" function.... Thanks everyone for the help.
Related
I'm doing a console checkers in C++. The problem is I can't refer to methods of forward-declared class.
Here's a header of Board class.
#define BOARD_H
#include "Square.h"
#include <vector>
#include <map>
class Man;
typedef std::vector<Square*> Column;
class Board
{
const int BOARD_SIZE = 8;
std::vector<Column> squares;
std::map<Location, Square*> locationSquareMap; //still kinda secret for me
public:
Board();
~Board();
//Board(Board& board);
void printBoard();
std::map<Location, Square*> getLocationSquareMap();
};
#endif
And it's implementation.
Here (printBoard()) I try to access man object via pointer, but I cannot do this because of incomplete type error.
#include "Board.h"
Board::Board()
{ //white black
for (int row = 0; row < BOARD_SIZE; row++)
{
squares.push_back(Column());
for (int column = 0; column < BOARD_SIZE; column++)
{
Location currentLocation(File(column), row);
SquareColor currentSquareColor = SquareColor::WHITE;
if ((column + row) % 2 == 0) //lol the bug was here, forgot about brackets
{
currentSquareColor = SquareColor::BLACK;
}
Square* currentSquare = new Square(currentLocation, currentSquareColor);
locationSquareMap.insert(std::make_pair(currentSquare->getLocation(), currentSquare));
squares.at(row).push_back(currentSquare);
}
}
}
Board::~Board()
{
}
void Board::printBoard()
{
for (int i = squares.size() - 1; i != -1; i--)
{
for (int g = squares.at(i).size() - 1; g != -1; g--)
{
std::cout << *(squares.at(i).at(g)) << std::endl;
}
}
std::cout << " ";
for (int i = 0; i < BOARD_SIZE; i++)
{
switch (File(i))
{
case File::A : std::cout << "A "; break;
case File::B : std::cout << "B "; break;
case File::C : std::cout << "C "; break;
case File::D : std::cout << "D "; break;
case File::E : std::cout << "E "; break;
case File::F : std::cout << "F "; break;
case File::G : std::cout << "G "; break;
case File::H : std::cout << "H "; break;
default: std::cout << " "; break;
}
}
std::cout << std::endl << std::endl;
for (int i = squares.size() - 1; i != -1; i--)
{
std::cout << i << " ";
for(int g = 0; g < squares.at(i).size(); g++)
{
Man* currentMan;
if (squares.at(i).at(g)->getIsOccupied())
{
currentMan = squares.at(i).at(g)->getCurrentMan();
//currentMan-> can't refer to methods
}
else
{
std::cout << "_ ";
}
}
std::cout << std::endl;
}
}
std::map<Location, Square*> Board::getLocationSquareMap()
{
return locationSquareMap;
}
Man's header:
#ifndef MAN_H
#define MAN_H
#include "ManColorEnum.cpp"
#include "Square.h"
#include <iostream>
#include <string>
#include <vector>
#include "LocationFactory.h"
#include "Board.h"
class Man
{
ManColor color;
Square* currentSquare;
bool isKing;
public:
Man(ManColor color, Square* square);
~Man();
//Man(Man& man);
Square* getCurrentSquare();
void setCurrentSquare(Square* square);
bool getIsKing();
void setIsKing();
ManColor getColor();
void makeMove(Square* square);
std::vector<Location> getValidMoves(Board* board);
friend std::ostream& operator<<(std::ostream& ostream, const Man& man);
};
#endif
Man's .cpp file:
#include "Man.h"
Man::Man(ManColor color, Square* square)
{
this->color = color;
this->currentSquare = square;
this->isKing = false;
}
Man::~Man()
{
//dtor
}
ManColor Man::getColor()
{
return color;
}
std::vector<Location> Man::getValidMoves(Board* board)
{
std::vector<Location> moveCandidates{};
if (!isKing)
{
Location currentLocation = this->currentSquare->getLocation();
std::map<Location, Square*> squareMap = board->getLocationSquareMap();
moveCandidates.push_back(LocationFactory::createLocation(currentLocation, 1, 1)); //move one right forward
moveCandidates.push_back(LocationFactory::createLocation(currentLocation, -1, 1)); //move one left forward
moveCandidates.push_back(LocationFactory::createLocation(currentLocation, 2, 2)); //capture forward right
moveCandidates.push_back(LocationFactory::createLocation(currentLocation, -2, 2)); //capture forward left
moveCandidates.push_back(LocationFactory::createLocation(currentLocation, 2, -2)); //capture backwards right
moveCandidates.push_back(LocationFactory::createLocation(currentLocation, -2, -2)); //capture backwards left
//TODO have to capture
for (int i = 0; i < moveCandidates.size(); i++)
{
//here I filter for moves to exist on the board
if (squareMap.count(moveCandidates.at(i)) == 0)
{
moveCandidates.erase(moveCandidates.begin() + i);
}
//here I filter occupied squares
if (squareMap[moveCandidates.at(i)]->getIsOccupied())
{
moveCandidates.erase(moveCandidates.begin() + i);
}
//here I filter capture moves
File currentFile = moveCandidates.at(i).getFile();
int currentRank = moveCandidates.at(i).getRank();
Square* preSquare = nullptr;
for (std::map<Location, Square*>::iterator it = squareMap.begin(); it != squareMap.end(); it++)
{
//check capture forward right
if (int(it->first.getFile()) == int(currentFile) + 2 && it->first.getRank() == currentRank + 2)
{
for (std::map<Location, Square*>::iterator itr = squareMap.begin(); itr != squareMap.end(); itr++)
{
if (int(itr->first.getFile()) == int(currentFile) + 1 && itr->first.getRank() == currentRank + 1)
{
preSquare = itr->second;
break; //check to be careful here
}
}
//if (it->second->getIsOccupied()
// || (preSquare->getIsOccupied() && preSquare->getCurrentMan()->color == color)
// || (!it->second->getIsOccupied() && !preSquare->getIsOccupied()))
//{
// moveCandidates.erase(moveCandidates.begin() + i);
// break; //check to be careful here
//}
}
//check capture backward right
else if (int(it->first.getFile()) == int(currentFile) + 2 && it->first.getRank() == currentRank - 2)
{
for (std::map<Location, Square*>::iterator itr = squareMap.begin(); itr != squareMap.end(); itr++)
{
if (int(itr->first.getFile()) == int(currentFile) + 1 && itr->first.getRank() == currentRank - 1)
{
preSquare = itr->second;
break;
}
}
}
//check capture forward left
else if (int(it->first.getFile()) == int(currentFile) -2 && it->first.getRank() == currentRank + 2)
{
for (std::map<Location, Square*>::iterator itr = squareMap.begin(); itr != squareMap.end(); itr++)
{
if (int(itr->first.getFile()) == int(currentFile) - 1 && itr->first.getRank() == currentRank + 1)
{
preSquare = itr->second;
break;
}
}
}
//check capture backwards left
else if (int(it->first.getFile()) == int(currentFile) - 2 && it->first.getRank() == currentRank - 2)
{
for (std::map<Location, Square*>::iterator itr = squareMap.begin(); itr != squareMap.end(); itr++)
{
if (int(itr->first.getFile()) == int(currentFile) - 1 && itr->first.getRank() == currentRank - 1)
{
preSquare = itr->second;
break;
}
}
}
if (it->second->getIsOccupied()
|| (preSquare->getIsOccupied() && preSquare->getCurrentMan()->color == color)
|| (!it->second->getIsOccupied() && !preSquare->getIsOccupied()))
{
moveCandidates.erase(moveCandidates.begin() + i);
break; //check to be careful here
}
}
}
}
return moveCandidates;
}
Square* Man::getCurrentSquare()
{
return currentSquare;
}
void Man::setCurrentSquare(Square* square)
{
this->currentSquare = square;
}
bool Man::getIsKing()
{
return isKing;
}
void Man::setIsKing()
{
isKing = true;
}
void Man::makeMove(Square* square)
{
this->currentSquare->reset();
this->currentSquare = square;
}
std::ostream& operator<<(std::ostream& ostream, const Man& man)
{
std::string colorStr;
switch (man.color)
{
case ManColor::BLACK: colorStr = "BLACK"; break;
case ManColor::WHITE: colorStr = "WHITE"; break;
default: colorStr = "COLOR";
}
return std::cout << "Man { color=" << colorStr << " } " << std::endl;
}
Put forward class declarations in header files and include headers in source files.
first time posting. Thanks in advance for your correspondence and patience.
I need to prevent the copy of an object, if that object already exists in my array.
I am beyond my depth as how to compare these values and prevent copy.
My next step is trying: unique_ptr or shared_ptr or compare() or iterator.
NO VECTORS.... I NEED TO ALLOCATE AND DE-ALLOCATE EVERYTHING.
ConfirmationSender.h
class ConfirmationSender {
Reservation** ppCon;
size_t cCnt;
public:
ConfirmationSender();
~ConfirmationSender();
ConfirmationSender& operator+=(const Reservation& res);
// ConfirmationSender& operator-=(const Reservation& res);
void display(std::ostream& os) const;
};
std::ostream& operator<<(std::ostream& os, const ConfirmationSender& obj);
ConfirmationSender.cpp
ConfirmationSender& ConfirmationSender::operator+=(const Reservation& res) {
if (cCnt == 0) {
++cCnt;
ppCon = new Reservation* [cCnt];
ppCon[0] = new Reservation(res);
}
else {
Reservation** temp1 = nullptr;
temp1 = new Reservation * [cCnt];
for (size_t i = 0; i < cCnt; i++) {
temp1[i] = ppCon[i];
}
++cCnt;
delete[] ppCon;
ppCon = new Reservation * [cCnt];
for (size_t i = 0; i < cCnt - 1; i++) {
ppCon[i] = temp1[i];
}
ppCon[cCnt - 1] = new Reservation(res);
delete[] temp1;
temp1 = nullptr;
/*
NEED TO IMPLEMENT CONDITION
SO THE VALUE I COPY
CANNOT BE A VALUE ALREADY EXISTING IN ARRAY
*/
}
return *this;
}
class Reservation {
std::string id, name, email;
int seats, day, hour;
public:
Reservation();
Reservation(const std::string& m_res);
void display(std::ostream& os) const;
};
std::ostream& operator<<(std::ostream& os, const Reservation& obj);
Reservation::Reservation() {
seats = 0;
day = 0;
hour = 0;
};
Reservation::Reservation(const std::string& m_res) {
std::string t_str;
t_str = m_res;
t_str.erase(std::remove(t_str.begin(), t_str.end(), ','), t_str.end());
istringstream iss(t_str);
std::string buffer = {};
static int count = 0;
while (iss >> buffer) {
switch (count % 6) {
case 0: id = buffer; break;
case 1: name = buffer; break;
case 2: email = buffer; break;
case 3: seats = atoi(buffer.c_str()); break;
case 4: day = atoi(buffer.c_str()); break;
case 5: hour = atoi(buffer.c_str()); break;
}
count++;
}
buffer.clear();
iss.str(std::string());
}
void Reservation::display(std::ostream& os) const {
string menu_type;
if (hour >= 6 && hour <= 9) { menu_type = "Breakfast"; }
else if (hour >= 11 && hour <= 15) { menu_type = "Lunch"; }
else if (hour >= 17 && hour <= 21) { menu_type = "Dinner"; }
else { menu_type = "Drinks"; }
os << "Reservation "
<< id << " "
<< name << " <"
<< email << "> "
<< menu_type << " on day "
<< day << " # "
<< hour << ":00 for "
<< seats << " people." << '\n';
}
std::ostream& operator<<(std::ostream& os, const Reservation& obj) {
obj.display(os);
return os;
}
main.cpp
// Confirmation Sender
{
std::cout << "CS: Testing Operators\n";
std::cout << "==========================\n";
sender1 += *ppReservations[5];
sender1 += *ppReservations[16];
sender1 += *ppReservations[16];
// THE SECOND [16] SHOULD NOT BE COPIED BY ADDITION OPERATOR
std::cout << sender1;
std::cout << "==========================\n\n";
}
I am creating a dynamic array class that holds polynomials. The problem I am having right now is when I run my code, once it hits the return statement in main it begins to call the destructor and begins to free the memory from each instance starting with C. It deletes C fine, but when it gets to B I get a heap corruption error. I have tried walking through the code, but I cannot see where the corruption is happening. Can anyone help me? The exact error it gives me is "CRT detected that the application wrote to memory after end of heap buffer."
*Edit: I am more than happy to get peoples recommendations to help make my code better, but remember this is for a class and has specific rules. I cannot use anything from STL. I love any criticism you can give me.
///////////////////////////Header/////////////////////////////
class Poly
{
friend std::ostream& operator<<(std::ostream& output, const Poly& pNomial);
public:
Poly();
Poly(const int& coeff, const int& degree = 0);
Poly(const Poly& copy);
~Poly();
void setCoeff(const int& coeff, const int& degree);
bool isEmpty()const;
Poly& operator=(const Poly& pNomial);
private:
int* coeffs;
int highestDegree;
};
///////////////////////////CPP////////////////////////
#include "poly.h"
Poly::Poly()
{
highestDegree = 0;
coeffs = new int[highestDegree+1]();
}
Poly::Poly(const int & coeff, const int & degree)
{
if (degree >= 0)
{
highestDegree = degree;
coeffs = new int[highestDegree + 1]();
coeffs[degree] = coeff;
}
else
{
highestDegree = 0;
coeffs = new int[highestDegree + 1]();
}
}
Poly::Poly(const Poly& copy)
{
highestDegree = copy.highestDegree;
coeffs = new int[highestDegree + 1]();
for (int i = 0; i < copy.highestDegree + 1; i++)
{
coeffs[i] = copy.coeffs[i];
}
}
Poly::~Poly()
{
delete[] coeffs;
}
void Poly::setCoeff(const int& coeff, const int& degree)
{
if (degree > this->highestDegree)
{
Poly temp = *this;
delete[] this->coeffs;
this->highestDegree = degree;
this->coeffs = new int[highestDegree]();
for (int i = 0; i < temp.highestDegree + 1; i++)
{
this->coeffs[i] = temp.coeffs[i];
}
}
if (degree >= 0)
{
this->coeffs[degree] = coeff;
}
}
bool Poly::isEmpty()const
{
bool check = true;
for (int i = 0; i < highestDegree + 1 && check; i++)
{
if (coeffs[i] != 0)
{
check = false;
}
}
return check;
}
Poly & Poly::operator=(const Poly& pNomial)
{
if (this != &pNomial)
{
delete[] this->coeffs;
this->highestDegree = pNomial.highestDegree;
this->coeffs = new int[this->highestDegree + 1]();
for (int i = 0; i < pNomial.highestDegree + 1; i++)
{
this->coeffs[i] = pNomial.coeffs[i];
}
}
return *this;
}
std::ostream& operator<<(std::ostream& output, const Poly& poly)
{
if (!poly.isEmpty())
{
for (int i = poly.highestDegree; i >= 0; i--)
{
if (i == 1 && poly.coeffs[i] != 0)
{
if (poly.coeffs[i] >= 1)
{
output << " +" << poly.coeffs[i] << "x";
}
else
{
output << " " << poly.coeffs[i] << "x";
}
}
else if (i == 0 && poly.coeffs[i] != 0)
{
if (poly.coeffs[i] >= 1)
{
output << " +" << poly.coeffs[i];
}
else
{
output << " " << poly.coeffs[i];
}
}
else if (poly.coeffs[i] != 0)
{
if (poly.coeffs[i] >= 1)
{
output << " +" << poly.coeffs[i] << "x^" << i;
}
else
{
output << " " << poly.coeffs[i] << "x^" << i;
}
}
}
}
else
{
output << " 0";
}
return output;
}``
/////////////////////////////////Main/////////////////////////
#include "poly.h"
#include <iostream>
int main()
{
Poly A, B(5, 7), C(2);
B.setCoeff(2, 10);
B.setCoeff(1, 3);
B.setCoeff(5, 4);
std::cout << A << std::endl;
std::cout << B << std::endl;
std::cout << C << std::endl;
return 0;
}
I must say, that I agree with comments and you should seriously look into proper lifetime management of resources you use in the Poly class.
To answer the problem you're facing now have a look at the setCoeff() function.
this->coeffs = new int[highestDegree]();
should be changed to,
this->coeffs = new int[highestDegree + 1]();
With your current implementation, you allocate the array with highestDegree and in your for loop you access temp.coeffs[highestDegree] which is out of bounds access i.e. you loop till i < temp.highestDegree + 1.
I'm not too sure if questions of this sort is allowed on stackoverflow but here it goes. I finished up this assignment to calculate reverse polish expressions with a small set of operators. All the tests passed and I have already submitted the assignment. I was wondering if you guys had any suggestions where I could improve on the code that could be applied to my future assignments/projects.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class TheStack
{
public:
void push(double d1);
double pop();
int getNodeTracker();
private:
struct Node
{
double data;
Node* next;
};
Node* bottom = NULL;
Node* top = NULL;
Node* newP = NULL;
int nodeTracker = 0;
};
void TheStack::push(double d1)
{
newP = new Node;
newP->data = d1;
newP->next = NULL;
if (bottom == NULL)
{
bottom = top = newP;
top->next = NULL;
}
else
{
newP->next = top;
top = newP;
}
nodeTracker += 1;
}
double TheStack::pop()
{
Node* tempP;
double tempD;
if (top == NULL)
{
cout << "The stack is empty." << endl;
}
else
{
tempP = top;
tempD = tempP->data;
top = top->next;
delete(tempP);
nodeTracker -= 1;
return(tempD);
}
}
int TheStack::getNodeTracker()
{
return nodeTracker;
}
bool isOperand(string s1);
bool isOperator(string s1);
double operate(string s1, double d1, double d2);
int processor(string str);
bool validString(string str);
int main()
{
// getting user input
string str;
getline(cin, str);
// input loop
while (str != "0")
{
processor(str);
cout << endl;
getline(cin, str);
}
// end program
return 0;
}
bool isOperand(string s1)
{
try
{
stod(s1);
}
catch (...)
{
return false;
}
return true;
}
bool isOperator(string s1)
{
string validOps[4] = { "+", "-", "*", "/" };
for (int i = 0; i < 4; i++)
{
if (s1 == validOps[i])
{
return true;
}
}
return false;
}
double operate(string s1, double d1, double d2)
{
char op = s1[0];
switch (op)
{
case '+':
return d1 + d2;
case '-':
return d1 - d2;
case '*':
return d1 * d2;
case '/':
return d1 / d2;
}
}
bool validString(string str)
{
for (int i = str.size()-1; i >= 0; i--)
{
if (str[i] == '=')
{
return true;
}
}
return false;
}
int processor(string str)
{
TheStack stacklist;
istringstream iss(str);
string streamVar;
iss >> streamVar;
// checks if expression has = sign
if (!validString(str))
{
cout << "Error: Please input valid expression." << endl;
return -1;
}
// builds and operates upon stack
while (streamVar != "=")
{
if (isOperand(streamVar))
{
stacklist.push(stod(streamVar));
}
else if (isOperator(streamVar))
{
if (stacklist.getNodeTracker() > 1)
{
double d2 = stacklist.pop();
double d1 = stacklist.pop();
if (streamVar == "/" && d2 == 0)
{
cout << "Error: Division by 0." << endl;
return -1;
}
double result = operate(streamVar, d1, d2);
stacklist.push(result);
}
else
{
cout << "Error: Too many operators." << endl;
return -1;
}
}
iss >> streamVar;
}
// operand error case
if (stacklist.getNodeTracker() > 1)
{
cout << "Error: Too many operands." << endl;
return -1;
}
// output
cout << stacklist.pop() << endl;
return 0;
}
I am writing a priority queue with a max heap structure as an assignment for school. I can either write it as an array or I can use a vector. I chose a vector. So the assignment is this, the user chooses options from a menu where he either wants to add,print, or view the elements. When the user chooses to add he gets ask who wants to be added, the instructor, student, or TA. He can enter i,I,t,T,S,s. The instructor having the highest priority where if the user chooses the option to print and there is an instructor in the queue he gets to go first. The TA having the second highest priority where if there is a TA and a student in the queue, the TA goes first. If there is is more than one instructor than the queue acts as a normal queue. I have written most of it, or tried. I got my max heap implementation from my textbook since they provide one. Now the problem is this, when I have more than one item in the priority queue and I choose to print, it crashes and gives me a vector subscript out of range exception. I been trying to fix it and no luck. Also, when I try to print the elements in the queue or print them, it needs to say the job# with the name of the person. Can someone help me find a way to implement that.
#pragma once
#include <vector>
struct Heap
{
std::vector<int> m_elements;
void ReHeapDown(int, int);
void ReHeapUp(int, int);
void Swap(int& a, int& b);
};
#include "heap.h"
void Heap::ReHeapDown(int index, int bottom)
{
int maxChild, rightChild, leftChild;
leftChild = index * 2 + 1;
rightChild = index * 2 + 2;
if (leftChild <= bottom)
{
if (leftChild == bottom)
maxChild = leftChild;
else
{
if (m_elements[leftChild] <= m_elements[rightChild])
maxChild = rightChild;
else
maxChild = leftChild;
}
if (m_elements[index] < m_elements[maxChild])
{
Swap(m_elements[index], m_elements[maxChild]);
ReHeapDown(maxChild, bottom);
}
}
}
void Heap::ReHeapUp(int index, int bottom)
{
int parent;
if (bottom > index)
{
parent = (bottom - 1) / 2;
if (m_elements[parent] < m_elements[bottom])
{
Swap(m_elements[parent], m_elements[bottom]);
ReHeapUp(index, parent);
}
}
}
void Heap::Swap(int& a, int& b)
{
int temp;
temp = a;
a = b;
b = temp;
}
#include <iostream>
#include "heap.h"
#pragma once
class PQTYPE
{
private:
Heap m_Items;
public:
bool isEmpty() const;
void Enqueue(int, std::string);
void Dequeue(int, std::string);
void printElements();
};
#include "pqtype.h"
bool PQTYPE::isEmpty() const
{
return m_Items.m_elements.empty();
}
void PQTYPE::Enqueue(int newItem, std::string lName)
{
if (lName == "Student")
{
m_Items.m_elements.push_back(newItem);
m_Items.ReHeapUp(0, m_Items.m_elements.size() - 1);
}
else if (lName == "TA")
{
m_Items.m_elements.push_back(newItem);
m_Items.ReHeapUp(0, m_Items.m_elements.size() - 1);
}
else if (lName == "Instructor")
{
m_Items.m_elements.push_back(newItem);
m_Items.ReHeapUp(0, m_Items.m_elements.size() - 1);
}
}
void PQTYPE::Dequeue(int item, std::string lName)
{
if (isEmpty())
std::cout << "No jobs to print\n";
else
{
m_Items.m_elements[0] = m_Items.m_elements.back();
std::cout << "Now printing Job#" << m_Items.m_elements[item - 1] << " " << lName.c_str() << std::endl;
m_Items.m_elements.pop_back();
m_Items.ReHeapDown(0, item - 1);
}
}
void PQTYPE::printElements()
{
if (isEmpty())
std::cout << "No jobs to print\n";
else
{
for (int i = 0; i < m_Items.m_elements.size(); i++)
{
std::cout << "Job#" << m_Items.m_elements[i] << std::endl;
}
}
}
#include"pqtype.h"
struct Person
{
int m_priority;
std::string m_name;
Person()
{
m_priority = 0;
m_name = " ";
}
};
int showMenu();
void addJobs(PQTYPE&, Person&);
void printJobs(PQTYPE&, Person&);
void viewJobs(PQTYPE&);
int main()
{
int option;
Person p;
PQTYPE pq;
do
{
option = showMenu();
switch (option)
{
case 1: addJobs(pq, p);
break;
case 2: printJobs(pq, p);
break;
case 3: viewJobs(pq);
break;
case 4:
break;
default: std::cout << "Wrong input\n";
break;
}
} while (option != 4);
return 0;
}
int showMenu()
{
int choice;
std::cout << " 1.)Add Job\n";
std::cout << " 2.)Print Job\n";
std::cout << " 3.)View Jobs\n";
std::cout << " 4.)Exit\n";
std::cout << " Enter Choice: ";
std::cin >> choice;
return choice;
}
void addJobs(PQTYPE& pq, Person& per)
{
char jobChoice;
std::cout << "Who is the job for ( Instructor(i or I), TA(t or T), Student(s or S) :";
std::cin >> jobChoice;
if (jobChoice == 'S' || jobChoice == 's')
{
per.m_priority++;
per.m_name = "Student";
pq.Enqueue(per.m_priority, per.m_name);
}
else if (jobChoice == 'T' || jobChoice == 't')
{
per.m_priority++;
per.m_name = "TA";
pq.Enqueue(per.m_priority, per.m_name);
}
if (jobChoice == 'I' || jobChoice == 'i')
{
per.m_priority++;
per.m_name = "Instructor";
pq.Enqueue(per.m_priority, per.m_name);
}
}
void printJobs(PQTYPE& pq, Person& p)
{
pq.Dequeue(p.m_priority, p.m_name);
}
void viewJobs(PQTYPE& pq)
{
pq.printElements();
}
In your original code the index used inside Dequeue() for accessing the vector doesn't seem to be initialised in the right way. Let's assume that you have added two entries to your list. In this case the value of P.m_priority inside your main() is 2. Now you're calling printJobs() for the first time. printJobs() calls pq.Dequeue(p.m_priority, p.m_name), so Dequeue() gets p.m_priority as its parameter item. Keep in mind that item has the value 2.
m_Items.m_elements[0] = m_Items.m_elements.back();
std::cout << "Now printing Job#" << m_Items.m_elements[item - 1] << " " << lName.c_str() << std::endl;
m_Items.m_elements.pop_back();
You're accessing your std::vector using an index of item - 1. This works for the first time, as there are two elements in your list. In this call, there is also a pop_back() done on your list, which decreases its size by one. The next time you call printJobs(), the given parameter item won't have changed, it still has the value 2. When you access your Itemlist, there is no longer an index of 1, and an subscript out of range exception will be thrown.
There were no fixed priorities assigned to the three entry types in your original version, so I added these (see addJobs() ).
So a possible solution to store the person's name could look like this:
struct Person
{
int m_priority;
std::string m_name;
Person()
{
m_priority = 0;
m_name = " ";
}
};
struct Heap
{
std::vector<Person> m_elements;
void ReHeapDown(int, int);
void ReHeapUp(int, int);
void Swap(Person& a, Person& b);
};
void Heap::ReHeapDown(int index, int bottom)
{
int maxChild, rightChild, leftChild;
leftChild = index * 2 + 1;
rightChild = index * 2 + 2;
if (leftChild <= bottom)
{
if (leftChild == bottom)
maxChild = leftChild;
else
{
if (m_elements[leftChild].m_priority <= m_elements[rightChild].m_priority)
maxChild = rightChild;
else
maxChild = leftChild;
}
if (m_elements[index].m_priority < m_elements[maxChild].m_priority)
{
Swap(m_elements[index], m_elements[maxChild]);
ReHeapDown(maxChild, bottom);
}
}
}
void Heap::ReHeapUp(int index, int bottom)
{
int parent;
if (bottom > index)
{
parent = (bottom - 1) / 2;
if (m_elements[parent].m_priority < m_elements[bottom].m_priority)
{
Swap(m_elements[parent], m_elements[bottom]);
ReHeapUp(index, parent);
}
}
}
void Heap::Swap(Person& a, Person& b)
{
Person temp;
temp = a;
a = b;
b = temp;
}
#include <iostream>
class PQTYPE
{
private:
Heap m_Items;
public:
bool isEmpty() const;
void Enqueue(Person);
void Dequeue();
void printElements();
};
bool PQTYPE::isEmpty() const
{
return m_Items.m_elements.empty();
}
void PQTYPE::Enqueue(Person newItem)
{
if (!newItem.m_name.compare("Student"))
{
m_Items.m_elements.push_back(newItem);
m_Items.ReHeapUp(0, m_Items.m_elements.size() - 1);
}
else if (!newItem.m_name.compare("TA"))
{
m_Items.m_elements.push_back(newItem);
m_Items.ReHeapUp(0, m_Items.m_elements.size() - 1);
}
else if (!newItem.m_name.compare("Instructor"))
{
m_Items.m_elements.push_back(newItem);
m_Items.ReHeapUp(0, m_Items.m_elements.size() - 1);
}
}
void PQTYPE::Dequeue()
{
if (isEmpty())
std::cout << "No jobs to print\n";
else
{
Person front = m_Items.m_elements.front();
std::cout << "Now printing Job#" << front.m_priority << " " << front.m_name.c_str() << std::endl;
m_Items.m_elements.erase(m_Items.m_elements.begin());
m_Items.ReHeapDown(0, m_Items.m_elements.size() - 1);
}
}
void PQTYPE::printElements()
{
if (isEmpty())
std::cout << "No jobs to print\n";
else
{
for (int i = 0; i < m_Items.m_elements.size(); i++)
{
std::cout << "Job#" << m_Items.m_elements[i].m_priority << " " << m_Items.m_elements[i].m_name.c_str() << std::endl;
}
}
}
int showMenu();
void addJobs(PQTYPE&, Person&);
void printJobs(PQTYPE&, Person&);
void viewJobs(PQTYPE&);
int showMenu()
{
int choice;
std::cout << " 1.)Add Job\n";
std::cout << " 2.)Print Job\n";
std::cout << " 3.)View Jobs\n";
std::cout << " 4.)Exit\n";
std::cout << " Enter Choice: ";
std::cin >> choice;
return choice;
}
void addJobs(PQTYPE& pq, Person& per)
{
char jobChoice;
std::cout << "Who is the job for ( Instructor(i or I), TA(t or T), Student(s or S) :";
std::cin >> jobChoice;
if (jobChoice == 'S' || jobChoice == 's')
{
per.m_priority = 0;
per.m_name = "Student";
pq.Enqueue(per);
}
else if (jobChoice == 'T' || jobChoice == 't')
{
per.m_priority = 1;
per.m_name = "TA";
pq.Enqueue(per);
}
if (jobChoice == 'I' || jobChoice == 'i')
{
per.m_priority = 2;
per.m_name = "Instructor";
pq.Enqueue(per);
}
}
void printJobs(PQTYPE& pq)
{
pq.Dequeue();
}
void viewJobs(PQTYPE& pq)
{
pq.printElements();
}
int main()
int option;
Person p;
PQTYPE pq;
do
{
option = showMenu();
switch (option)
{
case 1: addJobs(pq, p);
break;
case 2: printJobs(pq);
break;
case 3: viewJobs(pq);
break;
case 4:
break;
default: std::cout << "Wrong input\n";
break;
}
} while (option != 4);
return 0
}
Are you sure that the methods ReHeapUp and ReHeapDown meet your requirements? And shouldn't there be a distinction between job number and priority?