troubleshooting Question

Logic on my program

Avatar of CPlusJavaCSharp
CPlusJavaCSharp asked on
C++
1 Comment1 Solution320 ViewsLast Modified:
The purpose is to randomly print out five cards...
What I have compiles but does not work:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
   char rank[] = {'2','3','4','5','6','7','8','9','T','J','Q','K','A'};
   char suit[] = {'C','D','H','S'};

   srand(static_cast<unsigned>(time(0)));
   
   const int SUITS = 4;
   const int RANKS = 13;
   const int N = SUITS * RANKS;
   const int NUMCARDS = 5;

   char deck[N];
   bool used[N];

   for (int i = 0; i < N; i++)
         used[i] = false;

   for (int i = 0; i < RANKS; i++)
         {
           for (int j = 0; j < SUITS; j++)
                   {
                     deck[SUITS * i + j] = rank[i] + suit[j];
                   }
         }

   for (int i = 0; i < N; i++)
         {
           int r = i + (rand() * (N - i));
             //int r = i + rand() / (RAND_MAX / (N - i) + 1);
             char t = deck[r];
             deck[r] = deck[i];
             deck[i] = t;
         }

   for (int i = 0; i < NUMCARDS; i++)
         cout << deck[i] << endl;

   return 0;
}

My problem I believe lies on this line
deck[SUITS * i + j] = rank[i] + suit[j];

How can I fix my code here?

Also-This line
//int r = i + rand() / (RAND_MAX / (N - i) + 1);
I found online from stanford..does it look fair

Finally
I want to check for cards that have been used thus
 bool used[N];

   for (int i = 0; i < N; i++)
         used[i] = false;
Where would be the best place to place it...
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros