Hi everyone, I am having problems with my c++ code for about 2 - 3 hours now and decided I need help. I am a student at a college and I am trying to code this for my assignment.

cpp file:

#include "bridgehand.h"


BridgeHand::BridgeHand()
{
  for (int x = 0; x < MAX_LENGTH; x++)
  {
    clubs[x] = false;
    diamonds[x] = false;
    hearts[x] = false;
    spades[x] = false;
  }
  points = 0;
}

void BridgeHand::readCards()
{
  cout << "Please enter in " << MAX_LENGTH << " bridge cards\n";
  string temp;
  for (int x = 0; x < MAX_LENGTH; x ++)
  {
    cin >> temp;
    setCard(temp);
  }
  calcPoints();
}
  
void BridgeHand::setCard(string card)
{
  int rank = getRank(card);
  char suit = ' ';
  string temp = card.substr(1,1);
  if (temp == "C")
    suit = 'C';
  else if (temp == "D")
    suit = 'D';
  else if (temp == "H")
    suit = 'H';
  else if (temp == "S")
    suit = 'S';
  if (validCard(rank, suit))
  {
    switch(suit)
    { case 'C': clubs[rank] = true; break;
      case 'D': diamonds[rank] = true; break;
      case 'H': hearts[rank] = true; break;
      case 'S': spades[rank] = true; break;
    }
  }
}

void BridgeHand::calcPoints()
{
     points = points + calcSuit( clubs );
     points = points + calcSuit( diamonds );
     points = points + calcSuit( hearts );
     points = points + calcSuit( spades );
}

int BridgeHand::calcSuit( bool arrayOfCards [] )
{
    int tempCounter = 0;
    int tempPoints = 0;
    for( int i = 0 ; i < MAX_LENGTH ; i++ )
    {
         if( arrayOfCards[i] )
         {
             tempCounter  = tempCounter + 1;
             if( i == 0 )
             {
                 tempPoints = tempPoints + ACE_VAL;
             }
             if( i == 1 )
             {
                 tempPoints = tempPoints + KING_VAL;
             }
             if( i == 2 )
             {
                 tempPoints = tempPoints + QUEEN_VAL;
             }
             if( i == 3 )
             {
                 tempPoints = tempPoints + JACK_VAL;
             }
         }// end if
    } // end for loop
    if( tempCounter == 0 )
    {
        tempPoints = tempPoints + VOID_VAL;
    }
    else if( tempCounter == 1 )
    {
        tempPoints = tempPoints + SINGLETON_VAL;
    }
    else if( tempCounter == 2 )
    {
        tempPoints = tempPoints + DOUBLETON_VAL;
    }
    return tempPoints;
}

bool BridgeHand::validCard( int, char )
{
     return true;
}

int BridgeHand::getRank( string a )
{
    string temp1;
    int temp2;
    string temp3;
    temp1 = a.substr(0,1);
    if ( temp1 == "A" )
    {
         temp3 = "0";
    }
    if( temp1 == "K" )
    {
        temp3 = "1";
    }
    if( temp1 == "Q" )
    {
        temp3 = "2";
    }
    if( temp1 == "J" )
    {
        temp3 = "3";
    }
    if ( temp1 == "T" )
    {
         temp3 = "4";
    }
    if( temp1 == "9" )
    {
        temp3 = "5";
    }
    if( temp1 == "8" )
    {
        temp3 = "6";
    }
    if( temp1 == "7" )
    {
        temp3 = "7";
    }
    if( temp1 == "6" )
    {
        temp3 = "8";
    }
    if( temp1 == "5" )
    {
        temp3 = "9";
    }
    if( temp1 == "4" )
    {
        temp3 = "10";
    }
    if( temp1 == "3" )
    {
        temp3 = "11";
    }
    if( temp1 == "2" )
    {
        temp3 = "12";
    }
    temp2 = atoi(temp3.c_str());
    return temp2;
}

void BridgeHand::print()
{
     cout << "CLUBS: ";
     printSuit( clubs );
     cout << endl << "DIAMONDS: ";
     printSuit( diamonds );
     cout << endl << "HEARTS: ";
     //printSuit( hearts );
     cout << endl << "SPADES: ";
     //printSuit( spades );
     cout << endl << "TOTAL POINTS: " << points << endl;
     cin.get();
}

