Need help with do-while/validation

I am having problems at the end of the program with the do-while loop to restart the program and the validation. Can someone help guide me through this?
sorry if this is the wrong place for 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <string> 
#include <iomanip>
#include <cstdlib>
#include <time.h>
using namespace std;
int main ()
{
	double guessingcardnum; 
	int guessingcardsuit;
	int generatednum;
	int	generatedsuit;
	string cardsuit;
	string cardnum;
	//char dowhileloop1;
	char dowhileloop2;
	int wins = 0;
		int loses = 0;
//the number/suit generator
	do {
//do {
	srand(time(0));
	generatednum = rand() % 13 + 1;
	generatedsuit = rand() % 4 + 1;

//the user input. 

	cout << "Welcome to the card guessing game \n"; 
	do {	
	cout << "Please type a number 1-13 (1 being ace)  \n";
	cout << "enter your number now: "; cin >> guessingcardnum;
	if (guessingcardnum > 13)
	{
	cout << " You did not pick a number 1-13, Please pick a number from that range \n";
	}
	} while(guessingcardnum > 13);
	
	cout << "\n";
	do {
	cout << "Also please guess the suit by typing 1-4.\n";
    cout << "1 : spades             3:Diamonds \n";
	cout << "2 : clubs             4:Hearts \n"; 
	cout << " enter your card suit now:";cin >> guessingcardsuit;
	if (guessingcardsuit > 4)
	{
	cout << " You did not pick a number 1-4, Please pick a number 1-4 \n";
	}
	} while (guessingcardsuit > 4);
	cout << "\n";
	cout << "Lets see if you're lucky \n";

	if ( generatedsuit == 1) 
		cardsuit = "Spades"; 
	else if (generatedsuit == 2)
		cardsuit = "Clubs";
	else  if (generatedsuit == 3)
		cardsuit = "Diamonds";
	else if (generatedsuit == 4)
		cardsuit = "Hearts";
	/* if (generatednum == 11)
		generatednum = "Jack";
	 else if (generatednum == 1)
		generatednum = "Ace";
	 else if (generatednum == 12)
		generatednum =  "Queen";
	 else if (generatednum == 13)
		generatednum ="King";*/
	 

	 if (guessingcardnum == generatednum && guessingcardsuit == generatedsuit)
	 {
		 cout << "Good job you guess the right card! \n";
			wins++;
	
	 }
	 else if (guessingcardnum != generatednum || guessingcardsuit != generatedsuit)
		 {	cout << " sorry but you did not guess the correct card \n";
       cout << " The correct card was " << generatednum <<  " of "  << cardsuit << "\n";
	 loses++;
	 }
do {	
	 cout << " Do you want to play again? Press Y for yes or N for no:";
	cin >> dowhileloop2;
	cout << "\n";
	if (dowhileloop2 != 'Y' || 'y' ||'N' || 'n');
	{
		cout << " You need to either press ""Y"" or ""N"" \n";
	}
} while (dowhileloop2 != 'Y' || 'y' ||   'N' || 'n');
	} while (dowhileloop2 == 'Y' || 'y');
	
	return 0;
}
Last edited on
let me do the if for you and you can do the same for do-while.

1
2
3
4
5
6
7
cin >> dowhileloop2;
cout << "\n";
tolower(dowhileloop2);
if (!(dowhileloop2 == 'y' || dowhileloop2 == 'n'))
{
	cout << " You need to either press ""Y"" or ""N"" \n";
}



By making the character into lower case, we can make the code a bit more simple to look. Also, check the ; after the if.
I'm still having the problem with the code. where if i press "y" it still comes up with the error message "you need to either press "y or "n". It really should start back at the top, so you can play again.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <time.h>
using namespace std;
int main ()
{
	double guessingcardnum;
	int guessingcardsuit;
	int generatednum;
	int	generatedsuit;
	string cardsuit;
	string cardnum;
	//char dowhileloop1;
	char dowhileloop2;
	int wins = 0;
		int loses = 0;
//the number/suit generator
	do {
//do {
	srand(time(0));
	generatednum = rand() % 13 + 1;
	generatedsuit = rand() % 4 + 1;

//the user input.

	cout << "Welcome to the card guessing game \n";
	do {
	cout << "Please type a number 1-13 (1 being ace)  \n";
	cout << "enter your number now: "; cin >> guessingcardnum;
	if (guessingcardnum > 13)
	{
	cout << " You did not pick a number 1-13, Please pick a number from that range \n";
	}
	} while(guessingcardnum > 13);

	cout << "\n";
	do {
	cout << "Also please guess the suit by typing 1-4.\n";
    cout << "1 : spades             3:Diamonds \n";
	cout << "2 : clubs             4:Hearts \n";
	cout << " enter your card suit now:";cin >> guessingcardsuit;
	if (guessingcardsuit > 4)
	{
	cout << " You did not pick a number 1-4, Please pick a number 1-4 \n";
	}
	} while (guessingcardsuit > 4);
	cout << "\n";
	cout << "Lets see if you're lucky \n";

	if ( generatedsuit == 1)
		cardsuit = "Spades";
	else if (generatedsuit == 2)
		cardsuit = "Clubs";
	else  if (generatedsuit == 3)
		cardsuit = "Diamonds";
	else if (generatedsuit == 4)
		cardsuit = "Hearts";
	/* if (generatednum == 11)
		generatednum = "Jack";
	 else if (generatednum == 1)
		generatednum = "Ace";
	 else if (generatednum == 12)
		generatednum =  "Queen";
	 else if (generatednum == 13)
		generatednum ="King";*/


	 if (guessingcardnum == generatednum && guessingcardsuit == generatedsuit)
	 {
		 cout << "Good job you guess the right card! \n";
			wins++;

	 }
	 else if (guessingcardnum != generatednum || guessingcardsuit != generatedsuit)
		 {	cout << " sorry but you did not guess the correct card \n";
       cout << " The correct card was " << generatednum <<  " of "  << cardsuit << "\n";
	 loses++;
	 }
do {
	 cout << " Do you want to play again? Press Y for yes or N for no:";
	cin >> dowhileloop2;
	cout << "\n";
	if (!(dowhileloop2 == 'y' || dowhileloop2 == 'n'))
	{
		cout << " You need to either press ""Y"" or ""N"" \n";
	}
} while (dowhileloop2 != 'y' && dowhileloop2 != 'n'); //&& instead of ||
	} while (dowhileloop2 =='y');

	return 0;
}
Last edited on
Topic archived. No new replies allowed.