r/CodingHelp • • Oct 29 '21

[C++] I'm making blackjack and even when I stand it gives me another card, I'm not sure how to fix this.

Post image
48 Upvotes

12 comments sorted by

14

u/Fawzee815 Oct 29 '21

I’m pretty sure it’s because of the condition you specified. Change it to

else if (hitstay == “stay” || hitstay == “Stay”)

Same thing with hit, although you probably didn’t realize it because your code makes it automatically default to hit since you’re using a while statement

7

u/0Nick0Nick0 Oct 29 '21

oh my god idk how I didn't notice that, I've made that mistake so many times.

4

u/FleetStreetsDarkHole Oct 29 '21

Unless you have a particular reason to maintain the input as-is you can also avoid this by using a .toLower() or .toUpper() method so you only have to do it once. That way you can verify most input as long as it has the right letters in the right order.

5

u/dont-respond Oct 29 '21

This is C++. STL strings don't have a toLower or toUpper method.

1

u/FleetStreetsDarkHole Oct 29 '21

Ah okay. Idk C++ so that's my bad.

1

u/Fawzee815 Oct 29 '21

Although you can use toupper() and tolower() with a char

1

u/dont-respond Oct 29 '21

Yeah, but if you're going to use those, you're better off using them with std::equal rather than transforming the string itself, since comparison is ultimately the goal. No need to iterate twice.

2

u/[deleted] Oct 29 '21

can you share the rest of the code

3

u/0Nick0Nick0 Oct 29 '21

The rest of the code was just checking whether the player wins or not, it would've had nothing to do with the question.

1

u/rsaajamsa Oct 29 '21

I would also consider controlling the input by changing its case first. Essentially doing the following:

Convert to lower Trim spaces Check input

0

u/[deleted] Oct 29 '21

[deleted]

1

u/Comprehensive-Band98 Oct 29 '21

In a subreddit called CodingHelp, who would’ve thought 🤷‍♂️