I am creating a program for my Computer Science Final and for some reason no mat
ID: 3836605 • Letter: I
Question
I am creating a program for my Computer Science Final and for some reason no matter what I enter into the output box the answer always comes up as "Stand!" Could someone please tell me what I am doing wrong? Why is the program not reading my command[1]? Thank you
#include<iostream>
#include <string>
#include <iomanip>
using namespace std;
//This program will tell you if you should hit or stand for hard hands only while playing blackjack/21
int main() //Function
{
int dealer; //What card the dealer is showing
int user; //User's current hand
string command[2] = { "Hit", "Stand" }; //Array
//Beginning of Program
cout << "This program will tell you if you should Hit or Stand ";
cout << "------------------------------------------------------- ";
cout << " J, Q, K = 10 ";
cout << " Aces = 1 or 11 ";
//Ask what the dealer is showing
cout << "What is the dealer showing? ";
cin >> dealer;
//Loop: if the user enters a number not allowed
while (dealer < 1)
{
cout << "The hand must be at least showing an ace ";
cout << "What is the dealer showing? : ";
cin >> dealer;
}
//Ask what the user's hand is.
cout << "What is your hand?: ";
cin >> user;
//Loop 2
while (user < 3)
{
cout << "The hand must be at least 3. ";
cout << "Enter the dealers hand : ";
cin >> dealer;
}
// Conditional & Array: Determine the results.
if (user <= 12 && dealer < 7)
cout << "" << command[1] << "! ";
else if (user > 12 && dealer < 7)
cout << "" << command[2] << "! ";
else if (user < 12 && dealer >= 7)
cout << "" << command[1] << "! ";
else if (user > 12 && user < 17 && dealer > 7)
cout << "" << command[1] << "! ";
else if (user >= 17 && user <= 21)
cout << "" << command[2] << "! ";
system("pause");
return 0;
}
Explanation / Answer
#include<iostream>
#include <string>
#include <iomanip>
using namespace std;
//This program will tell you if you should hit or stand for hard hands only while playing blackjack/21
int main() //Function
{
int dealer; //What card the dealer is showing
int user; //User's current hand
string command[2] = { "Hit", "Stand" }; //Array
//Beginning of Program
cout << "This program will tell you if you should Hit or Stand ";
cout << "------------------------------------------------------- ";
cout << " J, Q, K = 10 ";
cout << " Aces = 1 or 11 ";
//Ask what the dealer is showing
cout << "What is the dealer showing? ";
cin >> dealer;
//Loop: if the user enters a number not allowed
while (dealer < 1)
{
cout << "The hand must be at least showing an ace ";
cout << "What is the dealer showing? : ";
cin >> dealer;
}
//Ask what the user's hand is.
cout << "What is your hand?: ";
cin >> user;
//Loop 2
while (user < 3)
{
cout << "The hand must be at least 3. ";
cout << "Enter the dealers hand : ";
cin >> dealer;
}
// Conditional & Array: Determine the results.
if (user <= 12 && dealer < 7)
cout << "" << command[0] << "! ";
else if (user > 12 && dealer < 7)
cout << "" << command[1] << "! ";
else if (user < 12 && dealer >= 7)
cout << "" << command[0] << "! ";
else if (user > 12 && user < 17 && dealer > 7)
cout << "" << command[0] << "! ";
else if (user >= 17 && user <= 21)
cout << "" << command[1] << "! ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.