Hi, I need help on checking duplicate values on a user input 2D array. I have this code so far and I am stuck on how to move on forward.
What I want it to do:
To check if the user entered 2 card values that are the same (the suit values doesn't matter if its the same). And if it is the same ask them for a new card value.
What it does:
After I enter my first card value, my code to let the user know they entered 2 same values pops up.
#define _CRT_SECURE_NO_WARNINGS
#define col 5
#define row 2
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main(void)
{
int card[row][col];
int i, j, l, k;
char units[20] = "cards";
printf("\Please enter the card value followed by its suit\n");
for (j = 0; j < col; j++)
{
for (i = 0; i < row; i++)
{
int rest = 13;
if (i == 1)
{
strcpy(units, "suits");
rest = 4;
}
scanf("%i", &card[i][j]);
while (card[i][j] > rest || card[i][j] < 1)
{
printf("\nOnly enter %s value between 1 to %i.\n", units, rest);
scanf("%i", &card[i][j]);
}
for (k = 0; k < row; k++)
{
for (l = 0; l < col; l++)
{
while (card[k][j] == card[i][j])
{
printf("You entered 2 identical cards, try again:\n");
scanf("%i", &card[i][j]);
}
}
}
}
}
printf("\nThe card value and suit that you've chosen are:\n");
for (i = 0; i < row; i++)
{
if (i == 1)
{
printf("\n");
}
for (j = 0; j < col; j++)
{
printf("%i ", card[i][j]);
}
}
while (!_kbhit());
}
please explain in moar words what it does not do correctly vs what you want it to do.
what is a card, is it an int from {0-52? something like this? something else?}
if its a standard 52 card deck (which it seems to be) you can just make a counter, eg
int counter[52] = {0};
and when the user puts a card in, ++ that card:
counter[card_ID]++;
and then
if(counter[card_ID] > 1) //they put in a duplicate, deal with it and set counter[card_ID] back to 1.
this could make the task much easier than having to loop over all the cards looking for matches etc. say the 2 of clubs is card #3. you have none, they put one in, you have 1, they put another one in later, you catch it without loops or searching its just known to already be in use.
For the cards it's a user input value that goes in a 2D array (2 rows and 5 columns)
the 1st row is for the values and the 2nd row is for suit.
eg.
1st row: 1 9 8 13 1
2nd row: 3 2 1 1 3
What I want my code to do is to check if the user entered duplicate values (in this case its the value "1") and tell them to enter a new value since it can't be duplicate.
for the approach I said, if you defined suits 0-3 and cards 1-13 (ace, 2..10, jack, queen, king) you can map as I said above into a table of 'used cards'. int deck[13*4+1], and any given card is deck[suit*13+faceval]. I suggest you name the constants with some enums, eg enum suits{clubs, ..}