fully iterating before termination


I want this code to keep iterating and check the whole string before terminating the while loop i have setup over this.

the problem is that it terminates as soon as it checks an element and it happens to not be zero. I need to check the entire line rather than individual elements.

is it even possible to do 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

for(int i = 0; i < 4; i++){ //step through rows 
			for(int j = 0; j < 13; j++){ //step through columns
				string comparableCard = face[j] + " of " + suit[i];
				if(card.compare(0, 16, comparableCard) == 0){
					table[i][j] = table[i][j] + 1;
				}

				if(to_string(table[0][j]).find("0") == string::npos){
					end = false;
				}
				if(to_string(table[1][j]).find("0") == string::npos){
					end = false;
				}
				if(to_string(table[2][j]).find("0") == string::npos){
					end = false;
				}
				if(to_string(table[3][j]).find("0") == string::npos){
					end = false;
				}

			}	
	  	}
I don't see a while loop in your code, so it's hard to understand exactly what you're doing/want to do.
I assume 'card' is type string?

I think you're going about this perhaps wrong? You should store the cards a way that's the easiest to use for processing. Only convert them to "Blah of Bloo" strings for display purposes.
Last edited on
I'm trying to grab an entire row of an array and check if it has any zeros. If there aren't any zeros I want to exit my while loop.

sorry the condition for the while loop is just above those for loops it has while(end = true)

and yes card is type string
What's the underlying type of table? What are lines 9/10 and the others similar trying to do?
its a 2d array 4x13. lines 9/10 are trying to turn each row of the array into a string and check for a 0. if they don't find a 0 in the row, then return false to the while loop and exit the loop
Last edited on
... you appear to convert 'something' to a string. what was it before you did that? what possible values could it have? I suspect this isnt necessary: these look like cards... what makes a card have a zero? 10? anything else (are all face cards 10 ?).
You're still not explaining this very clearly; I'm sure I'm not alone in that opinion. What the purpose of this "0"? Is it some sort of sentinel value?

I can guarantee you that the way you're stringifying your data for comparisons is (1) not optimal and (2) confusing for both the programmer and the reader (evidence: Your issue has not yet been resolved).

Even something still partially stringy like the following may clearer, in my opinion:
1
2
3
4
5
6
7
8
9
10
11
struct Card
{
   string suit;
   string face;
   
   bool operator==(const Card& other)
   {
        return this.suit == other.suit &&
               this.face == other.face;   
   }
}

Then you can use the == to compare Card structs themselves.
Last edited on
the table keeps track of cards generated 4 being diamonds, hearts, etc. and 13 being ace through king.

the while loop generates random cards and compares them to two string arrays, suits and face which contain all the suits and faces respectively.

if the random card generated matches the suit and the face, then we increment the table at that index in the table to mark that one card of a certain suit and face has been generated.

the while loop needs to terminate when ace-king has been shown at least once for one suit only.

any of the suits can be completed first because the cards are randomly generated.

hence why I'm trying to check each row for a 0, because a 0 would mean that a card hasn't been seen yet. if any of the rows don't have a 0, all of the cards for that suit have been seen and i need to terminate the loop
not sure its related but you may want a multi-state (enum or similar) for cards, (nothing matches, suit matches, value matches, both match). but it depends what game you are playing too.

you ARE checking all the cards. there is NO early exit. If it is doing something weird, look at 5/6 lines, its probably that. remember that strings must be exact matches, case vs case, space vs space, everything exact. its easy to cook up a string that looks like a match but isnt.
SSDEEZ, that is much clearer now. (Will write a little something up later if I have time, or maybe somebody will beat me)
Last edited on
You never did show us the while loop. The quick snippet you posted, though, has a major error in it.

sorry the condition for the while loop is just above those for loops it has while(end = true)


This while loop will always evaluate "true" because you are assigning true to end. You probably want while(end == true). You may actually have that, but that is not what you posted.

None of this is related to the question that you asked, but it will give you fits at some point in time.
Take a look at the any_suit_contains_full_run and amend it for your purposes. It helps you think if you find logical places to split the functionality into separate functions.

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
#include <iostream>
#include <string>
#include <cstdlib>

const int NumSuits = 4;
const int NumFaces = 13;

struct Card {
	int suit; // 1, 2, 3, 4
	int face; // 1, 2, 3, ... 13
};

static const char* const suits[] = {
	"Clubs", "Hearts", "Spades", "Diamonds" };
static const char* const faces[] = { "Ace", "2", "3", "4", "5",
	"6", "7", "8", "9", "10", "Jack", "Queen", "King" };

std::string to_string(const Card& card)
{
	return std::string(faces[card.face - 1]) + " of " + std::string(suits[card.suit - 1]);
}

// expects 1-based suit number
std::string suit_to_string(int suit)
{
	return suits[suit - 1];
}

// Just for demonstration.
// In a real situation, you'd want to pick out of a shuffled deck.
Card generate_random_card()
{
	// just using C-style rand for brevity
	return Card { rand() % NumSuits + 1,
	              rand() % NumFaces + 1 };
}

// returns 1-based suit that was found, otherwise 0
int any_suit_contains_full_run(int table[][NumFaces])
{
	for (int i = 0; i < NumSuits; i++)
	{
		bool missing_card = false;
		for (int j = 0; j < NumFaces; j++)
		{
			if (table[i][j] == 0)
			{
				missing_card = true;
				break;
			}
		}	
		
		if (!missing_card)
		{
			return (i + 1);
		}
	}
	
	return 0; // all suits contain at least one missing card
}

void print_table(int table[][NumFaces])
{
	for (int i = 0; i < NumSuits; i++)
	{
		for (int j = 0; j < NumFaces; j++)
		{
			std::cout << table[i][j] << ' ';
		}
		std::cout << '\n';
	}
}

int main()
{
	int table[NumSuits][NumFaces] { };

	while (true)
	{	
		//print_table(table);
		
		Card card = generate_random_card();

		std::cout << "Adding a " << to_string(card) << '\n';
		
		// add card to table
		table[card.suit - 1][card.face - 1]++;
		
		int suit = 0;
		if ((suit = any_suit_contains_full_run(table)) != 0)
		{
			std::cout << "\nFull run of: " << suit_to_string(suit) << '\n';
			
			print_table(table);
					
			break;
		}
	}
}

Adding a 8 of Hearts
...
Adding a Jack of Diamonds

Full run of: Diamonds
1 1 1 0 1 3 3 1 3 1 3 3 1
3 1 4 0 4 3 1 4 1 4 4 4 2
3 2 3 0 5 4 2 0 1 2 3 2 1
3 3 2 2 1 5 1 1 2 1 1 1 1
Last edited on
Topic archived. No new replies allowed.