Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy won't the numbers stay random?
    text
    copied!<p>I have been coding a blackjack game that is almost done it's first stage of development. I am almost done, the only problem is that the random numbers that are generated in this part:</p> <pre><code>if(x.hit == 1) { if (x.userShowCardThree == 0) { x.userShowCardThree = 1 + (rand()%11); int dealerHit1 = x.userShowCardThree; x.userTotal += dealerHit1; x.cardCount++; } else { if (x.userShowCardFour == 0) { x.userShowCardFour = 1 + (rand()%11); int dealerHit2 = x.userShowCardFour; x.userTotal += dealerHit2; x.cardCount++; } else { if (x.userShowCardFive ==0) { x.userShowCardFive = 1 + (rand()%11); int dealerHit3 = x.userShowCardFive; x.userTotal += dealerHit3; x.cardCount++; } } } </code></pre> <p>is not the same as the numbers generated in the final part:</p> <pre><code> cout &lt;&lt; "You had a total of: " &lt;&lt; x.userTotal &lt;&lt; endl; </code></pre> <p>as i keep getting different numbers. I will paste my entire code below.</p> <pre><code>#include &lt;iostream&gt; #include &lt;ctime&gt; #include &lt;cstdlib&gt; #include &lt;cstring&gt; using namespace std; void setRand() { srand(time(0)); } class Game { public: int userCards[11] = {1,2,3,4,5,6,7,8,9,10,11}; int dealerCards[11] = {1,2,3,4,5,6,7,8,9,10,11}; int userShowCardOne = userCards[1 + (rand()%11)]; int userShowCardTwo = userCards[1 + (rand()%11)]; int dealerShowCard = dealerCards[1 + (rand()%11)]; int dealerHiddenCard = dealerCards[1 + (rand()%11)]; int userShowCards[5] = {userShowCardOne, userShowCardTwo, userShowCardThree, userShowCardFour, userShowCardFive}; int userShowCardThree = 0; int userShowCardFour = 0; int userShowCardFive = 0; int fresh = 1; int beginningInput; int hit = 1; int dealerTotal = dealerShowCard + dealerHiddenCard; int userTotal = userShowCardOne + userShowCardTwo; int cardCount = 2; int runGame = 1; private: }; // int a = 1 + rand()%11; /*int b = 1 + rand()%11; int c = 1 + rand()%11; int d = 1 + rand()%11; int e = 1 + rand()%11; int f = 1 + rand()%11; int g = 1 + rand()%11; int h = 1 + rand()%11; */ int startGame(); int main() { srand(time(0)); Game x; cout &lt;&lt; "Welcome to BlackJack 1.0.0" &lt;&lt; endl; cout &lt;&lt; "Press: " &lt;&lt;endl; cout &lt;&lt; "1 ----- New Game" &lt;&lt; endl; cout &lt;&lt; "2 ----- Help" &lt;&lt; endl; cin &gt;&gt; x.beginningInput; if(x.beginningInput == 1){ startGame(); cout &lt;&lt; "The dealer had: " &lt;&lt; endl; cout &lt;&lt; x.dealerHiddenCard &lt;&lt; endl; cout &lt;&lt; x.dealerShowCard &lt;&lt; endl; while(x.dealerTotal &lt;= 16) { cout &lt;&lt; "The dealer has decided to hit" &lt;&lt; endl; int dealerHit = 1 + (rand()%11); cout &lt;&lt; "The dealer has gotten " &lt;&lt; dealerHit &lt;&lt; endl; x.dealerTotal += dealerHit; } cout &lt;&lt; "The dealer has decided to stay" &lt;&lt; endl; cout &lt;&lt; "The dealer had a total of " &lt;&lt; x.dealerTotal &lt;&lt; endl; cout &lt;&lt; "You had a total of: " &lt;&lt; x.userTotal &lt;&lt; endl; if (x.dealerTotal &gt; 21 &amp;&amp; x.userTotal &lt;= 21) { cout &lt;&lt; "The dealer busted. You won!" &lt;&lt; endl; } else if (x.userTotal &gt; 21 &amp;&amp; x.dealerTotal &lt;= 21) { cout &lt;&lt; "You busted. The Dealer Won" &lt;&lt; endl; } else if (x.userTotal &gt; 21 &amp;&amp; x.dealerTotal &gt; 21) { cout &lt;&lt; "You and the dealer both busted. Tie" &lt;&lt; endl; } else { if (x.dealerTotal &gt; x.userTotal) { cout &lt;&lt; "Sorry, you lost to the dealer" &lt;&lt; endl; } else if (x.dealerTotal &lt; x.userTotal) { cout &lt;&lt; "Congrats, you won!" &lt;&lt; endl; } else { cout &lt;&lt; "You and the dealer tied. " &lt;&lt; endl; } } cout &lt;&lt; "Would you like to play again? 1 to play again, 0 to quit" &lt;&lt; endl; cin &gt;&gt; x.beginningInput; if (x.beginningInput == 1){ startGame(); } else if (x.beginningInput == 0){ return 0; } } else if (x.beginningInput == 0) { cout &lt;&lt; "Thanks for playing!" &lt;&lt; endl; } else { cout &lt;&lt; "Here's the help section" &lt;&lt; endl; } } int startGame() { Game x; srand(time(0)); if (x.fresh != 1){ cout &lt;&lt; "NEW GAME\n \n " &lt;&lt; endl; } while (x.runGame == 1) { cout &lt;&lt; "Dealer: \n" &lt;&lt; endl; cout &lt;&lt; "X" &lt;&lt; endl; cout &lt;&lt; x.dealerShowCard &lt;&lt; endl; cout &lt;&lt; "You: \n" &lt;&lt; endl; cout &lt;&lt; x.userShowCardOne &lt;&lt; endl; cout &lt;&lt; x.userShowCardTwo &lt;&lt; endl; x.userTotal = x.userShowCardOne + x.userShowCardTwo; if (x.userShowCardThree != 0) { cout &lt;&lt; x.userShowCardThree &lt;&lt; endl; } if (x.userShowCardFour != 0) { cout &lt;&lt; x.userShowCardFour &lt;&lt; endl; cout &lt;&lt; "You can only hit one more time!" &lt;&lt; endl; } if (x.userShowCardFive != 0) { cout &lt;&lt; x.userShowCardFive &lt;&lt; endl; } if(x.cardCount &gt; 5) { cout &lt;&lt; "sorry, there is a 5 card limit."; } cout &lt;&lt; "Would you like to hit or stay? (1 for hit or 2 for stay)" &lt;&lt; endl; cin &gt;&gt; x.hit; x.fresh = 2; if(x.hit == 1) { if (x.userShowCardThree == 0) { x.userShowCardThree = 1 + (rand()%11); int dealerHit1 = x.userShowCardThree; x.userTotal += dealerHit1; x.cardCount++; } else { if (x.userShowCardFour == 0) { x.userShowCardFour = 1 + (rand()%11); int dealerHit2 = x.userShowCardFour; x.userTotal += dealerHit2; x.cardCount++; } else { if (x.userShowCardFive ==0) { x.userShowCardFive = 1 + (rand()%11); int dealerHit3 = x.userShowCardFive; x.userTotal += dealerHit3; x.cardCount++; } } } } if (x.hit == 2) { x.runGame = 2; } } return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload