list boxes

This is a discussion on list boxes within the Windows Programming forums, part of the Platform Specific Boards category; I cannot get anything to show up in my list boxes, and i have tried just about everything. I have ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    5

    list boxes

    I cannot get anything to show up in my list boxes, and i have tried just about everything. I have had no luck with classwizards either. I just dont understand all that goes with it. Any help would be appreciated.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Without your code I will need to rely on my psycic powers....and my chrystal ball is in the cleaners.......so how about a clip of what you are trying to do?

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    I'm trying to read his mind but it is very hard, so little to work with!
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    5
    lol, sorry about that. I fixed that problem and on to another. This time i am having trouble updating my edit boxes. I am writing BlackJack, and the value of the cards are kept in an edit box, and when you hit play again, everything else changes except the values. Im stumped. the code is long, but i will post

    Code:
    #include <afxwin.h>
    #include <strstrea.h>
    #include "resource.h"
    #include <vector>
    #include <iostream>
    #include <string>
    #include <ctime>
    
    using std::string;
    using std::vector;
    
    #define DEALER_WINS 1
    #define PLAYER_WINS 2
    
    // -----------------------------------------------------------------------------
    // -----------------------------------------------------------------------------
    
    class Card {
    public:
    	string suit, face;
    	string ToString();
    	int value;
    protected:
    private:
    };
    Card c;
    // -----------------------------------------------------------------------------
    
    static const string SUIT[4] = {"Hearts", "Diamonds", "Clubs", "Spades" };
    static const string FACE[13] = {"Ace", "Two", "Three", "Four", "Five", "Six",
    								"Seven", "Eight", "Nine", "Ten", "Jack",
    								"Queen", "King"};
    
    // -----------------------------------------------------------------------------
    
    string Card::ToString()
    {
    
    return face+" of "+suit;
    }
    
    // -----------------------------------------------------------------------------
    //------------------------------------------------------------------------------
    
    class Deck {
    public:
    	Deck();
    	~Deck();
    	void Shuffle(void);
    	Card DealCard(void);
    protected:
    private:
    	vector<Card> cards;
    	void CreateCards(void);
    	void SwapCards(int, int);
    };
    
    Deck d;
    // -----------------------------------------------------------------------------
    
    Deck::Deck()
    {
    	Shuffle();
    }
    
    // -----------------------------------------------------------------------------
    
    Deck::~Deck()
    {
    	cards.clear();
    }
    
    // -----------------------------------------------------------------------------
    
    void Deck::CreateCards(void)
    {
    	cards.clear();
    	Card c;
    
    	int s, f;
      for (s=0; s < 4; s++) {       // loop for all suits
        for (f=0; f < 13; f++) {    // loop for all faces
    			c.face = FACE[f];
    			c.suit = SUIT[s];
    			c.value = f + 1;
    			if (c.value > 10)         // make sure face cards' values = 10
    				c.value = 10;
    			cards.push_back(c);
    		}
    	}
    }
    
    // -----------------------------------------------------------------------------
    
    void Deck::Shuffle(void)
    {
    	CreateCards();
    	srand((unsigned)time(NULL));
    
    	int i, random_pos;
    
    	for (i = 0; i < 52; i++) {
    		random_pos = rand() % 52;
    		SwapCards(i, random_pos);
    	}
    }
    
    // -----------------------------------------------------------------------------
    
    void Deck::SwapCards(int pos1, int pos2)
    {
    	Card temp;
    	temp = cards[pos1];
    	cards[pos1] = cards[pos2];
    	cards[pos2] = temp;
    }
    
    // -----------------------------------------------------------------------------
    
    Card Deck::DealCard(void)
    {
    	Card rt;
    	if (!cards.empty()) {
    		rt = cards[0];
    		cards.erase(cards.begin());
    	}
    	return rt;
    }
    
    // -----------------------------------------------------------------------------
    // -----------------------------------------------------------------------------
    
    class Hand {
    public:
      void AddCard(Card);
      Card FirstCard(void);
      int Value(void);
      int value;
    protected:
    private:
      vector<Card> cards;
    };
    
    // -----------------------------------------------------------------------------
    
    void Hand::AddCard(Card c)
    {
      cards.push_back(c);
      value += c.value;
    }
    
    // -----------------------------------------------------------------------------
    
    int Hand::Value(void)
    {
      int sum = 0;
      for (int i=0; i < cards.size(); i++)
        sum += cards.at(i).value;
      return sum;
    }
    
    // -----------------------------------------------------------------------------
    
    Card Hand::FirstCard(void)
    {
      // precondition is that the cards vector is not empty - which sould always be true here
    
      return cards.at(0);
    }
    
    //--------------------------------------------------------------------------------
    //--------------------------------------------------------------------------------
    
    class CMainWindow : public CDialog
    {
    public:
    	CMainWindow() : CDialog(MAIN) {};
    	afx_msg void OnAdd();
    	afx_msg void OnStay();
    	afx_msg void OnQuit();
    	BOOL CMainWindow::OnInitDialog();
    private:
    	DECLARE_MESSAGE_MAP()
    };
    
    //----------------------------------------------------------------------------------
    //----------------------------------------------------------------------------------
    
    class CStatusWindow : public CDialog
    {
    public:
    	CStatusWindow() : CDialog(STATUS) {};
    	BOOL CStatusWindow::OnInitDialog();
    	afx_msg void OnAgain();
    	afx_msg void OnQuit();
    private:
    	DECLARE_MESSAGE_MAP()
    };
    
    //----------------------------------------------------------------------------------
    //----------------------------------------------------------------------------------
    
    class InternalInfo{
    public:
    	InternalInfo();
    	~InternalInfo();
    	Hand player_hand;
    	Hand dealer_hand;
    	int winner;
    	int dealer_wins;
    	int player_wins;
    	CListBox* pData;	//data pointer for player
    	CListBox* pData2;	//data pointer for dealer
    	CListBox* pAdd;		//data pointer for player added cards
    	CEdit* pTotal1;		//data pointer for player score
    	CEdit* pTotal2;		//data pointer for dealer score
    	int sw;
    	CStatusWindow second;
    	CListBox* pAddDealer;
    private:
    };
    
    InternalInfo data;
    
    //----------------------------------------------------------------------------------
    //----------------------------------------------------------------------------------
    
    BOOL CMainWindow::OnInitDialog()
    {  
      // deal the intial two cards to player and dealer
    	CDialog::OnInitDialog();
       
    	data.pData= (CListBox*) GetDlgItem(IDC_LIST1);
    	
    //card 1 for player
    	c = d.DealCard();
    	c.ToString();
    	data.player_hand.AddCard(c);
    	data.pData->InsertString(-1, c.ToString().c_str());
    
    	data.pTotal1=(CEdit*) GetDlgItem(IDC_TOTAL1);
    	
    	char buffer6[30];
    	ostrstream str6(buffer6, 30);
    	str6.seekp(0);
    	str6<<data.player_hand.Value()<<ends;
    	data.pTotal1->SetWindowText(buffer6);
    //end card 1 for player
    	 
    //card 2 for player
    	c = d.DealCard();
    	c.ToString();
    	data.player_hand.AddCard(c);
    	data.pData->InsertString(-1, c.ToString().c_str());
    	
    	char buffer7[30];
    	ostrstream str7(buffer7, 30);
    	str7.seekp(0);
    	str7<<data.player_hand.Value()<<ends;
    	data.pTotal1->SetWindowText(buffer7);
    //end of card 2 for player
    
    	data.pData2= (CListBox*) GetDlgItem(IDC_LIST2);
    
    //card 1 for dealer
    	c = d.DealCard();
    	c.ToString();
    	data.dealer_hand.AddCard(c);
    	data.pData2->InsertString(-1, c.ToString().c_str());
    
    	data.pTotal2=(CEdit*) GetDlgItem(IDC_TOTAL2);
    	
    	char buffer8[30];
    	ostrstream str8(buffer8, 30);
    	str8.seekp(0);
    	str8<<data.dealer_hand.Value()<<ends;
    	data.pTotal2->SetWindowText(buffer8);
    //end card 1 for dealer
    
    //card 2 for dealer
      c = d.DealCard();
      c.ToString();
      data.dealer_hand.AddCard(c);
    //end card 2 for dealer
    	
    return TRUE;
    }
    
    //--------------------------------------------------------------------------------------
    
    afx_msg void CMainWindow::OnAdd()
    {	
    	c = d.DealCard();
    	c.ToString();
        data.player_hand.AddCard(c);
    	data.pAdd=(CListBox*) GetDlgItem(IDC_LIST1);
    	data.pAdd->InsertString(-1, c.ToString().c_str());
    	
    	data.pTotal1=(CEdit*) GetDlgItem(IDC_TOTAL1);
    
    	char buffer3[30];
    	ostrstream str3(buffer3, 30);
    	str3.seekp(0);
    	str3<<data.player_hand.Value()<<ends;
    	data.pTotal1->SetWindowText(buffer3);
    
    	
    	if (data.player_hand.Value() > 21)
    	{ data.winner = DEALER_WINS;
    	MessageBox("D'OH!", "Sorry", MB_OK);}
    }
    
    //------------------------------------------------------------------------------------
    
    afx_msg void CMainWindow::OnStay()
    {
    	data.sw=data.second.DoModal();
    
    	if(data.sw==IDC_QUIT){
    		MessageBox("thanks", "thanks", MB_OK);
    		SendMessage(WM_CLOSE);}
    
    	
    	data.pAddDealer=(CListBox*) GetDlgItem(IDC_LIST2);
    	
    	data.winner=PLAYER_WINS;
    	if (data.player_hand.Value() > 21)
        data.winner = DEALER_WINS;
      else {
        // make the dealer try to win until he "busts" or matches/exceeds player's hand
        while (data.dealer_hand.Value() < data.player_hand.Value()) {
          c = d.DealCard();
          data.dealer_hand.AddCard(c);
    
    	data.pAddDealer->InsertString(-1, c.ToString().c_str());
    	CEdit* pTotal2;
    	pTotal2=(CEdit*) GetDlgItem(IDC_TOTAL2);
    	char buffer2[30];
    	ostrstream str2(buffer2, 30);
    
    	str2.seekp(0);
    	str2<<data.dealer_hand.Value()<<ends;
    	pTotal2->SetWindowText(buffer2);
        }
        // if the dealer didn't "bust", then he must have won (b/c loop only ends on win or bust)
        if (data.dealer_hand.Value() <= 21)
          data.winner = DEALER_WINS;
      }	
    }
    
    //------------------------------------------------------------------------------------
    
    afx_msg void CMainWindow::OnQuit()
    {
    	MessageBox("Thanks for playing blackjack", "GAME OVER", MB_OK);
    	SendMessage(WM_CLOSE);
    }
    
    BEGIN_MESSAGE_MAP(CMainWindow, CDialog)
    	ON_COMMAND(IDC_HIT, OnAdd)
    	ON_COMMAND(IDC_STAY, OnStay)
    	ON_COMMAND(IDC_QUIT, OnQuit)
    END_MESSAGE_MAP()
    
    //-------------------------------------------------------------------------------------
    //-------------------------------------------------------------------------------------
    
    BOOL CStatusWindow::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    
    return TRUE;
    }
    
    //------------------------------------------------------------------------------------
    
    afx_msg void CStatusWindow::OnAgain()
    {
    	MessageBox("Lets play another", "New Game", MB_OK);
    	data.pData->ResetContent();
    	data.pData2->ResetContent();
    	data.pTotal1->Clear();
    	data.pTotal2->Clear();
    	SendMessage(WM_CLOSE);
    
    }
    
    //------------------------------------------------------------------------------------
    
    afx_msg void CStatusWindow::OnQuit()
    {
    	MessageBox("Thanks for playing blackjack", "GAME OVER", MB_OK);
    	SendMessage(WM_CLOSE);
    }
    
    //------------------------------------------------------------------------------------
    
    BEGIN_MESSAGE_MAP(CStatusWindow, CDialog)
    	ON_COMMAND(IDOK, OnAgain)
    	ON_COMMAND(IDC_QUIT, OnQuit)
    END_MESSAGE_MAP()
    
    //------------------------------------------------------------------------------------
    //------------------------------------------------------------------------------------
    
    class CMainApp : public CWinApp
    {
    	BOOL InitInstance(){
    	int sw=IDOK;
    	CMainWindow main;
    	while(sw==IDOK){sw=main.DoModal();}
    	return FALSE;
    	}
    };
    
    CMainApp app;
    
    //------------------------------------------------------------------------------------
    //------------------------------------------------------------------------------------
    
    InternalInfo::InternalInfo()
    {
    	dealer_wins=0;
    	player_wins=0;
    }
    
    //-----------------------------------------------------------------------------------
    
    InternalInfo::~InternalInfo()
    {
    	dealer_wins=0;
    	player_wins=0;
    }
    
    //------------------------------------------------------------------------------------
    //----------------------------------------------------------------------------------
    --

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    5
    Edit:: oops, hit the back button, sorry, oooo look, smiles in my code! /Edit
    Last edited by dpsalchl; 04-24-2002 at 12:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 02:19 PM
  3. Simple list code
    By JimpsEd in forum C Programming
    Replies: 1
    Last Post: 05-24-2006, 02:01 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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