
Learn how to install the IDE (Integrated Development Environment) Code::Blocks on Microsoft Windows.
Source code available for download as compressed (.zip) cpp file also.
// My first C++ program!
#include <iostream>
int main()
{
std::cout << "Hello, World!";
return 0;
}
Source code available for download as compressed (.zip) cpp file also.
// hname - My second C++ program!
// By Cody Ray Miller
// Version 2.0
#include <iostream>
const std::string greeting = "Hello, ";
std::string name;
char punct = '!';
int currentYear;
int birthYear;
int age;
bool hasHadBday;
char birthdayYet;
int main()
{
std::cout << "~@~@~@~@~@~@~@~@~@~@~@~@~@~@~" << std::endl;
std::cout << "~@~@~ Welcome to hname! ~@~@~" << std::endl;
std::cout << "~@~@~@~ Version 2.0 ~@~@~@~@~" << std::endl;
std::cout << "~@~@~@~@~@~@~@~@~@~@~@~@~@~@~\n\n";
std::cout << "Please type your name and hit ENTER: ";
std::cin >> name;
std::cout << greeting+name+punct << std::endl;
std::cout << "What is the current year?\n";
std::cin >> currentYear;
std::cout << "In what year were you born?\n";
std::cin >> birthYear;
std::cout << "Have you had your birthday this year? (Y/N)\n";
std::cin >> birthdayYet;
if (birthdayYet == 'N') {
age = currentYear-birthYear-1;
} else { age = currentYear-birthYear; }
std::cout << "You are ";std::cout << age;std::cout << " years old!";
return 0;
}
Source code available for download as compressed (.zip) cpp file also.
// "SUBTRACT!" by Cody Ray Miller
// Version 1.0
#include <iostream>
#include <iomanip>
double x; // The first user-entered number.
double y; // The second user-entered number.
double z; // The final product!
bool again = 1; // True by default.
char againChar;
int main()
{
std::cout << "Welcome to SUBTRACT!\n";
while (again)
{
std::cout << "\nPlease enter a number: ";
std::cin >> x;
std::cout << "Please enter another number: ";
std::cin >> y;
z = x - y;
std::cout << std::setprecision(16) << x << " - " << y << " = " << z << "\n";
std::cout << "Calculate again? (Y/N)\n";
std::cin >> againChar;
if (againChar == 'N')
{
again = 0;
}
}
return 0;
}
Source code available for download as compressed (.zip) cpp file also.
// "MADS" by Cody Ray Miller
// Version 1.0
#include <iostream>
#include <iomanip>
// Main Variables
double x; // The first user-entered number.
double y; // The second user-entered number.
double z; // The final product!
char operationSelect; // Character used for operation selection by user.
bool again = 1; // Set to TRUE by default.
char againChar; // Char used to determine boolean 'again' value
// Multiplication Function
double multiply(double x, double y)
{
z = x * y;
std::cout << std::setprecision(16) << x << " * " << y << " = " << z << "\n";
}
// Addition Function
double add(double x, double y)
{
z = x + y;
std::cout << std::setprecision(16) << x << " + " << y << " = " << z << "\n";
}
// Division Function
double divide(double x, double y)
{
z = x / y;
std::cout << std::setprecision(16) << x << " / " << y << " = " << z << "\n";
}
// Subtraction Function
double subtract(double x, double y)
{
z = x - y;
std::cout << std::setprecision(16) << x << " - " << y << " = " << z << "\n";
}
int main()
{
std::cout << "@@@@@@@@@@@@@@@@@@@@@\n";
std::cout << "Welcome to MADS v.1.0\n";
std::cout << "@@@@@@@@@@@@@@@@@@@@@\n";
while (again) // Begin Loop
{
std::cout << "\nPlease enter a number: ";
std::cin >> x;
std::cout << "Please enter another number: ";
std::cin >> y;
std::cout << "Please select an operation:\nM = Multiply | A = Add | D = Divide | S = Subtract\n";
std::cin >> operationSelect;
switch (operationSelect)
{
case 'M' : case 'm' :
multiply(x, y);
break;
case 'A' : case 'a' :
add(x, y);
break;
case 'D' : case 'd' :
divide(x, y);
break;
case 'S' : case 's' :
subtract(x, y);
break;
default :
std::cout << "ERROR: Selection not recognized.\n\n";
}
againChar = 'Q'; // Assign a nonsense value
while (againChar == 'Q') { // Begin againChar Loop
std::cout << "Perform another calculation? (Y/N)\n";
std::cin >> againChar;
if (againChar == 'N' or againChar == 'n')
{
again = 0;
}
else if (againChar == 'Y' or againChar == 'y')
{
again = 1;
}
else
{
againChar = 'Q';
std::cout << "User Input Error: Please enter Y or N.\n";
}
} // End againChar Loop
} // End Loop
return 0;
}
#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <ctime>
// "Random Password Generator" by Cody Ray Miller
std::string versionNumber = "1.1.0";
// Password variables to be assigned random values
char special[] = { '!', '@', '#', '$', '%', '^', '&', '*' };
// ^ 8 Elements, 0-7
char capital[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
// ^ 26 Elements, 0-25
char lower[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
// ^ 26 Elements, 0-25
char number[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
// ^ 10 Elements, 0-9
// Logic Check Variables
bool hasSpecial; // Ensure the password contains at least one special character
bool hasCapital; // Ensure the password contains at least one capital letter
bool hasNumber; // Ensure the password contains at least one number
char randPw[7]; // 8 Character Random Password Array
int randNum;
int genCount = 0; // Used as a loop counter and as randPw array element number
char againChar;
bool again = 1; // True by default because it is set to 1
void randomPasswordGenerator()
{
std::cout << "Now generating your random password . . .";
Sleep(250);
while (genCount < 8)
{ // Begin Loop
/* Generate a random number to decide whether the character
will be a special, capital, lower, or number.
0 = special, 1 = capital, 2 = lower, 3 = number */
randNum = ( rand() % 4 );
switch (randNum)
{
case 0 :
// Generate a random number to pull from the 'special' array
randNum = ( rand() % 8 );
// Assign a random value from the 'special' array to randPw array
randPw[genCount] = special[randNum];
hasSpecial = 1;
break;
case 1 :
// Generate a random number to pull from the 'capital' array
randNum = ( rand() % 26 );
// Assign a random value from the 'capital' array to randPw array
randPw[genCount] = capital[randNum];
hasCapital = 1;
break;
case 2 :
// Generate a random number to pull from the 'lower' array
randNum = ( rand() % 26 );
// Assign a random value from the 'lower' array to randPw array
randPw[genCount] = lower[randNum];
break;
case 3 :
// Generate a random number to pull from the 'number' array
randNum = ( rand() % 10 );
// Assign a random value from the 'number' array to randPw array
randPw[genCount] = number[randNum];
hasNumber = 1;
break;
default :
randPw[genCount] = 'x';
}
/* Once 8 characters have been randomly assigned, check to ensure
password requirements have been met.*/
if (genCount == 7 and hasSpecial and hasCapital and hasNumber)
{
genCount = 8;
hasSpecial = 0;
hasCapital = 0;
hasNumber = 0;
}
else if (genCount == 7)
{
std::cout << " . . .";
genCount = 0;
hasSpecial = 0;
hasCapital = 0;
hasNumber = 0;
}
genCount = genCount + 1;
} // End Loop
}
int main()
{
// Seed the random number!
srand( (int) time(0) );
std::cout << "Welcome to Random Password Generator\n";
Sleep(500);
std::cout << "Version " << versionNumber << "\n";
Sleep(500);
std::cout << "Written by Cody Ray Miller\n\n";
Sleep(500); // usleep(500000);
std::cout << "This program will generate a random 8 character password.\n"
<< "Generated passwords contain at least 1 special character, capital letter, and number.\n\n";
Beep(1000,200); // "\a"
system("pause");
while (again) { // Begin Main Loop
randomPasswordGenerator();
std::cout << "\nYour random password is: " << randPw << "\n";
againChar = 'Q'; // Assigning a nonsense value to againChar
while (againChar == 'Q')
{ // Begin Again Loop
std::cout << "\nDo you want to generate another password? (Y/N)\n";
std::cin >> againChar;
if (againChar == 'N' or againChar == 'n')
{
again = 0;
}
else if (againChar == 'Y' or againChar == 'y')
{
genCount = 0;
}
else
{
std::cout << "User Input Error: Please enter either Y or N.\n";
againChar = 'Q';
}
} // End Again Loop
} // End Main Loop
return 0;
}
#include <iostream>
#include <fstream>
// "FILEditor" by Cody Ray Miller
std::string versionNumber = "1.0"; // Dynamic version number
char text[256]; // // Array used for writing text to files
char operationSelect; // Char used to determine user operation selection
char againChar; // Char used to determine if a loop should run again
char sameFileChar; // Char used to determine if the user wants to use same file
std::string fileName; // String used to store user-entered file name
std::string stringText; // Used for reading text from files
bool again = 1; // Used to determine if program should continue or exit
bool sameFile; // Used to determine if the user wants to change files
int main()
{
std::cout << "Welcome to FILEditor!\n"
<< "Program Version: " << versionNumber << "\n\n";
while (again)
{ // Begin Again Loop
// Ask the user to specify a file name
// Reset the operationSelect char
operationSelect = 'Q';
if (!sameFile)
{
std::cout << "Please specify a file name:\n"
<< "If the file does not exist it will be created during WRITE operation.\n"
<< "(File name must not contain spaces.)\n";
std::cin >> fileName;
}
// Reset sameFile Boolean
sameFile = 0;
while (operationSelect == 'Q')
{ // Begin operationSelect Loop
std::cout << "\nDo you want to read or write to " << fileName << "?\n"
<< "Enter 'R' for READ, 'W' for WRITE.\n";
std::cin >> operationSelect;
if (operationSelect == 'R' or operationSelect == 'r')
{
std::cout << "Reading from " << fileName << " . . .";
std::ifstream myFile(fileName);
// Read the text from the file specified
std::getline(myFile, stringText);
std::cout << "\n" << stringText << "\n\n";
// Close the file
myFile.close();
// Reset againChar
againChar = 'Q';
while (againChar == 'Q')
{
std::cout << "Perform another file operation? (Y/N)\n";
std::cin >> againChar;
if (againChar == 'N' or againChar == 'n')
{
again = 0;
}
else if (againChar == 'Y' or againChar == 'y')
{
again = 1;
sameFileChar = 'Q';
while (sameFileChar == 'Q')
{ // Begin sameFileChar Loop
std::cout << "CURRENT FILE: " << fileName
<< " | Keep using this file? (Y/N)\n";
std::cin >> sameFileChar;
if (sameFileChar == 'Y' or sameFileChar == 'y')
{
sameFile = 1;
}
else if (sameFileChar == 'N' or sameFileChar == 'n')
{
sameFile = 0;
}
else
{
std::cout << "USER INPUT ERROR: Please specify 'Y' or 'N'\n";
sameFileChar = 'Q';
}
} // End sameFileChar Loop
}
else
{
std::cout << "USER INPUT ERROR: Please enter 'Y' or 'N'\n";
againChar = 'Q';
}
}
}
else if (operationSelect == 'W' or operationSelect == 'w')
{
std::ofstream myFile(fileName);
std::cout << "Enter text to be written to " << fileName << ":";
// Clear cin prior to user input
std::cin.clear(); // Clear any error flags
std::cin.sync(); // Clear input buffer
// Get text from the user
std::cin.getline(text, sizeof(text));
// Write the text to the file
myFile << text;
// Close the file
myFile.close();
// Reset againChar
againChar = 'Q';
while (againChar == 'Q')
{
std::cout << "Perform another file operation? (Y/N)\n";
std::cin >> againChar;
if (againChar == 'N' or againChar == 'n')
{
again = 0;
}
else if (againChar == 'Y' or againChar == 'y')
{
again = 1;
sameFileChar = 'Q';
while (sameFileChar == 'Q')
{ // Begin sameFileChar Loop
std::cout << "CURRENT FILE: " << fileName
<< " | Keep using this file? (Y/N)\n";
std::cin >> sameFileChar;
if (sameFileChar == 'Y' or sameFileChar == 'y')
{
sameFile = 1;
}
else if (sameFileChar == 'N' or sameFileChar == 'n')
{
sameFile = 0;
}
else
{
std::cout << "USER INPUT ERROR: Please specify 'Y' or 'N'\n";
sameFileChar = 'Q';
}
} // End sameFileChar Loop
}
else
{
std::cout << "USER INPUT ERROR: Please enter 'Y' or 'N'\n";
againChar = 'Q';
}
}
}
else
{
// Run error handling code and rerun the loop
std::cout << "USER INPUT ERROR: Please enter either 'R' or 'W'";
operationSelect = 'Q';
}
} // End operationSelect Loop
} // End Again Loop
std::cout << "\nThank you for using FILEditor version "
<< versionNumber << "! Goodbye!";
return 0;
}
// Argumenter program by Cody Ray Miller
#include <iostream>
// Counter variable to be used with 'for' loop
int i;
/* 'int argc' holds the number of arguments passed to the program by the user
'char * argv[]' is an array of 'pointers' to char elements */
int main(int argc, char * argv[])
{
// 'for' loops are used to execute code a number of times
// STRUCTURE: for (statement 1, statement 2, statement 3)
// statement 1 = code executed prior to the loop
// statement 2 = condition for loop to run
// statement 3 = code executed after each loop
for ( i = 0; i < argc; i++ )
{
std::cout << "Argument #"<< i << " = " << argv[i] << std::endl;
}
return 0;
}
#include <iostream> // For basic input/output
#include <ctime> // Used to get time for seeding random number
#include <cctype> // Used for 'toupper' function
// Black Jack Game By Cody Ray Miller
std::string versionNumber = "1.0"; // Dynamic Version Number
// Card array variables
// Constant set of 52 cards used for "resetting" the 'cards' array
const char permaCards[] = { 'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K',
'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K',
'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K',
'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K' };
// Array used as a temporary pool of cards when shuffling
char cards[] = { 'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K',
'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K',
'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K',
'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K' };
// Represents a shuffled deck of 52 cards
char deck[52];
// The player's active hand, filled with card chars
char playerHand[12];
// An array to store the individual point values of each card in the player's hand
int playerHandScore[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
// Total value if the player's hand in a single int
int playerHandTotal;
// The player's active hand, filled with card chars
char dealerHand[12];
// An array to store the individual point values of each card in the player's hand
int dealerHandScore[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
// Total value if the player's hand in a single int
int dealerHandTotal;
// Counting variables
int i; // Integer used as a counter for 'for' loops
int a; // Counter for ace 'for' loop
int dealerHandCount = 0; // Used to keep track of the number of cards in dealer's hand
int playerHandCount = 0; // Used to keep track of the number of cards in player's hand
int deckCount = 1; // Used to keep track of how many cards have been pulled from the deck
// Logic variables
bool validCard; // Used by 'shuffle' function to determine if card "drawn" is a non-repeat
bool playerDone; // Is the player DONE making moves?
bool dealerDone; // Is the dealer DONE making moves?
bool playerBust; // Has the player gone BUST?
bool dealerBust; // Has the dealer gone BUST?
bool gameOver; // Is the game OVER?
bool keepPlaying = true; // Keep playing or exit the game?
// Other variables
int randNum; // Integer used for random number assignment
int aceSelect = 1; // Used with 'cin' to receive user input for the desired value of an Ace card
char hitSelect = 'Q'; // Used with 'cin' to determine if the player wants to HIT or STAY
char playAgainChar = 'Q'; // Used win 'cin' to determine if the player wants to play another hand
// Money variables
int playerChips = 500; // Number of chips (dollars) the player starts with
int playerBet; // The amount the player wants to bet per hand
void updatePlayerHandTotal()
{
playerHandTotal = playerHandScore[0]+playerHandScore[1]+playerHandScore[2]+playerHandScore[3]+playerHandScore[4]+playerHandScore[5]+playerHandScore[6]+playerHandScore[7]+playerHandScore[8]+playerHandScore[9]+playerHandScore[10]+playerHandScore[11]+playerHandScore[12]+playerHandScore[13]+playerHandScore[14]+playerHandScore[15];
}
void updateDealerHandTotal()
{
dealerHandTotal = dealerHandScore[0]+dealerHandScore[1]+dealerHandScore[2]+dealerHandScore[3]+dealerHandScore[4]+dealerHandScore[5]+dealerHandScore[6]+dealerHandScore[7]+dealerHandScore[8]+dealerHandScore[9]+dealerHandScore[10]+dealerHandScore[11]+dealerHandScore[12]+dealerHandScore[13]+dealerHandScore[14]+dealerHandScore[15];
}
void updatePlayerHandValues()
{
if (!playerDone)
{
for ( i = 0; i < 16; i++ )
{
switch (playerHand[i])
{
case 'K' : case 'Q' : case 'J' : case 'T' :
playerHandScore[i] = 10;
break;
case '9' :
playerHandScore[i] = 9;
break;
case '8' :
playerHandScore[i] = 8;
break;
case '7' :
playerHandScore[i] = 7;
break;
case '6' :
playerHandScore[i] = 6;
break;
case '5' :
playerHandScore[i] = 5;
break;
case '4' :
playerHandScore[i] = 4;
break;
case '3' :
playerHandScore[i] = 3;
break;
case '2' :
playerHandScore[i] = 2;
break;
case 'A' :
// Display cards in the player's current hand
std::cout << "\nYour hand: ";
// For loop used to output each card in the player's hand
// The loop will run until there are no more cards to display
for ( a = 0 - playerHandCount; a < 0; a++ )
{
std::cout << playerHand[a + playerHandCount];
if (a < -1) { std::cout << ", "; } // Display a comma if NOT the last card of the hand
}
std::cout << "\n\nCard #" << i+1 << " in your hand is an Ace.\n"
<< "Should it be counted as 1 or 11?\n";
std::cin >> aceSelect;
if (aceSelect == 1)
{
std::cout << "Using the Ace as a 1 . . .";
playerHandScore[i] = 1;
}
else if (aceSelect == 11)
{
std::cout << "Using the Ace as an 11 . . .";
playerHandScore[i] = 11;
}
break;
case 'x' :
playerHandScore[i] = 0;
}
}
}
}
void updateDealerHandValues()
{
if (!dealerDone)
{
for ( i = 0; i < 16; i++ )
{
switch (dealerHand[i])
{
case 'A' :
// Dealer will always use an Ace as 11.
// This is unrealistic and should be improved later.
dealerHandScore[i] = 11;
break;
case 'K' : case 'Q' : case 'J' : case 'T' :
dealerHandScore[i] = 10;
break;
case '9' :
dealerHandScore[i] = 9;
break;
case '8' :
dealerHandScore[i] = 8;
break;
case '7' :
dealerHandScore[i] = 7;
break;
case '6' :
dealerHandScore[i] = 6;
break;
case '5' :
dealerHandScore[i] = 5;
break;
case '4' :
dealerHandScore[i] = 4;
break;
case '3' :
dealerHandScore[i] = 3;
break;
case '2' :
dealerHandScore[i] = 2;
break;
case 'x' :
dealerHandScore[i] = 0;
}
}
}
}
void playerTurn()
{
if (!playerDone)
{
hitSelect = 'Q';
while (hitSelect == 'Q')
{
std::cout << "\n\nDo you want to HIT or STAY? (Please enter 'H' or 'S'): ";
std::cin >> hitSelect;
hitSelect = toupper(hitSelect); // Convert character entered to UPPERCASE.
if (hitSelect == 'H')
{
std::cout << "\nDealing card " << deckCount << " to player . . .\n";
playerHand[playerHandCount] = deck[deckCount];
deck[deckCount] = 'x';
playerHandCount++;
deckCount++;
updatePlayerHandValues();
updatePlayerHandTotal();
// Display cards in the player's current hand
std::cout << "\nYour hand: ";
// For loop used to output each card in the player's hand
// The loop will run until there are no more cards to display
for ( i = 0 - playerHandCount; i < 0; i++ )
{
std::cout << playerHand[i + playerHandCount];
if (i < -1) { std::cout << ", "; } // Display a comma if NOT the last card of the hand
}
std::cout << "\n\nThe total of your current hand is . . . " << playerHandTotal << ".";
if (playerHandTotal > 21)
{
playerDone = true;
playerBust = true;
}
}
else if (hitSelect == 'S')
{
std::cout << "\nPlayer will STAY with " << playerHandTotal << ".";
playerDone = true;
}
else
{
std::cout << "Invalid entry. Please enter 'H' or 'S' when prompted.";
hitSelect = 'Q';
}
}
}
}
void dealerTurn()
{
updateDealerHandTotal();
if (!dealerDone)
{
if (dealerHandTotal < 17)
{
std::cout << "\n\nDealing card " << deckCount << " to dealer . . .\n";
dealerHand[dealerHandCount] = deck[deckCount];
deck[deckCount] = 'x';
dealerHandCount++;
deckCount++;
updateDealerHandValues();
updateDealerHandTotal();
// Show the Dealer's cards.
// Display cards in the dealer's current hand
std::cout << "\nDealer's hand: ";
// For loop used to output each card in the dealer's hand
// The loop will run until there are no more cards to display
std::cout << "[?], ";
for ( i = 0 - dealerHandCount + 1; i < 0; i++ )
{
std::cout << dealerHand[i + dealerHandCount];
if (i < -1) { std::cout << ", "; } // Display a comma if NOT the last card of the hand
}
}
else
{
std::cout << "\nDealer will STAY.";
dealerDone = true;
}
}
}
void exitPrompt()
{
playAgainChar = 'Q';
while (playAgainChar == 'Q')
{
std::cout << "\n\nDo you want to play again?\n";
std::cin >> playAgainChar;
playAgainChar = toupper(playAgainChar);
if (playAgainChar == 'Y')
{
keepPlaying = true;
// Reset variables
playerDone = false;
dealerDone = false;
playerBust = false;
dealerBust = false;
gameOver = true;
dealerHandTotal = 0;
playerHandTotal = 0;
deckCount = 1;
dealerHandCount = 0;
playerHandCount = 0;
a = 0;
// Reset player and dealer hand scores,
for (i = 0; i < 16; i++)
{
playerHandScore[i] = 0;
playerHand[i] = 'x';
}
for (i = 0; i < 16; i++)
{
dealerHandScore[i] = 0;
dealerHand[i] = 'x';
}
}
else if (playAgainChar == 'N')
{
keepPlaying = false;
}
else
{
std::cout << "Invalid Input! Please select either 'Y' or 'N'!";
playAgainChar = 'Q';
}
}
}
void checkWin()
{
//std::cout << "\n\nCHECK WIN RUNNING.";
// Update hand totals and check for busts
updatePlayerHandTotal();
updateDealerHandTotal();
if (playerHandTotal > 21)
{
playerBust = true;
}
if (dealerHandTotal > 21)
{
dealerBust = true;
}
if (playerBust and !dealerBust)
{
std::cout << "\n\nPlayer went BUST with " << playerHandTotal << ". Dealer WINS.";
playerChips = playerChips - playerBet;
gameOver = true;
exitPrompt();
}
else if (playerDone and !playerBust and dealerBust)
{
std::cout << "\n\nDealer went BUST with " << dealerHandTotal << ". Player WINS!";
// Show the Dealer's cards.
// Display cards in the dealer's current hand
std::cout << "\nDealer's hand: ";
// For loop used to output each card in the dealer's hand
// The loop will run until there are no more cards to display
for ( i = 0 - dealerHandCount; i < 0; i++ )
{
std::cout << dealerHand[i + dealerHandCount];
if (i < -1) { std::cout << ", "; } // Display a comma if NOT the last card of the hand
}
playerChips = playerChips + playerBet;
gameOver = true;
exitPrompt();
}
else if (playerBust and dealerBust)
{
std::cout << "\n\nPlayer and Dealer both went BUST. Dealer WINS.";
playerChips = playerChips - playerBet;
gameOver = true;
exitPrompt();
} else if (playerDone and dealerDone)
{
if (playerHandTotal > dealerHandTotal)
{
std::cout << "\n\nDealer had " << dealerHandTotal << ". Player wins with "
<< playerHandTotal << "!";
playerChips = playerChips + playerBet;
gameOver = true;
exitPrompt();
}
else if (playerHandTotal < dealerHandTotal)
{
std::cout << "\n\nDealer wins with " << dealerHandTotal << ". (Player had "
<< playerHandTotal << ".)";
playerChips = playerChips - playerBet;
gameOver = true;
exitPrompt();
}
else if (playerHandTotal == dealerHandTotal)
{
std::cout << "\n\nDealer has " << dealerHandTotal << ". Player has "
<< playerHandTotal << ". PUSH!";
gameOver = true;
exitPrompt();
}
}
}
void shuffle()
{
std::cout << "Shuffling deck of cards . . .";
for ( i = 0; i < 52; i++ )
{
// Reset validCard boolean
validCard = false;
// Loop to execute until a valid card is selected for each "deck slot"
while (!validCard)
{
// Generate a random number!
randNum = ( rand() % 52 );
// If the cards array element has not been drawn already
// (i.e. If the value of the randomly generated card array slot
// does not have the value of 'x'...)
if (cards[randNum] != 'x')
{
deck[i] = cards[randNum];
validCard = true;
cards[randNum] = 'x';
}
}
}
// Reset cards
for (i = 0; i < 52; i++)
{
cards[i] = permaCards[i];
}
}
void game()
{
gameOver = false;
// Shuffle the deck of cards and begin the game!
shuffle();
// Betting
std::cout << "\n\nYou have $" << playerChips << ". How much do you want to bet?\n";
std::cin >> playerBet;
std::cout << "\nBetting $" << playerBet << ". . .\n";
// Deal the initial cards to the player and dealer
for ( i = 0; i < 3; i++ )
{
std::cout << "\nDealing card " << i+1 << " to player . . .";
playerHand[playerHandCount] = deck[i];
deck[i] = 'x';
playerHandCount++;
i++;
deckCount++;
std::cout << "\nDealing card " << i+1 << " to dealer . . .";
dealerHand[dealerHandCount] = deck[i];
deck[i] = 'x';
dealerHandCount++;
deckCount++;
}
// Show cards to the player
std::cout << "\nYour hand: " << playerHand[0] << ", " << playerHand[1];
std::cout << "\nDealer's hand: " << "[?]" << ", " << dealerHand[1];
// Update values related to cards in hand and card totals
updatePlayerHandValues();
updateDealerHandValues();
updatePlayerHandTotal();
// Show the player their hand total
std::cout << "\n\nThe total of your current hand is . . . " << playerHandTotal << ".";
while (!gameOver)
{
// Attempt to give the player a turn
// Logic for skipping this function is contained within the 'playerTurn' function
playerTurn();
checkWin();
if (!gameOver)
{
dealerTurn();
checkWin();
}
}
}
int main()
{
// Seed the random number
srand( (int) time(0) );
while (keepPlaying)
{
game();
}
return 0;
}
Programming should be fun and accessible to everyone. This course is designed with the complete beginner in mind. Don't know anything about programming? No problem! This is the course for you.
Inside the course, you'll be creating REAL programs with each new module. Learn by doing, rather than reading a stuffy old textbook.
Use real-life coding skills to solve programming challenges and increase your abilities. No need to memorize information, as concepts are reinforced while you learn. Each video takes you from one set of concepts to the next, anchoring your knowledge through application.
What's stopping you from a new and exciting career in programming? There's no need to enroll in an expensive collegiate program. Employers hire people who know how to do what they need done and programming is more in-demand than ever before.
So, what are you waiting for? With a full 30 day moneyback guarantee, you have nothing to lose. Try the course out and if it isn't the best C++ course you've ever experienced, I'll give you a complete refund. No questions asked.
Ready to get started? Enroll today... and I'll see YOU on the inside!