program crashes on exit

This is a discussion on program crashes on exit within the C++ Programming forums, part of the General Programming Boards category; hello i have a problem with my blackjack game it was working befor i added a few new functions that ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    118

    program crashes on exit

    hello i have a problem with my blackjack game it was working befor i added a few new functions that allow a split to happen.

    the problem is the only thing ive changed is that it now can check if a split can happen(badly at that) the problem seems to be an access to far into an array error?
    heres some code that might be causing the problem
    Code:
    while((a.stick==false)&&(a.bust==false))
    	{
    		if ((checksplit(a.myHand))==true)
    	{
    		cout<<"you can split. would you like to?y/n\n";
    		char tempa;
    		cin>>tempa;
    		if (tempa=='y')
    		{ *gamestate=splitstate;
    		}
    		//system("pause");
    	}
    		char answer;
    		cout<<"would you like to 's'tick or 't'wist\n";
    		cin>>answer;
    		if (answer=='s')
    		{
    			a.stick=true;
    			answer='o';
    		}
    		if (answer=='t')
    		{
    			system("cls");
    			a.twist(ndeck.drawCard());
    			a.handValue();
    			for (int j=0;j<b.cardcount;j++)
    			{
    				cout<<b.myHand[j].value<<b.myHand[j].suit<<" ";
    			}
    			cout<<endl;			
    			for(int i=0;i<a.cardcount;i++)
    			{
    			cout<<a.myHand[i].value<<a.myHand[i].suit<<" ";
    			}
    			cout<<endl;
    			if(a.value>21)
    			{
    				a.bust=true;
    			}
    			answer='o';
    		}
    	}
    		if((a.stick==true)&&(a.bust==false))
    		{
    			*gamestate=dealerturn;
    			return;
    		}
    		if (a.bust==true)
    		{
    			Sleep(1000);
    			*gamestate=whowon;
    			return;
    		}
    		
    
    }
    thats one function here is the other it could be from
    Code:
    bool checksplit(card thand[])
    {
    	for (int i=0;i<5;i++)
    	{
    		for (int j=0;j<5;j++)
    		{
    			if (j!=i)
    			{
    				if ((thand[i].value)==(thand[j].value)&&(thand[i].value)>0)
    				{
    					return true;
    				}
    			}
    		}
    	}
    	return false;
    }
    the array only has 5 in it and cant have any more or less than that

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    could someone please tell me how to add a zip file to this post so you can see my source files better?
    ive tried the manage attachments but it does work for me?

    ok im sorry to of wasted your time i found my problem the constructor for my player class had this loop for (int i=0; i<10;i++) the trouble was is there only an array of 5.... so easy to overlook
    Last edited by thestien; 05-09-2008 at 07:15 PM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > loop for (int i=0; i<10;i++) the trouble was is there only an array of 5
    Having
    const int deckSize = 52;
    const int handSize = 5;

    Then later say
    for ( i = 0 ; i < handSize ; i++ )

    Will help solve a lot of the array overstep problems.

    But this is C++, so consider using std::vector instead which will at least trap if you do something wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    Quote Originally Posted by Salem View Post
    But this is C++, so consider using std::vector instead which will at least trap if you do something wrong.
    But only if you use .at(). Good to remember. Using the index operator for accessing items in a vector out-of-bounds is undefined behaviour.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    thanks for the help and advice i really appreciate it.
    i do plan to use vectors soon but it means more code(i think) and i wanted to get the program running smoothly first so that when i do change it to vector's
    (which i have no experience with yet)
    it will be easier to learn how to use them as i would know that the code did work
    in the first place.
    but here is my agenda for this game so far.
    1.get it working with all the common blackjack (21) features.
    2.convert from a consol app to a windows app using the sdl librarys
    3.change the arrays to vectors
    4.rename all variables and unjumble the code

    i know my varible names are quite descret and dont use a proper naming convention either i was just curious as to how messy my code looks to you guys and how i could clean the layout up?
    i mean how do you nest for loops and if statments?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    Nah, not really. Vectors can be used similarly to arrays and far, far easier. Try a little test project to get the hang of it.
    All you really need to know is it's defined as:
    std::vector<your_type> name;
    And to add something to the end do:
    name.push_back();
    And to access something in the vector, do:
    name.at(index);
    This will throw an exception if you try to access out-of-bounds.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    oh that sounds a lot simpler than i imagined but i think i will make a test project just to get used to them. it would make my program have so less functions if i did use vecktors because i could have a vecktor of hands for when i split instead of haveing
    Code:
    class player{
    card myhand[5]; // holds my non split hand
    card sparehand[5];  // holds my split hand if i get a chance to split
    };
    but from what i gather i could use this instead? probly different than it should be declared but to the same end as this
    Code:
    vector <card> hands;

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    That's how it's done.
    You can remove items using .erase() and .pop_back() if you don't need them anymore.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > i do plan to use vectors soon but it means more code(i think) and i wanted to get the program running smoothly first so that when i do change it to vector's
    Start with vectors for safety, convenience and comfort.

    Then only switch to arrays if you REALLY need the speed, though to be honest I don't think there is an awful lot in it with a good compiler.

    Days spent looking for a bug which will only save you a few microseconds of machine time is not an ideal payoff.

    > but here is my agenda for this game so far.
    Whereas I would tackle them in exactly the reverse order!
    2 will be a hell of a lot easier if 3 and 4 are done first.

    Style wise, I would switch from using hard tabs to 4 spaces. As you may have noticed, this forum takes a tab rather too keenly, and before you know it the code is off to the right. If you ever mix spaces and tabs, it can rapidly look a mess everywhere except your editor (fine for you maybe, but no-one else will want to read it).

    Spaces on the other hand are identical and universal. Only the most brain-damaged tools fail to deal with spaces properly. Any decent IDE will auto-indent with the correct number of spaces in place of the correct number of tabs, if configured to do so.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    I think tabs vs spaces hardly matters.
    Yes, they are unfortunately a little wider on the boards, but I hardly think that qualifies for a reason to dump your current style.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    oh super ill take all your advice into account and see how i do im gonna take a detour(testvector.cpp)
    then ill rename all variables to something similar to
    Code:
     int nMyInt;
    double dMyDouble;
    oh and how should i name classes and members that i make?

    and untab all the code and use spaces instead
    then im gonna recode most of my program keeping the parts which i can but using less functions and more vector's (currently over 25 functions for a simple blackjack game lol)
    and now i wonder why i didnt learn to use vectors in the first place i kinda been avoiding them as well as avoiding pointers .
    i will search the web now and look into how to use vectors and ill stick it until i understand them alot better

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    Quote Originally Posted by thestien View Post
    and untab all the code and use spaces instead
    No need to, really. If you like tabs, then there's no reason to undo them just because of the board.
    It's a matter of style and neither is "right," it's your own preference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    well if im honest i dont tab them over when i type and press enter after some things it just auto tabs it over so i think its been lazyness on my part but if 4 spaces looks neater then ill try to use that because you guys help me so much i dont want to make it a chore to be able to read my code

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    It's no chore, really. I always use tabs myself.
    Visual Studio and other IDEs auto-indent when typing code (which is nice!).
    I'm not sure how to change settings to make it use spaces, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    The function 'push_back()' does not guarantee that all its elements are put close to each other in the memory. With a few ints or doubles it doesn't matter, but consider the following pseudo-code:

    Code:
    double aDouble;
    std::vector<double aVector;
    std::vector<std::vector<double> > aTable;
    for all i { aVector.push_back( aDouble) };
    for all j { aTable.push_back( aVector)};
    This is really inefficient for a large spectrum of i,j . Instead, consider the following:

    Code:
    double aDouble;
    std::vector<double aVector( its_size);
    std::vector<std::vector<double> > aTable ( its_size);
    for all i { aVector.at( i) = aDouble };
    for all j { aTable.at( i) = aVector};
    Visual Studio and other IDEs auto-indent when typing code
    I have never ever seen a more horrible editor than the one in Visual Studio. Try emacs instead....

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program crashes + fscanf() question
    By happyclown in forum C Programming
    Replies: 27
    Last Post: 01-16-2009, 02:51 PM
  2. Utter newb?: Program crashes after input
    By deductible in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2008, 09:27 PM
  3. Calling delete Crashes my Program
    By thetinman in forum C++ Programming
    Replies: 19
    Last Post: 10-13-2007, 03:07 AM
  4. Program Terminating With Error On Exit
    By chriscolden in forum C Programming
    Replies: 19
    Last Post: 01-14-2006, 03:40 AM
  5. My program crashes with this code
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-27-2002, 11:28 PM

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