Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Sorting Numbers in ascending order in text file
    text
    copied!<p>So I wrote a little slot machine program but I cannot for the life of me figure out how to sort these numbers.</p> <p>I got it to write the random numbers generated out to a file but I need them to be sorted in ascending order. I also need to have binary search to search for a value in the file, but I'm just worried about the sorting for now. Here is what I have and any help is greatly appreciated!!</p> <p>It writes out to the file in the void SlotMachine::spinReel()</p> <pre><code>//////////// main.cpp /////////// // Created by ___Josh Modica___ on 11/10/13. /* */ #include &lt;iostream&gt; #include &lt;time.h&gt; #include &lt;stdlib.h&gt; #include &lt;iomanip&gt; #include &lt;string.h&gt; #include &lt;Windows.h&gt; #include &lt;cstdio&gt; #include &lt;fstream&gt; #include &lt;algorithm&gt; using namespace std; const int WIN_COL=100; const int LIMIT = 1000; const int PAY_A=1000; const int PAY_C=100; const int PAY_D=50; const int PAY_e=30; const int PAY_F=3; const int PAY_S=250; void macro(); class SlotMachine { private: int reels[3][3]; //int elements = sizeof(reels) / sizeof(reels[0][0]); int totCred; int totBet; int numLines; public: SlotMachine(); void getBet(); void spinReel(); void display(); void checkWin(); void payOut(); void showPaytable(); void cleanup(); }; SlotMachine::SlotMachine() { int i, j; for (i = 0; i &lt; 3; i++) { for (j = 0; j &lt; 3; j++) { reels[i][j] = 0; } } totBet = 0; numLines = 0; totCred = 2500; } void macro() { cout &lt;&lt;" "&lt;&lt;right&lt;&lt;setw(80) &lt;&lt; setfill('^') &lt;&lt; "\n"; cout &lt;&lt; setw(88) &lt;&lt;setfill(' ')&lt;&lt;left&lt;&lt;" *_ ____ ___ __"&lt;&lt; "_*"&lt;&lt; endl; cout &lt;&lt; setw(88) &lt;&lt;" *_ /\\ _`\\ /\\_ \\ /\\ \\__"&lt;&lt; "_*"&lt;&lt; endl; cout &lt;&lt; setw(88) &lt;&lt;" *_ \\ \\,\\_\\_\\\\//\\ \\ ___\\ \\ ,_\\"&lt;&lt; "_*"&lt;&lt; endl; cout &lt;&lt; setw(88) &lt;&lt;" *_ \\/_\\__ \\ \\ \\ \\ / __`\\ \\ \\"&lt;&lt; "_*"&lt;&lt;endl; cout &lt;&lt; setw(88) &lt;&lt;" *_ /\\ \\_\\ \\ \\_\\ \\_/\\ \\_\\ \\ \\ \\_"&lt;&lt; "_*"&lt;&lt; endl; cout &lt;&lt; setw(88) &lt;&lt;" *_ \\ `\\____\\/\\____\\ \\____/\\ \\__\\"&lt;&lt; "_*"&lt;&lt; endl; cout &lt;&lt; setw(88) &lt;&lt;" *_ \\/_____/\\/____/\\/___/ \\/__/"&lt;&lt; "_*"&lt;&lt; endl; cout &lt;&lt; setw(88) &lt;&lt;" *_ __"&lt;&lt; "_*"&lt;&lt; endl; cout &lt;&lt; setw(88) &lt;&lt;" *_ /'\\_/`\\ /\\ \\ __"&lt;&lt; "_*"&lt;&lt; endl; cout &lt;&lt; setw(88) &lt;&lt;" *_ /\\ \\ __ ___\\ \\ \\___ /\\_\\ ___ __"&lt;&lt; "_*"&lt;&lt; endl; cout &lt;&lt; setw(88) &lt;&lt;" *_ \\ \\ \\__\\ \\ /'__`\\ /'___\\ \\ _ `\\/\\ \\ /' _ `\\ /'__`\\"&lt;&lt; "_*"&lt;&lt; endl; cout &lt;&lt; setw(88) &lt;&lt;" *_ \\ \\ \\_/\\ \\/\\ \\_\\.\\_/\\ \\__/\\ \\ \\ \\ \\ \\ \\/\\ \\/\\ \\/\\ __/"&lt;&lt; "_*"&lt;&lt; endl; cout &lt;&lt; setw(88) &lt;&lt;" *_ \\ \\_\\\\ \\_\\ \\__/.\\_\\ \\____\\\\ \\_\\ \\_\\ \\_\\ \\_\\ \\_\\ \\____\\"&lt;&lt; "_*"&lt;&lt; endl; cout &lt;&lt; setw(88) &lt;&lt;" *_ \\/_/ \\/_/\\/__/\\/_/\\/____/ \\/_/\\/_/\\/_/\\/_/\\/_/\\/____/"&lt;&lt; "_*"&lt;&lt; endl; cout &lt;&lt; setw(88) &lt;&lt;" * "&lt;&lt; "*"; cout &lt;&lt;right&lt;&lt; " "&lt;&lt;setw(90) &lt;&lt; setfill('`') &lt;&lt; "\n"&lt;&lt;setfill(' '); cout&lt;&lt;" # ____ _ _ ____ _____ ___ _ _ __ __ _____ ____ ____ ___ __ #"&lt;&lt;endl; cout&lt;&lt;" # ( _ \\( \\/ ) (_ _)( _ )/ __)( )_( ) ( \\/ )( _ )( _ \\(_ _)/ __) /__\\ #"&lt;&lt;endl; cout&lt;&lt;" # ) _ &lt; \\ / .-_)( )(_)( \\__ \\ ) _ ( ) ( )(_)( )(_) )_)(_( (__ /(__)\\ #"&lt;&lt;endl; cout&lt;&lt;" # (____/ (__) \\____) (_____)(___/(_) (_) (_/\\/\\_)(_____)(____/(____)\\___)(__)(__) #"; cout&lt;&lt; right&lt;&lt;" "&lt;&lt; setw(90) &lt;&lt; setfill('_') &lt;&lt; "\n"&lt;&lt;setfill(' ')&lt;&lt;right&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl; } void centerTxt(char*txt) { int center; center=strlen(txt); cout&lt;&lt;setw((WIN_COL+center)/2)&lt;&lt;txt&lt;&lt;endl; } void SlotMachine::getBet() { int bet=0; centerTxt("How Many Lines To Play"); centerTxt("----------------------"); cout&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl; if (totCred &lt; 1) { cout &lt;&lt; "Game Over =(" &lt;&lt; endl &lt;&lt; endl; system("pause"); exit(0); } if (totCred &gt;= 1) cout &lt;&lt; setw(WIN_COL/3/2)&lt;&lt;"1 line"; if (totCred &gt;= 2) cout &lt;&lt; setw(WIN_COL/3) &lt;&lt; "2 lines"; if (totCred &gt;= 3) cout &lt;&lt; setw(WIN_COL/3) &lt;&lt; "3 lines"; cout &lt;&lt; endl &lt;&lt; endl; while(numLines&lt;1 || numLines&gt;3){ cout&lt;&lt;"&gt;&gt;"; cin &gt;&gt; setw(1) &gt;&gt; numLines; } system("cls"); display(); centerTxt( "How Many Credits Per Line"); centerTxt( "-------------------------"); cout&lt;&lt; endl&lt;&lt; endl; cout &lt;&lt;" "&lt;&lt; setw(WIN_COL/5)&lt;&lt;left&lt;&lt;"1 credit"; if (totCred &gt;= numLines * 2) cout &lt;&lt; setw(WIN_COL/5) &lt;&lt; "2 credits"; if (totCred &gt;= numLines * 3) cout &lt;&lt; setw(WIN_COL/5) &lt;&lt; "3 credits"; if (totCred &gt;= numLines * 4) cout &lt;&lt; setw(WIN_COL/5) &lt;&lt; "4 credits"; if (totCred &gt;= numLines * 5) cout &lt;&lt; setw(WIN_COL/5) &lt;&lt; "5 credits"; cout &lt;&lt; endl &lt;&lt; endl; while( bet&lt;1 || bet&gt;5){ cout&lt;&lt;"&gt;&gt;"; cin &gt;&gt; setw(1) &gt;&gt; bet; } totBet = numLines * bet; totCred -= totBet; system("cls"); display(); } void SlotMachine::spinReel() { int i, j; srand(time(NULL)); for (i = 0; i &lt; 3; i++) { for (j = 0; j &lt; 3; j++) { reels[i][j] = rand() % LIMIT + 1; ////// write out to file ///// ofstream outputFile("project.txt", ios_base::app); outputFile &lt;&lt; reels[i][j]&lt;&lt;"\n"; outputFile.close(); //else cout &lt;&lt; "Unable to open file"; } } for (i = 0; i &lt; 3; i++) { cout&lt;&lt;setw(30); for (j = 0; j &lt; 3; j++) { if((reels[i][j]==1)||(reels[i][j]==20)) { reels[i][j]=1; cout&lt;&lt;"JACK POT"; } else if((reels[i][j]&gt;20)&amp;&amp;(reels[i][j]&lt;=160)) { reels[i][j]=2; cout&lt;&lt;"$$$$$$$$"; } else if((reels[i][j]&gt;160)&amp;&amp;(reels[i][j]&lt;=360)) { reels[i][j]=3; cout&lt;&lt;"GOLD BAR"; } else if((reels[i][j]&gt;360)&amp;&amp;(reels[i][j]&lt;=660)) { reels[i][j]=4; cout&lt;&lt;"-SILVER-"; } else if((reels[i][j]&gt;660)&amp;&amp;(reels[i][j]&lt;=790)) { reels[i][j]=5; cout&lt;&lt;"-BRONZE-"; } else if((reels[i][j]&gt;790)&amp;&amp;(reels[i][j]&lt;= 900)) { reels[i][j]=6; cout&lt;&lt;"++++++++"; } else if((reels[i][j]&gt;900)&amp;&amp;(reels[i][j]&lt;=950)) { reels[i][j]=7; cout&lt;&lt;"--------"; } else { reels[i][j]=8; cout&lt;&lt;"LOSELOSE"; } cout &lt;&lt; " \t\t"; } cout &lt;&lt; endl&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl; } } void SlotMachine::checkWin() { int line1=0, line2=0, line3=0; int scatter=0; if(reels[1][0]==reels[1][1]==reels[1][2]) { switch(reels[1][0]) { case 1: centerTxt("You Win!!!!"); cout&lt;&lt;setw(40)&lt;&lt;"New Balance: $"&lt;&lt;PAY_A * (totBet/numLines); line1=PAY_A*(totBet/numLines); system("pause"); break; case 2: case 3: centerTxt("You Win!!!!"); cout&lt;&lt;setw(40)&lt;&lt;"New Balance: $"&lt;&lt;PAY_C * (totBet/numLines); line1=PAY_C*(totBet/numLines); system("pause"); break; case 4: centerTxt("You Win!!!!"); cout&lt;&lt;setw(40)&lt;&lt;"New Balance: $"&lt;&lt;PAY_D * (totBet/numLines); line1=PAY_D*(totBet/numLines); system("pause"); break; case 5: centerTxt("You Win!!!!"); cout&lt;&lt;setw(40)&lt;&lt;"New Balance: $"&lt;&lt;PAY_e * (totBet/numLines); line1=PAY_e*(totBet/numLines); system("pause"); break; case 6: centerTxt("You Win!!!!"); cout&lt;&lt;setw(40)&lt;&lt;"New Balance: $"&lt;&lt;PAY_F * (totBet/numLines); line1=PAY_F*(totBet/numLines); system("pause"); break; } } if((numLines&gt;=2)&amp;&amp;(reels[0][0]==reels[1][1])&amp;&amp;(reels[0][0]==reels[2][2])) { switch(reels[0][0]) { case 1: centerTxt("You Win!!!!"); cout&lt;&lt;setw(40)&lt;&lt;"New Balance: $"&lt;&lt;PAY_A * (totBet/numLines); line2=PAY_A*(totBet/numLines); system("pause"); break; case 2: case 3: centerTxt("You Win!!!!"); cout&lt;&lt;setw(40)&lt;&lt;"New Balance: $"&lt;&lt;PAY_C * (totBet/numLines); line2=PAY_C*(totBet/numLines); system("pause"); break; case 4: centerTxt("You Win!!!!"); cout&lt;&lt;setw(40)&lt;&lt;"New Balance: $"&lt;&lt;PAY_D * (totBet/numLines); line2=PAY_D*(totBet/numLines); system("pause"); break; case 5: centerTxt("You Win!!!!"); cout&lt;&lt;setw(40)&lt;&lt;"New Balance: $"&lt;&lt;PAY_e * (totBet/numLines); line2=PAY_e*(totBet/numLines); system("pause"); break; case 6: centerTxt("You Win!!!!"); cout&lt;&lt;setw(40)&lt;&lt;"New Balance: $"&lt;&lt;PAY_F * (totBet/numLines); line2+=PAY_F*(totBet/numLines); system("pause"); break; } } if((numLines==3)&amp;&amp;(reels[2][0]==reels[1][1]==reels[0][2])) { switch(reels[0][2]) { case 1: centerTxt("You Win!!!!"); cout&lt;&lt;setw(40)&lt;&lt;"New Balance: $"&lt;&lt;PAY_A * (totBet/numLines); line3=PAY_A*(totBet/numLines); system("pause"); break; case 2: case 3: centerTxt("You Win!!!!"); cout&lt;&lt;setw(40)&lt;&lt;"New Balance: $"&lt;&lt;PAY_C * (totBet/numLines); line3=PAY_C*(totBet/numLines); system("pause"); break; case 4: centerTxt("You Win!!!!"); cout&lt;&lt;setw(40)&lt;&lt;"New Balance: $"&lt;&lt;PAY_D * (totBet/numLines); line3=PAY_D*(totBet/numLines); system("pause"); break; case 5: centerTxt("You Win!!!!"); cout&lt;&lt;setw(40)&lt;&lt;"New Balance: $"&lt;&lt;PAY_e * (totBet/numLines); line3=PAY_e*(totBet/numLines); system("pause"); break; case 6: centerTxt("You Win!!!!"); cout&lt;&lt;setw(40)&lt;&lt;"New Balance: $"&lt;&lt;PAY_F * (totBet/numLines); line3=PAY_F*(totBet/numLines); system("pause"); break; } system("pause"); } for(int i=0;i&lt;3;i++) { for(int j=0;j&lt;3;j++) { if(reels[i][j]==7) scatter++; } } if(scatter&gt;=3) { centerTxt("You Win!!!"); cout&lt;&lt;setw(40)&lt;&lt;PAY_S * totBet*(scatter-2); totCred+=PAY_S*totBet*(scatter-2); system("pause"); } if(line1+line2+line3==0) { centerTxt("You Lose =("); system("pause"); } totCred+=line1+line2+line3; } void SlotMachine::display() { macro(); cout&lt;&lt;endl; cout&lt;&lt;"Credits: $"&lt;&lt;totCred&lt;&lt;endl; cout&lt;&lt;"--------"&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl; } void SlotMachine::cleanup() { int i,j; system("cls"); numLines=0; totBet=0; /*for (j = 0; j &lt; 3; j++) { for (i = 0; i &lt; 3; i++) { reels[i][j] = 0; cout&lt;&lt;i&lt;&lt;", "&lt;&lt;j&lt;&lt;'\t'; } cout&lt;&lt;endl; }*/ } int main() { system("mode 100,50"); ////// picking the color ////////// HANDLE hConsole; int color =10; hConsole = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hConsole, color); //system("color 2"); SlotMachine slot; while(true) { slot.display(); slot.getBet(); slot.spinReel(); slot.checkWin(); slot.cleanup(); } 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