Beginning Blackjack Code-DoWhile/Switch Problem

Hi, I am currently having a problem with my Blackjack game code. I have little experience with C++ and am learning by video tutorials, so I have very limited knowledge of C++ besides a few of the basics. I would appreciate any hints. I'm looking to learn, not just get an answer. So far my code just asks the user if they would like to play or not and exits the game if the type 'n' and asks them to enter y or n if they fail to type either in. The problem comes here: when the user inputs something other than 'y' or 'n', it asks them to "Enter y or n"; then, if they try to type in 'n', it does not display "Thank you for playing.", like it normally would if you originally typed in 'n'. I cannot quite grasp why it will not display "Thank you for playing." Again, any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
using namespace std;

int main()
{
	char a;
	cout << "Would you like to play blackjack? (y/n)" << endl;
	cin >> a;
	do
	{
		switch (a)
		{
			case 'y':
				break;
			case 'n':
				cout << "Thank you for playing." << endl;
				exit(0);
			default:
				cout << "Enter y or n: " << endl;
				cin >> a;
				break;
		}
	} while (a != 'n' && a != 'y');
	cin.ignore();
	cin.get();
	return 0;
closed account (3qX21hU5)
The answer lies in your do while statement.

Lets go through it step by step. Lets say the user enters a 'k' the first time around for the cin on line 9. So since we don't have a switch case for the letter 'k' we hit the default case on line 19. It asks us to enter another letter so this time we enter a 'n'.

So now a == 'n'. So we move on to check the do-while condition which is (a != 'n' && a != 'y'). So when we hit that the condition is actually false because remember a == 'n' so we fail the first test.

So then we exit the loop and go on to these statements

1
2
3
cin.ignore();
cin.get();
return 0;


So the reason why it doesn't display "Thank you for playing" is because it never gets the chance to do a second iteration of the do-while loop so it never hits the 'n' case.

Hope that was a clear explanation ;p if you have any questions just let me know.
Last edited on
Thank you very much! I've taken out the a!=n and now it works just as I had hoped. I forgot that since that condition was at the end, it would not loop back. I'm also now realizing that if they type in 'k' and then 'y', it will also not loop back. So, it would seem I need no condition in my do while statement, but I need it so that when something other than 'y' or 'n', it can repeat "Enter y or n". I changed the condition to (a==a) and it seems to work, but this seems somehow redundant or unnecessary. Is there a simpler or more efficient way to do this?
closed account (3qX21hU5)
Yes there is :) there is actually a bunch of different ways you can do it.

One of them is consider moving your first cin statement (The one on line 9) inside of your do-while loop. Make it so that the cin statement is the first thing that is called in the loop.

Then you can delete the cin statement inside the default case.

This way when the user enter's a letter that is not a 'n' or 'y' it will hit the default loop output 'Enter y or n: " and then restart the loop again and they will hit the cin statement at the top of the loop.

So something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
using namespace std;

int main()
{
	char a;
	cout << "Would you like to play blackjack? (y/n)" << endl;
	do
	{
	        cin >> a;
		switch (a)
		{
			case 'y':
				break;
			case 'n':
				cout << "Thank you for playing." << endl;
				return 0;
			default:
				cout << "Enter y or n: " << endl;
				break;
		}
	} while (a != 'n' && a != 'y');
	cin.ignore();
	cin.get();
	return 0;
}


There is of course other ways to do it that can be better.
Last edited on
I tried that out and I like it a lot; it seems a little better. Thank you for all your help. As I learn more I'm going to keep working on this mini-project and I'll keep experimenting with different ways to do it. Thanks again :)
Topic archived. No new replies allowed.