void BridgeHand::printSuit( bool arrayOfCards [] )
{
     for( int j = 0 ; j < MAX_LENGTH ; j++ )
     {
      if( arrayOfCards[j] )
      {
          if( j == 0 )
          {
              cout << "A" << " ";
          }
          if( j == 1 )
          {
              cout << "K" << " ";
          }
          if( j == 2 )
          {
              cout << "Q" << " ";
          }
          if( j == 3 )
          {
              cout << "J" << " ";
          }
          if( j == 4 )
          {
              cout << "10" << " ";
          }
          if( j == 5 )
          {
              cout << "9" << " ";
          }
          if( j == 6 )
          {
              cout << "8" << " ";
          }
          if( j == 7 )
          {
              cout << "7" << " ";
          }
          if( j == 8 )
          {
              cout << "6" << " ";
          }
          if( j == 9 )
          {
              cout << "5" << " ";
          }
          if( j == 10 )
          {
              cout << "4" << " ";
          }
          if( j == 11 )
          {
              cout << "3" << " ";
          }
          if( j == 12 )
          {
              cout << "2";
          }
      } // end if
     } // end loop
     cin.get();
}

.h file

#include <iostream>
#include <string>
 
using namespace std;
 
const int MAX_LENGTH = 13;
const int ACE_VAL = 4;
const int KING_VAL = 3;
const int QUEEN_VAL = 2;
const int JACK_VAL = 1;
const int VOID_VAL = 3;
const int SINGLETON_VAL = 2;
const int DOUBLETON_VAL = 1;
 
const int ACE = 0;
const int KING = 1;
const int QUEEN = 2;
const int JACK = 3;
const int TEN = 4;
const int NINE = 5;
const int EIGHT = 6;
const int SEVEN = 7;
const int SIX = 8;
const int FIVE = 9;
const int FOUR = 10;
const int THREE = 11;
const int TWO = 12;
 
class BridgeHand
{
  public:
    BridgeHand();
    void readCards();
    void setCard(string);
    void calcPoints();
    int calcSuit(bool []);
    bool validCard(int, char);
    int getRank(string);
    void print();
    void printSuit(bool []);
 
  private:
    bool clubs[MAX_LENGTH];
    bool diamonds[MAX_LENGTH];
    bool hearts[MAX_LENGTH];
    bool spades[MAX_LENGTH];
    int points;
};

This is a simple program in which it calculates a hand in the game bridge. I got almost all of it down but I am having problems with the print() function. As soon as I call the printSuit the second time, it stops there and ignores the rest of the couts in the print() function. I am not getting any compiler errors so yeah, help would really be appreciated! Thanks in advance.

If you have tried switching on the compiler warnings and still don't get any help, you can try running it using some memory debugger like purify/valgrind. They can help in many cases.

There certainly isn't enough information in your description to even make an educated guess.
Maybe you need to use a debugger to follow the program through the printing section.

Also, if you'd consider using arrays, you can remove all those if statements and make your code smaller and a little more understandable.

I am still new to c++ and I am trying to make this program with the ideas that I think or thought were efficient. That's my main problem, I can't figure it out since there isn't enough info to begin with. Normally, I'll just put couts everywhere and detect what's happening, but now the cout is the problem. I'll try the debugger feature that both of you are talking about and see how it goes.

Also, awanishg, the compiler warning you're talking about are the pragma's right?

If you include the main block, or an example of how you're trying to use the program, that would definitely help. Personally, I'm a little stuck on your usage of cin.get() after every printSuit() command -- is it possible you're simply not hitting a key after you execute it?

Also, compiler warnings aren't pragmas, although you can disable warnings through pragma commands. You can set up the warning level for compilation through compiler-dependent options (they'd be in the project settings for Visual C++). I think awanishg was assuming that you had turned all compiler warnings off, which isn't the case by default. In any event, I don't see any obvious things the compiler should complain about. Again, I think your usage is ultimately what is causing problems here.

See if you can provide the main block as well as the inputs you are providing to the program.

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.