How can I access a struct (from a header file)?

This is a discussion on How can I access a struct (from a header file)? within the C++ Programming forums, part of the General Programming Boards category; Ok, I am loving C++ so far. I am creating a card program. Here is a snippet of my header ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    7

    How can I access a struct (from a header file)?

    Ok, I am loving C++ so far. I am creating a card program. Here is a snippet of my header file.
    Code:
    struct card
    {int value; char suit;};
    card c2={2,'c'}; card d2={2,'d'}; card s2={2,'s'}; card h2={2,'h'};
    That is the line for 2 of clubs, diamonds, spades and hearts. I declared all 52 cards. =)

    Now in my main program, I can not figure out how to declare a new (struct?) hand that has 2 cards in it.

    What do I cin>>? an int? char[2]? string? struct?

    Something like: If the user types c2 for their first card, then use the struct I called c2. (then I can write the following

    Code:
    if (firstcard.value==secondcard.value)
    {cout<<"That is a pair of "<<firstcard.value<<"\'s";}
    else
    {cout<<"That is not a pair.";}
    So how do I reference a struct (from a header file) based on user input?

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Code:
    class Card
    {
          int value;
          char suit;
    public:
          Card(char ch, int val)
          {
                assert(ch == 'd' || ch == 'c' || ch == 's' ||ch == 'h');
                value =  val;
                suit = ch;
          }
    };
    
    //In main()
    
    Card s2('s', 2);
    
    //Then you access value and suit like this
    s2.suit
    s2.value;
    
    //Now if user typed c2 you generate a c2
    
    string str;
    cin >> str;
    Card userSelectedCard;
    if(str == "c2")
    {
          userSelectedCard.suite = 'c';
          userSelectedCard.value = 2;
    
    }
    There is also another soulution. Make one class for each suit.

    Code:
    class Cardc
    {
    public:
          const char suit;
          int value;
          Cardc(int val)
          {
                suit = 'c';
                value = val;
          }
          Cardc()
          {
                suit = c;
          }
    }c_cards[13]; //Each array element is one card
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    If you want to check if it's a pair of something, try this:
    Code:
    #include <iostream>
    
    int getfirst(char name,char* output){
    	if(name=='c'){
    		strcpy(output,"clubs");
    	}
    	else if(name=='d'){
    		strcpy(output,"diamonds");
    	}
    	else if(name=='h'){
    		strcpy(output,"hearts");
    	}
    	else if(name=='s'){
    		strcpy(output,"spades");
    	}
    	return 0;
    }
    int getsecond(char name,char* output){
    	if(name=='a'){
    		strcpy(output,"ace");
    	}
    	else if(name=='k'){
    		strcpy(output,"bla");
    	}
    	else if(name=='q'){
    		strcpy(output,"bla");
    	}
    	else if(name=='s'){
    		strcpy(output,"bla");
    	}
    	else{
    		char buffer[2];
    		buffer[0]=name;
    		buffer[1]='\0';
    		strcpy(output,buffer);
    	}
    	return 0;
    }
    int main(){
    	char first[3],second[3],buffer[256],buffer2[256];
    	std::cin.getline((char*)first,3);
    	std::cin.getline((char*)second,3);
    	if(first[0]==second[0] && first[1]==second[1]){
    		getsecond(first[1],buffer2);getfirst(first[0],buffer);
    		std::cout<<"Identical cards: "<<buffer2<<" of "<<buffer<<std::endl;
    	}
    	else if(first[0]==second[0]){
    		getfirst(first[0],buffer);
    		std::cout<<"A pair of "<<buffer<<std::endl;
    	}
    	else if(first[1]==second[1]){
    		getsecond(second[1],buffer);
    		std::cout<<"A pair of "<<buffer<<"s\n";
    	}
    	else{
    		std::cout<<"These cards don't have anything in common\n";
    	}
    	system("PAUSE");
    }
    Probably it doesn't help but I posted it anyway.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    7
    I am soooo happy. I think I nailed this. I am sure I can change some of the ints to chars or otherwise make it more efficient, however, I want to test the program first

    How can I change the hand? Like with my input? Note: This program will only work if the hand is sorted, highest value on the left.

    Anyhow I want to post my first ever program and elicit your help... How do I change this line of code based on user input?

    Code:
    first=dA;second=dK,third=sK;fourth=c3;fifth=d2;
    My whole program follows:

    Code:
    #include <iostream>
    using namespace std;    
    
    struct card
    {int value; char suit;};
    
    card c2={2,'c'}; card d2={2,'d'}; card s2={2,'s'}; card h2={2,'h'};
    card c3={3,'c'}; card d3={3,'d'}; card s3={3,'s'}; card h3={3,'h'};
    card c4={4,'c'}; card d4={4,'d'}; card s4={4,'s'}; card h4={4,'h'};
    card c5={5,'c'}; card d5={5,'d'}; card s5={5,'s'}; card h5={5,'h'};
    card c6={6,'c'}; card d6={6,'d'}; card s6={6,'s'}; card h6={6,'h'};
    card c7={7,'c'}; card d7={7,'d'}; card s7={7,'s'}; card h7={7,'h'};
    card c8={8,'c'}; card d8={8,'d'}; card s8={8,'s'}; card h8={8,'h'};
    card c9={9,'c'}; card d9={9,'d'}; card s9={9,'s'}; card h9={9,'h'};
    card cT={10,'c'}; card dT={10,'d'}; card sT={10,'s'}; card hT={10,'h'};
    card cJ={11,'c'}; card dJ={11,'d'}; card sJ={11,'s'}; card hJ={11,'h'};
    card cQ={12,'c'}; card dQ={12,'d'}; card sQ={12,'s'}; card hQ={12,'h'};
    card cK={13,'c'}; card dK={13,'d'}; card sK={13,'s'}; card hK={13,'h'};
    card cA={14,'c'}; card dA={14,'d'}; card sA={14,'s'}; card hA={14,'h'};
    
    int checksuited(card first, card second, card third, card fourth, card fifth);
    int checkforaceishigh(card first, card second, card third, card fourth, card fifth);
    int checkconsecutive(card first, card second, card third, card fourth, card fifth);
    int checkfor4k(card first, card second, card third, card fourth, card fifth);
    int firstsort(int suited,int consecutive,int aceishigh);
    int prelimcheck(card first,card second,card third,card fourth,card fifth);
    int suited,consecutive,aceishigh,handvalue;
    int prelimcheck2(card first, card second, card third, card fourth, card fifth);
    int checkfor3k(card first, card second, card third, card fourth, card fifth);
    int checkforpair45(card fourth, card fifth);
    int checkforpair12(card first, card second);
    int secondsort(int threeofakind, int pair45, int pair12);
    int threeofakind, firstpairno3, pair45, pair12;
    int prelimcheck4(card first, card second, card third, card fourth, card fifth);
    int checkforpairs123(card first, card second, card third);
    int checkforpairs345(card third, card fourth, card fifth);
    int thirdsort(int earlypairno3, int latepairno3);
    int earlypairno3,latepairno3;
    int result(int handvalue);
    
    card first,second,third,fourth,fifth;
    
    int main()
    {
        first=dA;second=dK,third=sK;fourth=c3;fifth=d2;
        
            cin.get();
        handvalue=prelimcheck(first,second,third,fourth,fifth);//Checking for Royal flush, 
             if (handvalue!=0)                                 //Straight Flush or Straight
             {result(handvalue);}    
        handvalue=checkfor4k(first, second, third, fourth, fifth);//Checking for 4 of a kind
             if (handvalue!=0)
             {result(handvalue);} 
        handvalue=prelimcheck2(first, second, third, fourth, fifth);//Checking for Full House
             if (handvalue!=0)                            //or 3 of a kind
             {result(handvalue);} 
        handvalue=prelimcheck4(first, second, third, fourth, fifth);
        result(handvalue);
        cout<<"You should not see this line on screen";     
        return 0;
    }   
    
    /* Below this line, is where we check for Royal Flush, Flush or Straight*/
    int prelimcheck(card first,card second,card third,card fourth,card fifth)    
        {    
        suited=checksuited(first, second, third, fourth, fifth);
        aceishigh=checkforaceishigh(first, second, third, fourth,fifth);
        consecutive=checkconsecutive(first, second, third, fourth, fifth);
        if (aceishigh==0)
        {consecutive=1;}
        handvalue=firstsort(suited,consecutive,aceishigh);
        return handvalue;
        }
    int checksuited(card first, card second, card third, card fourth, card fifth)
        {   
        if (first.suit==second.suit&&second.suit==third.suit&&third.suit==fourth.suit&&fourth.suit==fifth.suit)
        {suited=1;}
        else
        {suited=0;}
        return suited;
        }
    int checkforaceishigh(card first, card second, card third, card fourth, card fifth)
        {
        if (first.value==14&&second.value==5&&third.value==4&&fourth.value==3&&fifth.value==2)
        {aceishigh=0;}
        else
        {aceishigh=1;}
        return aceishigh;
        }
    int checkconsecutive(card first, card second, card third, card fourth, card fifth)
        {
        if (first.value==(second.value+1)&&second.value==(third.value+1)&&third.value==(fourth.value+1)&&fourth.value==(fifth.value+1))
        {consecutive=1;}
        else
        {consecutive=0;}
        return consecutive;
        }
    int firstsort(int suited,int consecutive,int aceishigh)
        {   
            if (suited==1&&consecutive==1&&aceishigh==1)
               {   handvalue=1;}
            if (suited==1&&consecutive==1&&aceishigh==0)
               {   handvalue=2;}
            if (suited==1&&consecutive==0)
               {   handvalue=5;}
            if (suited==0&&consecutive==1)
               {   handvalue=6;}
            else
               {   handvalue=0;}
            return handvalue; 
        }
    /*Above this line, is where we check for Royal Flush, Flush or Straight*/
    /*Below this line, is where we check for Four of a Kind*/
    int checkfor4k(card first, card second, card third, card fourth, card fifth)
        {   
        if((first.value==second.value&&second.value==third.value&&third.value==fourth.value)||(second.value==third.value&&third.value==fourth.value&&fourth.value==fifth.value))
             {handvalue=3;}
        else
             {handvalue=0;}
        return handvalue;
        }
    /*Above this line, is where we check for Four of a Kind*/
    /*Below this line, is where we check for Three of a kind or Full House*/
    int prelimcheck2(card first, card second, card third, card fourth, card fifth)
        {
        threeofakind=checkfor3k(first, second, third, fourth, fifth);
        pair45=0;pair12=0; //ensuring they are 0 before we start
            if (threeofakind==123)
               {pair45=checkforpair45(fourth, fifth);}
            if (threeofakind==345)
               {pair12=checkforpair12(first, second);}
               
           handvalue=secondsort(threeofakind,pair45,pair12);
            
            return handvalue;
         }
    int checkfor3k(card first, card second, card third, card fourth, card fifth)
         {
          if(first.value==second.value&&second.value==third.value)
             {threeofakind=123;}
          if(second.value==third.value&&third.value==fourth.value)
             {threeofakind=234;}
          if(third.value==fourth.value&&fourth.value==fifth.value)
             {threeofakind=345;}
          else
             {threeofakind=0;}
          return threeofakind;
          }        
    int checkforpair45(card fourth, card fifth)/*this is only called, if 1,2,3 are 3 of a kind*/
          {
          if(fourth.value==fifth.value)
           {pair45=45;}
           else
           {pair45=0;}
           return pair45;
           }
    int checkforpair12(card first, card second)/*this is only called, if 3,4,5 are 3 of a kind*/
           {
           if(first.value==second.value)
           {pair12=12;}
           else
           {pair12=0;}
           return pair12;
           }
    int secondsort(int threeofakind, int pair45, int pair12)   
         { 
            if (threeofakind==123)
                 {
                      if (pair45==45)
                        {handvalue=4;}
                      if (pair45==0)
                        {handvalue=7;}
                 }
            if (threeofakind==345)
                 {
                      if (pair12==12)
                         {handvalue=4;}
                      if (pair12==0)
                         {handvalue=7;}
                 }
            if (threeofakind==234)
               {handvalue=7;}
            else
                {handvalue=0;}
            return handvalue;
         } 
    /*Above this line, is where we check for Three of a kind or Full House*/ 
    /*Below this line, we check for 2 pair, Jacks or Better or Bust*/ 
    int prelimcheck4(card first, card second, card third, card fourth, card fifth)
        {
        earlypairno3=checkforpairs123(first, second, third);
        latepairno3=checkforpairs345(third, fourth, fifth);
        handvalue=thirdsort(earlypairno3, latepairno3);
        return handvalue;
            
        }
    int checkforpairs123(card first, card second, card third)
        {
        if((first.value==second.value)||(second.value=third.value))
           {if (second.value>10)
               {earlypairno3=222;}
            else
               {earlypairno3=2;}
            }
        else
           {earlypairno3=0;}  
        return earlypairno3;
        }
    int checkforpairs345(card third, card fourth, card fifth)
        {
        if((third.value==fourth.value)||(fourth.value==fifth.value))
           {if (fourth.value>10)
               {latepairno3=444;}
            else
               {latepairno3=4;}
            }
        else
           {latepairno3=0;}
        return latepairno3;
        }
    
    int thirdsort(int earlypairno3, int latepairno3)
        {  
        if ((earlypairno3>0)&&(latepairno3>0))
           {handvalue=8;}
        else   
           {if ((earlypairno3>100)||(latepairno3>100))
                  {handvalue=9;}
               else
                   {handvalue=0;}}
        return handvalue;
        }
    /*Above this line we check for 2 pair, Jacks or better or Bust*/
    /*Below this line, we output the result*/
    int result(int handvalue)
    {    switch(handvalue)
           {   case 1:
                 cout<<"Royal Straight Flush";
                 break;
               case 2:
                 cout<<"Straight Flush";
                 break;
               case 3:
                 cout<<"Four of a Kind";
                 break;
               case 4:
                 cout<<"Full House";
                 break;
               case 5:
                 cout<<"Straight";
                 break;
               case 6:
                 cout<<"Flush";
                 break;
               case 7:
                 cout<<"Three of a Kind";
                 break;
               case 8:
                 cout<<"Two Pair";
                 break;
               case 9:
                 cout<<"Jacks or Better";
               break;
               case 0:
                 cout<<"Bust";
                 break;
               default:
                 cout<<"Error";
                 break;
           }  
               main();    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  4. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  5. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21