Y/N Loops

I need to make a loop that will make this program run again if the user chooses to do so but so far it hasn't been working out. I've been trying to make the loop and i have a slight idea of what it looks like but I can't seem to get it right. There's more to this program but I went ahead and shortened it.



--------------------------------------------------------------------------------

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <string>
using namespace std;

int main(void)

{
   char response;
   do
   {
		//// 1 Input Hand of Playing Cards ////
		int x,y,z;
		while ( true )
		{
			cout << "Enter value of cards in hand ranging from 1-13 and separated by spaces: " << endl;
			cin >> x >> y >> z;
			cin.ignore(999,'\n');
	
			if ( cin.fail() )
			{
				cin.clear();
				cin.ignore(999,'\n');
				cout << endl;
				cout << "Ivalid card values. Please try again." << endl;
				cout << endl;
				continue;
			}
			if ( x <= 0 || y <= 0 || z <= 0 )
			{
				cout << endl;
				cout << "Card values not within given range. Please try again." << endl;
				cout << endl;
				continue;
			}
			if ( x > 13 || y > 13 || z > 13 )
			{
				cout << endl;
				cout << "Card values not within given range. Please try again." << endl;
				cout << endl;
				continue;
			}
			break;
		}

		//// 2 Process Hand ////

		//// 2.1 Identify Cards in Hand ////
	
		int leastValueCard, medianCard, greatestValueCard;
		if ( x <= y && y <= z )
		{
			leastValueCard = x;
			medianCard = y;
			greatestValueCard = z;
		}
		else if ( x <= z && z <= y )
		{
			leastValueCard = x;
			medianCard = z;
			greatestValueCard = y;
		}
		else if ( y <= z && z <= x )
		{
			leastValueCard = x;
			medianCard = z;
			greatestValueCard = x;
		}
		else if ( y <= x && x <= z )
		{
			leastValueCard = y;
			medianCard = x;
			greatestValueCard = z;
		}
		else if ( z <= x && x <= y )
		{	
			leastValueCard = z;
			medianCard = x;
			greatestValueCard = y;
		}
		else if ( z <= y && y <= x )
		{
			leastValueCard = z;
			medianCard = y;
			greatestValueCard = x;
		}
	
	
	
	
	
	
	
	
	
	
		////  Output Result ////

		//// Three's ////
		if ( x == x && y == x && z == x && x == 1) 
		{
			cout << endl;
			cout << "You have three Aces! " << endl;
		}
		else if ( x == x && y == x && z==x && x== 13 )
		{
			cout << endl;
			cout << "You have three Kings!" << endl;
		}
		else if ( x == x && y == x && z==x && x== 12 )
		{
			cout << endl;
			cout << "You have three Queens!" << endl;
		}
		else if ( x == x && y == x && z==x && x== 11 )
		{
			cout << endl;
			cout << "You have three Jacks!" << endl;
		}
		else if ( x == x && y == x && z==x && x== 2 )
		{
			cout << endl;
			cout << "You have three Twos." << endl;
		}
		else if ( x == x && y == x && z==x && x== 3 )
		{
			cout << endl;
			cout << "You have three Threes." << endl;
		}
	 
		else if ( x == x && y == x && z==x && x== 4 )
		{
			cout << endl;
			cout << "You have three Fours." << endl;
		}
		else if ( x == x && y == x && z==x && x== 5 )
		{
			cout << endl;
			cout << "You have three Fives." << endl;
		}
		else if ( x == x && y == x && z==x && x== 6 )
		{
			cout << endl;
			cout << "You have three Sixes." << endl;
		}
		else if ( x == x && y == x && z==x && x== 7 )
		{
			cout << endl;
			cout << "You have three Sevens." << endl;
		}
		else if ( x == x && y == x && z==x && x== 8 )
		{
			cout << endl;
			cout << "You have three Eights" << endl;
		}
		else if ( x == x && y == x && z==x && x== 9 )
		{
			cout << endl;
			cout << "You have three Nines." << endl;
		}
		else if ( x == x && y == x && z==x && x== 10 )
		{
			cout << endl;
			cout << "You have three Tens." << endl;
		}
	

		
		//// Reinitiate Program ////
		char response;
		cout << endl;
		cout << "Would you like to run the program again? (y/n)" 
                << endl;
		cin >> response;
		cin.ignore(999,'\n');
		return 0;
	
	} while (response == 'y');
        return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
int main()
{
   char response;
   do
   {
      // The rest of you code goes here
   } while (response == 'y');
   return 0;
}


For the future, try to use code tags & decent indentation to make your code easier to read.
You're going to need to upload your code the right way, before anybody is going to help you.

Upload it like this;

[co.de] INSERT CODE HERE [/co.de]

Your code goes in between. (Also take out the period between the O's and the D's)

Should look like this, for example;

cout<<"Hello!";

This way, the lines of codes will be numbered and it will make it easier on anybody trying to help you.
Last edited on
Alright thanks for the uploading tips, I was wondering how to do that. Thanks. But now it's saying that the variable response is being used without being initialized. I tried fixing it but it wouldn't compile afterward
Last edited on
Line 169: extraneous response variable, unneeded.

Line 175: extra return statement, which causes your program to end before it hits the loop condition.

Get rid of those two lines, and the entire program should work swell. Or at least when I ran it on the shell.
Topic archived. No new replies allowed.