I am trying to do a deck of cards in linear form so that I would be able to tell if I am guessing the right card. The program should be able to tell if the guess is getting closer or not. I made a random number generator (randomNum = rand() % 52) and the user inputs the rank and suit.

What I did with this is 2 of clubs would be the first card in the deck, Jack of Clubs would be no. 10, followed by the queen, king, and ace.
The "lowest card" would be 2C and the highest card would be the ace of spades. Is there anyway that I can make this efficient?

//Checks the user guess of suit and rank and assigns 
the card number

if(guessSuit == 'C')
    {
        if(guessRank == 2)
        {
            theCard = 1;
        }
        else if(guessRank == 3)
        {
            theCard = 2;
        }
        else if(guessRank == 4)
        {
            theCard = 3;
        }
        else if(guessRank == 5)
        {
            theCard = 4;
        }

A first approach might be to use a struct to order all the cards in the deck

struct Card
{
   int value;
   char suit;
   int place; // 0..51 //
} ;

Then could use the 'place value' to compare cards ...

We havent gotten to that part yet. And the instructor suggested the if else statement. However, I felt that it is inefficient to do an if else statement for 4 different suits and the same if else stament inside each suit.

Has your teacher covered the switch statement?
This is an excelent opportunity to use it, if it is allowed.
Here is a link that tells about how it works: Click Here

It is quite possible that the teacher wanted you to do this in a less efficient way, just to teach you how the "if .. else .. if" thing works. The very next topic might be how to make it work better by using a switch statement.

I'm not entirely sure I'm understanding what you're trying to do, but my suggestion is that instead of trying to separate into suits and then rank, try setting ranges, where if there are 52 cars, 0-12 is spade, 13-25 is club... So on, and then modulo the number to find out what rank within that "suit range" it is.

For example. Let's say the number within the program for the card to be guessed is 18. Program determines it is within 13-25 so it must be a club, then 18 modulo 13(for 13 cards in each suit) is 5, and since modulo is going from 0-12, you add one and determine that the card is a 6 of clubs.

And then if x modulo 13 == 0, the card is an ace, == 12 the card is a king, ==11 the card is a queen and == 10 the card is a jack.

An other very simple and fast way to handle your card closeness problem could be to do a TABLE LOOKUP ...

//card_look_up.cpp //
 // includes etc...
// ...
const string CARDS[] = { "A1", "A2", "B1", "B2" };
const int NUM_CARDS = sizeof CARDS / sizeof CARDS[0];

// do a liner search ...
// or could do fast binary search ... if 'in order' //
int getPosIn( const string ary[], int size, const string& x )
{
// return index+1 if found
// return 0 otherwise
}

// presumption here is that BOTH x and y ARE IN ary of strings
int howClose( const string ary[], int size, const string& x, const string& y )
{
    int posX = getPosIn( ary, size, x );
    int posY = getPosIn( ary, size, y );
    return posY-posX;.
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.