Thread: Ok, I've butchered this.

  1. #16
    Registered User
    Join Date
    Sep 2003
    Posts
    28

    Oh, one more thing

    Is there a standard clearscreen command for console apps?

  2. #17
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    This is your standard clear screen:
    Code:
    void ClearScreen()
    {
         for(int x=0 ; x<25 ; x++)
              cout << endl;               // or cout << "\n";
    }
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  3. #18
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I hate console apps so much. Even clearing the screen is dodgy. My advice? Move to Win32. It's clearly better for GUI.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #19
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    That's harsh bennyandthejets!

    I can't think of a better environment in which to learn programming than the console. It's all very well to say Win32 is better, no one will dispute you on that, but win32 is certainly not suitable for beginners.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  5. #20
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Agreed. I should have been more specific. Grins2Pain, stick with the console for now, it makes things a lot easier in the beginning. Eventually, you may want to move to Win32, for better aesthetics in your programs, etc.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #21
    Registered User
    Join Date
    Sep 2003
    Posts
    28

    Yeah, no doubt

    Actually, long term, I want to learn Win32 then MFC, then DirectX. So I'll definately be cracking the books on Win32.

    Tell me, can direct X simply be called fullscreen from a console app, I mean it DOES say WIN32 console app

    Anyhow, I got a new, REALLY odd bug. The program is now pretty much perfect(in it's own, utterly sub-pong lame way), BUT I get a HUGE error when I use menu option 3 to exit. I tried to debugger but the crash is like some really bad access violation or something MAJOR screwy and the debugger can't even stomach it.

    "main.cpp"

    Code:
    #include "myheaders.h"
    
    int main (void)
    
    {
    	int how_many;
    	cout<<"How many prisoners? ( 1 to 32 )\n";
    	cin>>how_many;
    	lchairs *program = new lchairs( how_many );
    	delete program;
    	return 0;
    
    }
    "myheaders.h"

    Code:
    #include <iostream.h>
    #include <cmath>
    #include <cstring>
    #include <stdlib.h>
    
    #include "core.h"
    "core.h"

    Code:
    #define SHOW system("CLS"); display(); cout<<"\n"; 
    
    class lchairs 
    
    {
    
    private:
    
    	unsigned int *elechairs;
    	bool active;
    	int priscount;
    	int bits[32];
    	void flagset( unsigned int &target, const int, const char *op_type );
    	bool flagcheck ( unsigned int target, const int bit );
    	void elecmain();
    	void elecmenu( int );
    	void display();
    
    public:
    
    	lchairs(int count = 8);
    	~lchairs(void) { delete elechairs; }
    
    };
    
    void lchairs::elecmain()
    
    {
    
    	
    	active = true;
    	int menuchoice;
    	while ( active == true ) 
    
    	{
    	
    		SHOW
    		cout<<"\nMenu\n\n1)Execute\n2)Replacement\n3)Exit\n\n";
    		cin>>menuchoice;
    		elecmenu(menuchoice);
    
    	}		
    		
    }
    
    lchairs::lchairs(int count)
    
    {
    		
    	(count < 1) ? (count = 1) : ((count > 32) ? (count = 32) : (NULL)) ; 
    	this->priscount = count;
    	elechairs = new unsigned int;
    	*elechairs = 0;
    	for (int i=1;i<=32;i++)
        
    		bits[i]=(int)pow(2,i - 1);
    	
    	for (int x = 1 ; x <= this->priscount ; x++ )
    
    		flagset ( *elechairs, x, "raise" );
    
    	elecmain();
    	
    }
    
    
    void lchairs::flagset( unsigned int &target, const int bit, const char*op_type )
    
    {
    
    	if (!strcmp(op_type, "lower"))
    
    		target = target & ~(int)(bits[bit]);
    	
    	else if (!strcmp(op_type, "raise"))
    
    		target = target | (int)(bits[bit]);
    	
    }
    
    
    
    bool lchairs::flagcheck ( unsigned int target, const int bit )
    
    {
    
    	if ((target & (int)(bits[bit])) >= 1)
    
    		return true;
    	
    	else
    
    		return false;
    
    }
    
    void lchairs::elecmenu ( int choice )
    
    {
    
    	switch ( choice )
    
    		{
    
    			case 1:
    
    			{
    				
    				SHOW
    				cout<<"\nWho shall die?\n\n";
    				cin>>choice;
    				if ( (choice >= 1) && (choice <= this->priscount) ) flagset( *elechairs, choice, "lower" );
    
    			}
    			
    			break;
    
    			case 2:
    
    			{
    		
    				SHOW
    				cout<<"\nWho shall be replaced?\n\n";
    				cin>>choice;
    				if ( (choice >= 1) && (choice <= this->priscount) ) flagset( *elechairs, choice, "raise" );
    			
    			}
    			
    			break;
    		
    			case 3: 
    
    			active = false;
    		
    			break;
    		
    			default:
    			break;
    
    		}
    
    }
    
    void lchairs::display()
    
    {
    			
    		for ( int x = 1 ; x <= this->priscount ; x++ )
    
    		{
    
    			if ( flagcheck(*elechairs, x) == true)
    						
    			{
    				
    				if (x < 10) cout<<"0";
    				cout<<x<<" |OO| ";
    				if (x % 8 == 0)
    
    				{
    								
    					cout<<"\n";
    					for (int z = 0 ; z <= 62 ; z++ )
    					cout<<"-";
    					cout<<"\n";
    							
    				}
    							
    			}
    				
    		else if ( flagcheck(*elechairs, x) == false)
    						
    			{
    				
    				if (x < 10) cout<<"0";
    				cout<<x<<" |XX| ";
    				if (x % 8 == 0) 
    							
    				{
    								
    					cout<<"\n";
    					for (int z = 0 ; z <= 62 ; z++ )
    					cout<<"-";
    					cout<<"\n";
    							
    				}
    							
    			}
    				
    		}
    
    }

  7. #22
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Hmmm. I've narrowed the problem down to main.cpp, where you delete the lchairs. It doesn't seem to like that. I just can't work out why. I'll work it out eventually, don't worry.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #23
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Got it! I found it while mulling over something you did. When you filled the bits array, you filled from bits[1] to bits[32]. You should have done it the way I did, filling from bits[0] to bits[31], which is the norm. What you did was muck around with some memory that wasn't yours. Declaring an array bits[32] lets you manipulate any memory from bits[0] to bits[31]. By changing bits[32], you messed around with some important system memory, or something. So, here is the fix:

    Code:
    class lchairs 
    
    {
    
    private:
    
    	unsigned int *elechairs;
    	bool active;
    	int priscount;
    	int bits[33]; //<-----------fixed this
    	void flagset( unsigned int &target, const int, const char *op_type );
    It works fine with that code. Note that you're wasting 4 bytes of memory by not using bits[0]. In the future, try to implement zero-based arrays, because that's the way it's done in C++.

    The reason, if you really want to know, is because the square brackets work by offsetting a memory address by the number specified. So, bits[0] offsets it zero, ie, no offset, bits[1] offsets it 4 bytes (the length of an int). This way, you always have to start from bits[0], and your final element is bits[size-1].
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #24
    Registered User
    Join Date
    Sep 2003
    Posts
    28

    Smile Ah, yes!

    In the future I'll remember that. I'd almost pay to know exactly what I managed to target by accident. Almost makes me want to make a program that addresses random memory locations with random values in a big loop.... almost

    *yawn* Well, I've been up all night brushing up on classes, so I'm going to take a nap. I looked ahead into the next section, overloading. It seems quite useful, but it has to be the weirdest damn thing I've seen so far. I mean, overloading the unassuming little + is a little extreme lol. But I can see how for classes, it would be VERY useful. If I understand correctly, you can do... unnatural things with classes and basic math operators. Why, I think I'll overload my + sign to append arrays together as soon as I get some sleep

  10. #25
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Your program would crash after the first iteration of the loop. When you crash because you access memory you don't own, it's not because you accessed that memory, it's because the OS STOPPED you from accessing that memory.
    The memory you're attempting to access probably doesn't contain anything, but that's beside the point as far as the OS is concerned. I'm pretty sure, anyway.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  11. #26
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Minor point in passing, you can combine these:

    elechairs = new unsigned int;
    *elechairs = 0;

    like this:

    elechairs = new unsigned int(0);

  12. #27
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Originally posted by HybridM
    Your program would crash after the first iteration of the loop. When you crash because you access memory you don't own, it's not because you accessed that memory, it's because the OS STOPPED you from accessing that memory.
    The memory you're attempting to access probably doesn't contain anything, but that's beside the point as far as the OS is concerned. I'm pretty sure, anyway.
    Did you try the code HybridM? It didn't crash until delete was called on the lchairs object. Clearly, the compiler/OS doesn't flag this particular access violation. The four bytes at bits[32] must not be special enough to flag an immediate error, but they ARE special enough to prevent deletion of the object.

    Grins2Pain, have fun with overloading, it's great.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  13. #28
    Registered User
    Join Date
    Sep 2003
    Posts
    28

    He was referring to something else

    Benny, Hybrid was talking about my joke program, the random value setter.

    Hey, I got a question for you and hybrid. I'm interested in getting started with 2D directdraw and direct sound. What should I learn from here? I assume I need to know Win32 and possibly MFC. I know directdraw hasn't been updated in a while, but I can use it still right?

    Please, point me in the right direction.

  14. #29
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Well, I haven't used DirectX, only OpenGL, so I'm not sure. Go to the game programming forum, and check the sticky at the top. It contains links to just about any game/graphics site you'll ever need. Before you do however, it may help to learn some Win32 API. You have to know how to open up a window and handle messages before you can use Windows graphics. You won't need to know MFC. Nobody does IMO.

    I think this site (theForger's) is a good place to start for Win32 API.

    Good luck!
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  15. #30
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    All new versions of DX support all previous versions, but a while back Direct3D kinda absorbed DirectDraw, I think DX7.

    I don't know if you can still use DirectDraw methods in dx9, you may have to use an older SDK.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

Popular pages Recent additions subscribe to a feed