I'm having problems with two for loops inside of my program and the if/else statements inside of them. The counter starts at 0, then increments every time the loop is run. I have tested, and the counter increments properly, but the if/else statements act as if the counter starts at 1 and then stays there (I have added comments in my code where it seems to "get stuck".) Can anyone help me? It's probably something super-obvious but I just don't see it. Thank you.
using namespace std;
// Define global constants
// The integer maximum number of cards that can be in a player’s hand = 10
const int maxCards = 10;
// The integer maximum number of players = 5
const int maxPlayers = 5;
// Define 2 global data structures:
struct card {
//The suit as a character: H-hearts, S-spades, C-clubs, D-diamonds
char cardSuit;
// The face as a character: A-ace, K-king, Q-queen, J-jack, T-ten, 9-2
char cardFace;
// The value as an integer: A=13, K=12, Q=11, J=10, 9-2
int cardValue;
// Status of the card in the deck: true=in the deck, false=in the stack or a player’s hand
bool cardStatus;
};
struct player {
// An array of Cards up to the max number of cards in a player’s hand above
card playerCards[maxCards] = {};
};
int main(void) {
// Initialize stack
// Declare an array of 52 Cards to represent a new stack of cards
card initialCards[52];
// For every card
int initialCardCounter = 0;
// For each suit
for (int initialSuitCounter = 0; initialSuitCounter < 4; initialSuitCounter ) {
int initialSuitLoopCounter = initialSuitCounter;
// Assign a Suit
if (initialSuitLoopCounter = 0) {
initialCards[initialCardCounter].cardSuit = 'H';
}
// !!! It skips to this point, then doesn't increment. !!!
else if (initialSuitLoopCounter = 1) {
initialCards[initialCardCounter].cardSuit = 'S';
}
else if (initialSuitLoopCounter = 2) {
initialCards[initialCardCounter].cardSuit = 'C';
}
else if (initialSuitLoopCounter = 3) {
initialCards[initialCardCounter].cardSuit = 'D';
}
else if (initialSuitLoopCounter > 3) {
cout << "Error";
}
cout << initialCards[initialCardCounter].cardSuit;
// For each face
for (int initialFaceCounter = 0; initialFaceCounter < 14; initialFaceCounter ) {
int initialFaceLoopCounter = initialFaceCounter;
if (initialFaceLoopCounter = 0) {
initialCards[initialCardCounter].cardFace = 'A';
initialCards[initialCardCounter].cardValue = 13;
}
// !!! Same issue here, skips to here even though InitialFaceLoopCounter = 0 and just stays here filling the entire array with 'K' !!!
else if (initialFaceLoopCounter = 1) {
initialCards[initialCardCounter].cardFace = 'K';
initialCards[initialCardCounter].cardValue = 12;
}
else if (initialFaceLoopCounter = 2) {
initialCards[initialCardCounter].cardFace = 'Q';
initialCards[initialCardCounter].cardValue = 11;
}
else if (initialFaceLoopCounter = 3) {
initialCards[initialCardCounter].cardFace = 'J';
initialCards[initialCardCounter].cardValue = 10;
}
else if (initialFaceLoopCounter = 4) {
initialCards[initialCardCounter].cardFace = 'T';
initialCards[initialCardCounter].cardValue = 9;
}
else if (initialFaceLoopCounter = 5) {
initialCards[initialCardCounter].cardFace = '9';
initialCards[initialCardCounter].cardValue = 8;
}
else if (initialFaceLoopCounter = 6) {
initialCards[initialCardCounter].cardFace = '8';
initialCards[initialCardCounter].cardValue = 7;
}
else if (initialFaceLoopCounter = 7) {
initialCards[initialCardCounter].cardFace = '7';
initialCards[initialCardCounter].cardValue = 6;
}
else if (initialFaceLoopCounter = 8) {
initialCards[initialCardCounter].cardFace = '6';
initialCards[initialCardCounter].cardValue = 5;
}
else if (initialFaceLoopCounter = 9) {
initialCards[initialCardCounter].cardFace = '5';
initialCards[initialCardCounter].cardValue = 4;
}
else if (initialFaceLoopCounter = 10) {
initialCards[initialCardCounter].cardFace = '4';
initialCards[initialCardCounter].cardValue = 3;
}
else if (initialFaceLoopCounter = 11) {
initialCards[initialCardCounter].cardFace = '3';
initialCards[initialCardCounter].cardValue = 2;
}
else if (initialFaceLoopCounter = 12) {
initialCards[initialCardCounter].cardFace = '2';
initialCards[initialCardCounter].cardValue = 1;
}
else if (initialFaceLoopCounter > 12) {
cout << "Error";
}
cout << initialCards[initialCardCounter].cardFace;
// Increment the card counter
if (initialCardCounter < 51) {
initialCardCounter ;
}
} // end for each face
} // end for each suit
} // end main
CodePudding user response:
if (initialFaceLoopCounter = 0)
These are assignments, not comparisons. You need to adjust your if
statements to double ==
to compare the values.