-4

both of my options are coming up when I run the code

I have tried two if statements else if and else

cout << "would you like to hit or stand?" << endl; //asking if you would ike to hit or stand

bool hit; //the hjitting opption
bool stand; // the stand / hit option

cin >> hit, stand; // the avaiabillity for hitting or standing (player input)
if ( hit = true) // if you hit
{
    for (n = 1; n <= 1; n++) //loop for how many cards are you have
    {
        num = rand() % 13; //get random number
        cout << num << "\t"; //outputting the number
    }
}

{
    cout << "you stand \n"; // saying you stand

I expect the code to output the number when u say hit and say you stand when you say stand but it is out putting either just the hit just the stand or bothenter code here

Scheff's Cat
  • 16,517
  • 5
  • 25
  • 45
Chris.B
  • 7
  • 1

2 Answers2

2

The snippet:

bool hit;
bool stand;
cin >> hit, stand; 

does not magically set one of the booleans based on what you enter. Your cin statement will attempt to get two separate booleans from the user.

What you probably want to do is get a string on then act on that, something like:

std::string response;
std::cin >> response;
if (response == "hit") {
    do_hitty_things();
} else if (response == "stand") {
    do_standy_things();
} else {
    get_quizzical_look_from_dealer();
}

In addition (though irrelevant if you take my advice), the expression hit = true is an assignment rather than a comparison. A comparison would use ==. The result of if (hit = true) is to first set hit to true and then use that as the condition. Hence it will always be true.

Also see here for the absurdity of explicitly checking booleans against true and/or false.

paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
-1

Hit or stand is one choice so you need one boolean variable.

bool hit;
cin >> hit;

hit is a boolean variable so it's already true of false, you don't need to compare it with true (or false). So just if (hit) is OK. If you were to compare it with true then it's == not =, so if (hit == true) would be OK as well.

Finally since your choice results in two alternatives you need an if ... else ... statement.

if (hit)
{
    for (n = 1; n <= 1; n++) //loop for how many cards are you have
    {
        num = rand() % 13; //get random number
        cout << num << "\t"; //outputting the number
    }
}
else
{
    cout << "you stand \n"; // saying you stand
}

When you are still learning the basics of C++ syntax and rules you need to write smaller amounts of code. Even in this short program you had multiple errors and it's hard to figure out what's wrong when that happens. At this stage you should literally be writing one line of code at a time. Test that to make sure it's works before you write the next line.

john
  • 71,156
  • 4
  • 49
  • 68
  • Since OP has stated they want to enter "hit" and/or "stand", a boolean is probably the wrong choice. – paxdiablo Mar 29 '19 at 06:46
  • Hit or stand is a single choice. Of course you make multiple such choices, which obviously then requires a loop. I think the OP is generally confused and needs to slow down a little, or try something simpler first. – john Mar 29 '19 at 06:55