Logic on my program

Posted on 2007-07-20
Last Modified: 2010-04-01
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'};

   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

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...
Question by:CPlusJavaCSharp
    1 Comment
    LVL 45

    Accepted Solution

    Hi CPlusJavaCSharp,

    You're correct in that the problem lies with this line:

      deck[SUITS * i + j] = rank[i] + suit[j];

    The underlying type for both rank[] and suit[] is char, which is an 8-bit integer.  C++ is dutifully adding the characters together when what you seem to want is to concatenate them.

    The easiest solution might be a simple function that Deck() that puts the two characters together into a string.

    char *Deck (int Suit, int Rank)
      static Name[4];

      Name[0] = suit[Suit];
      Name[1] = ' ';
      Name[2] = rank[Rank];
      Name[3] = 0;
      return Name;

    Note that you'll need to move the declarations for rank[] and suit[] to global storage.  But that's something that you should do anyway.  It usually not a good idea to initialize arrays on the stack.

    Good Luck,

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now