bus error caused by print statement - c++

Ok so I have simple function that returns the highest non-pair card from a 5 card poker hand. But he problem I'm having is wierd. There is this random bus error that occurs randomly, and I don't know for what reason. I thought it was the print statements I was using but now I'm not sure. I know it looks a mess, but if you look at the line below where I print the words "test print", can someone tell me why immediately after this line theres a bus error and it doesn't get to the second "test print" statement. Could I have run out of memory for my program??? That probably makes no sense, but I can't really think of anything else (please exclude the slightly confusing code):
int Hand::highestNonPair(int *face_array_exclude, int size)
{
int highest = 0;
int contains_excludable = 0;
int i = 0;
if(this->hasAnother(i) == false)
highest = cards[i]->getFace();
for(i= 0;i<cards.size();i++)
{
if((cards[i+1]->getFace() > cards[i]->getFace()) &&
(this->hasAnother(i) == false)){
if(size>0){
for(int c = 0;c<size;c++){
if(cards[i]->getFace() == face_array_exclude[c])
contains_excludable = 1;
}
}
if(!contains_excludable)
highest = cards[i+1]->getFace();
cout<<\nTEST PRINT"<<endl;
contains_excludable = 0;
cout<<\nTEST PRINT"<<endl;
}
}
return highest;
}

The bit can case problems:
if((cards[i+1]->getFace() > cards[i]->getFace()) &&
As the highest value of i could be cards.size() - 1. This would mean that cards[i+1 will be an invalid entry.
Change the loop:
for(i= 0;i<cards.size();i++)
to
for(i= 0;i<cards.size() - 1;i++)
Perhaps

Related

Fill index common matrix fields [STRANGE BUGS]

It's my first time posting here, but I always look for some ideas between your posts.
I'm trying to fill the upper half of an array accordingly to the other filled cells (all with 0s and 1s), something like this:
if (matrix[3,2] == 1) and (matrix[3,1] == 1){
matrix[2,1] = 1;}
After much trying, I've made something that barely works with:
matrix[a][b] = 1;
for (unsigned int i = 0; i < max_index; i++){
if (matrix[i][a] == 1){
matrix[i][b] = 1;
}
}
But all I can do is wait for a matrix[corner_i.v1][corner_i.v2] that fills some gaps that shouldn't exists... like this [0, 2]:
01011
00111
00011
00001
00000
Can you guys give me some advices or show some way that I can make this happen? Thank you all.
[edit 2]:
I figured out this was left, but the code still not working at 100%:
for (unsigned int i = 0; i < max_index; i++){
if (matriz[i][a] == 1){
matriz[i][b] = 1;
}
else if (matriz[b][i] == 1){
matriz[a][i] = 1;
}
}
Output be like
[EDIT 3]:
I've solved part of the problem in the function void conectar_matriz(), but some of the inputs seems to bug the program, specially matrices over 40x40.
https://pastebin.com/L3GsciK0

Why is my output freezing when it gets to this section of the code?

I'm trying to compare two decks of cards, yet every time I try another method of doing it, I get the same result... Everything before the code outputs, and it just freezes as soon as it hits the comparison code, as if it's stuck in an infinite loop.
I've tried for loops, static variables, do-while loops, etc. This is my first time leaving the loop at the client code.
The code that supposedly throws the program into an infinite loop.
while (repeatLoop == false)
{
deck1.shuffleDeck();
counter++;
repeatLoop = deck1.compareDecks();
}
compareDecks function.
bool deck::compareDecks()
{
int deckCount = 0;
suitType tempOriginalSuit;
suitType tempShuffleSuit;
rankType tempOriginalRank;
rankType tempShuffleRank;
while (index < 52)
{
tempOriginalSuit = originalCardDeck[index].getSuit();
tempShuffleSuit = shuffledCardDeck[index].getSuit();
if (int(tempOriginalSuit) == int(tempShuffleSuit))
{
tempOriginalRank = originalCardDeck[index].getRank();
tempShuffleRank = shuffledCardDeck[index].getRank();
if (int(tempOriginalRank) == int(tempShuffleRank))
{
deckCount++;
if (deckCount == 52)
return true;
}
}
else
{
return false;
index++;
}
}
}
The shuffleDeck function
(This function pushes back the first card from the first half of the deck and the first card from the second half of the deck towards the end until all 52 cards have been pushed in this pattern. This makes the deck have 52 x 2 cards (with the second half of the deck being the perfect shuffle), so I delete the first half of the cards using .erase as it is not needed)
void deck::shuffleDeck()
{
for (int a = 0, b = 2; a < 2 && b < 4; a++, b++)
{
for (int i = 2; i < 15; i++)
{
shuffledCardDeck.push_back(card{ static_cast<cardSpace::suitType>(a),
static_cast<cardSpace::rankType>(i) });
shuffledCardDeck.push_back(card{ static_cast<cardSpace::suitType>(b),
static_cast<cardSpace::rankType>(i) });
}
}
shuffledCardDeck.erase(shuffledCardDeck.begin(),
shuffledCardDeck.begin() + (shuffledCardDeck.size() / 2));
}
The two decks initialized by this constructor.
deck::deck()
{
for (int i = 0; i < 4; i++)
{
for (int j = 2; j < 15; j++)
{
originalCardDeck.push_back(card{ static_cast<cardSpace::suitType>(i),
static_cast<cardSpace::rankType>(j) });
shuffledCardDeck.push_back(card{ static_cast<cardSpace::suitType>(i),
static_cast<cardSpace::rankType>(j) });
}
}
}
Also note that I've done a perfect shuffle on the shuffledCardDeck vector in another function. I'm trying to repeat the perfectShuffle function until it reaches it's original state and output how many times it took to do this.
I get an infinite loop.
EDIT: I've decided to add the return false; statement in the compareDecks function into the if-else. Also, I think what's causing the problem is that my index i is reset to zero everytime it is called again. Are there any solutions you guys could propose to this? I've tried using static variables, but they just would not increment in the for loop.
EDIT 2: I enclosed my if statements within the curly braces, per users' request, as it's a flaw in my code.
EDIT 3: After commenting out
deck1.shuffleDeck()
The compareDecks function returned true, stating that the decks are equal, which isn't supposed to happen... This caused the loop to end after only one loop.
I was expecting you to actually shuffle the deck.
Your code was pushing a specific, newly synthesized card onto the end of the deck:
shuffledCardDeck.push_back(card{ static_cast<cardSpace::suitType>(a),
static_cast<cardSpace::rankType>(i) });
For example, the first card it will push is always the 2 of 0's (Whatever the 0th suit is). That's not what you want. You actually want to push a copy of the card that is at a specific position index in the deck. For example, loop index from 0 to 25 and then push shuffledCardDeck[index] and shuffledCardDeck[26 + index].
Then you can still wrap up by using your technique of erasing the first half of the deck.
void deck::shuffleDeck()
{
for (int index = 0; index < 26; ++index) {
shuffledCardDeck.push_back(shuffledCardDeck[index]);
shuffledCardDeck.push_back(shuffledCardDeck[26 + index]);
}
shuffledCardDeck.erase(shuffledCardDeck.begin(),
shuffledCardDeck.begin() + 52);
}
You are not modifying the value in the loop, you're using a double equals sign:
repeatLoop == deck1.compareDecks();
That would explain your observed behavior.

g++ error: stray '\177' in program

I was trying to code for following program
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.
Example 1:
Given intervals [1,3],[6,9] insert and merge [2,5] would result in [1,5],[6,9].
Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] would result in [1,2],[3,10],[12,16].
This is the relevant part of my program
here. I want to erase the few positions from the vector
then I am getting the following error
error: stray '\177' in program
intervals.erase(intervals.begin()+(p+1),intervals.begin()+(q+1));
vector<Interval> Solution::insert(vector<Interval> &intervals, Interval newInterval) {
int n = intervals.size();
int p=-1,q=-1,a,b;
for(int i=0;i<n;++i){
if(intervals[i].start <= newInterval.start <= intervals[i+1].end)
p = i;
else if(intervals[i].end < newInterval.start < intervals[i+1].start)
a = i;
if(intervals[i].start <= newInterval.end <= intervals[i+1].end)
q = i;
else if(intervals[i].end < newInterval.end < intervals[i+1].start)
b = i;
}
int x,z;
if(p != -1 && q != -1)
x = q-p;
if(x > 0){
z=intervals[q].end;
intervals.erase(intervals.begin()+(p+1),intervals.begin()+(q+1));
intervals[p].end = z;
}
return vector
}
Did you copy that code from a website?
I managed to reproduce your result with this snippet:
const char* msg = "You can't copy this";
When copied and put on coliru here you'll get the same error code.
What I used for the above snippet in HTML code was:
<code>const char* msg = </code><code>"You can't copy this";
</code>
Note the  character I put in there.
To fix that, you can use a decent editor like Notepad++ that will make the stray characters visible:

Need to find a logic error in a card shuffling method

I'm trying to write a method that takes an array of integers (0-51, in that order), cuts it into two separate arrays (A and B in the below function by using the cut method, which I know for sure works) and then re-fuses the two arrays together by randomly selecting 0, 1 or 2 cards from the BOTTOM of either A or B and then adding them to the deck.
(ps- by "array" I mean linked list, I just said array because I thought it would be conceptually easier)
This is my code so far, it works, but there's a definite bias when it comes to where the cards land. Can anybody spot my logic error?
[code]
void Deck::shuffle(){
IntList *A = new IntList();
IntList *B = new IntList();
cut(A, B);
IntListNode *aMarker = new IntListNode;
aMarker = A->getSentinel()->next;
//cout<< A->getSentinel()->prev->prev->data <<'\n'<<'\n';
IntListNode *bMarker = new IntListNode;
bMarker = B->getSentinel()->next;
//cout<< B->getSentinel()->prev->data;
deckList.clear();
srand(time(NULL));
int randNum = 0, numCards = 0, totalNumCards = 0;
bool selector = true, aisDone = false, bisDone = false;
while(totalNumCards < 52){
randNum = rand() % 3;
if(randNum == 0){
selector = !selector;
continue;
}
numCards = randNum;
if(!aisDone && !bisDone){
if(selector){
for(int i = 0; i < numCards; i++){
deckList.push_back(aMarker->data);
aMarker = (aMarker->next);
if(aMarker == A->getSentinel()){
aisDone = true;
break;
}
}
selector = false;
}else{
for(int i = 0; i < numCards; i++){
deckList.push_back(bMarker->data);
bMarker = (bMarker->next);
if(bMarker == B->getSentinel()){
bisDone = true;
break;
}
}
selector = true;
}
}
if(aisDone && !bisDone){
for(int i = 0; i < (52 - totalNumCards); i++){
deckList.push_back(bMarker->data);
bMarker = (bMarker->next);
if(bMarker == B->getSentinel()){
bisDone = true;
break;
}
}
//return;
}
if(bisDone && !aisDone){
for(int i = 0; i < (52 - totalNumCards); i++){
deckList.push_back(aMarker->data);
aMarker = (aMarker->next);
if(aMarker == A->getSentinel()){
aisDone = true;
break;
}
}
//return;
}
totalNumCards += numCards;
}
int tempSum = 0;
IntListNode *tempNode = deckList.head();
for(int j = 0; j < 52; j++){
//cout<< (tempNode->data) << '\n';
tempSum += (tempNode->data);
tempNode = (tempNode ->next);
}
if(tempSum != 1326)
system("PAUSE");
return;
}
[/code]
What about just using std::random_shuffle? Yeah, it won't work for linked list, but you can change it to vector :)
If your instructor would have the moral to teach you programming the way it should be done then they'd encourage you to solve the problem like so, with four lines of code:
#include<algorithm>
#include<vector>
// ...
std::vector<int> cards; // fill it in ...
std::random_shuffle(cards.begin(), cards.end());
Using the standard library is the right way of doing things. Writing code on your own when you can solve the problem with the standard library is the wrong way of doing things. Your instructor doesn't teach you right. If they want to get a point across (say, have you practice using pointers) then they should be more attentive in selecting the exercise they give you.
That speech given, here is a solution worse than the above but better than your instructor's:
52 times do the following:
Choose two random none-equal integers in the range [0,52).
Swap the values in the array corresponding to these positions.
For most random number generators, the low bits are the least random ones. So your line
randNum = rand() % 3;
should be modified to get its value more from the high- to middle-order bits from rand.
Your expectations may be off. I notice that you swap the selector if your random value is 0. Coupled with the relative non-randomness of randNum, this may be your problem. Perhaps you need to make things less random to make them appear more random, such as swapping the selector every time through the loop, and always taking 1 or more cards from the selected deck.
Comments:
srand(time(NULL));
This should only be called once during an applications run. This it is usally best to call it in main() as you start.
int randNum = 0, numCards = 0, totalNumCards = 0;
bool selector = true, aisDone = false, bisDone = false;
One identifier per line. Every coding standard written has this rule. It also prevents some subtle errors that can creep in when using pointers. Get used to it.
randNum = rand() % 3;
The bottom bits of rand are the lest random.
rand Num = rand() / (MAX_RAND / 3.0);
Question:
if(!aisDone && !bisDone)
{
This can execute
and set one of the above to isDone
Example:
Exit state aisDone == false bsiDone == false // OK
Exit state aisDone == true bsiDone == false // Will run below
Exit state aisDone == false bsiDone == ture // Will run below
}
if(aisDone && !bisDone)
{
Is this allowed to run if the first block above is run?
}
if(bisDone && !aisDone)
{
Is this allowed to run if the first block above is run?
}
The rest is too complicated and I don't understand.
I can think of simpler techniques to get a good shuffle of a deck of cards:
for(loop = 0 .. 51)
{
rand = rand(51 - loop);
swap(loop, loop+rand);
}
The above simulates picking a card at random from the deck A and putting it on the top of deck B (deck B initially being empty). When the loop completes B is now A (as it was done in place).
Thus each card (from A) has the same probability of being placed at any position in B.

expected primary-expression before 'else'

I have an emergency here. This homework is due tomorrow for my CP 1 class. We have to make a simple dice game. If you get doubles of the same number, then good things happen. Here is the function:
void Doubles(); //prototype for the function Doubles()
//pre: n/a
//post: Plays a simple dice game with the user
void Doubles()
{
//variables declared to store dice values
int DieOne, DieTwo, PlayerSame, ComputerSame;
cout<<"\nLET'S PLAY DOUBLES!!!\n"<<endl;
srand ( time(NULL) ); //initialize random seed
DieOne = rand()%6 + 1;
DieTwo = rand()%6 + 1;
cout<<"\nYour first die is a "<<DieOne;
cout<<"\nYour second die is a "<<DieTwo;
if(DieOne == DieTwo)
{
PlayerSame = 1;
}
else
{
PlayerSame = 0;
}
DieOne = rand()%6 + 1;
DieTwo = rand()%6 + 1;
cout<<"\n\nThe computer's first die is a "<<DieOne;
cout<<"\nThe computer's second die is a "<<DieTwo;
if(DieOne == DieTwo)
{
ComputerSame = 1;
}
else
{
ComputerSame = 0;
}
if(PlayerSame == 1 && ComputerSame == 0)
{
cout<<"\n\nYou win! Your dice are the same and the "
<<"computer's dice aren't!";
}
else if(PlayerSame == 1 && ComputerSame == 1)
{
cout<<"\n\nYou tied! Your dice are the same and the "
<<"computer's dice are the same!";
}
else if(PlayerSame == 0 && ComputerSame == 1)
{
cout<<"\n\nYou lost! Your dice are not the same, and the "
<<"computer's dice are!";
else
{
cout<<"\n\nNeither you nor the computer had dice that matched, "
<<"so you both lose!";
}
}
So why, wen I run this, am I getting the compiler error stated in the title? the primary expression is right there! The "else" it's referring to is the last one there. Any help is greatly appreciated.
You are missing the brace, as has been pointed out, but the flaw comes from not being able to look at the code indentation correctly and not being able to clearly see the error. The indentation on the else if's is indented one more than the if, and if this were not the case, you would quickly see the missing brace, but, as formatted, it is more easily missed, although I did not even read the whole post before looking at the code and spotting the missing brace myself (I have a lot of legacy code which I maintain that has bad indentation that I have been fixing for years, and this is a common type of formatting error that leads to this specific problem.. Proper formatting would lead to the spotting of this type of error much more quickly, especially in programmers that have not been programming for a long time and are not seasoned to fix the formatting in their head as they read through before looking at code specifics.
Jay
you forgot the closing brace for the block after the second else if
Looks like you are missing a brace } after <<"computer's dice are!"; line.
Doesn't your compiler tell you the line number where it sees the error?
PLease add the closing brace before the else.
else if(PlayerSame == 0 && ComputerSame == 1)
{
cout<<"\n\nYou lost! Your dice are not the same, and the "
<<"computer's dice are!";
}
else
{
cout<<"\n\nNeither you nor the computer had dice that matched, "
<<"so you both lose!";
}