Ok, I've butchered this.

This is a discussion on Ok, I've butchered this. within the C++ Programming forums, part of the General Programming Boards category; This was a silly little program I made to get a handle on classes, but I think I've really butchered ...

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

    Ok, I've butchered this.

    This was a silly little program I made to get a handle on classes, but I think I've really butchered some part of this, can anyone spot my mistake?

    *by the by, it's not entirely finished, but certain things SHOULD work already* I'm not desiring nor asking anyone to finish it, but I've just spent like 4 hours trying to figure out why it's not working. Feel free to call me a fool, I probably did something ridiculously stupid

    "main.cpp" FILE:
    Code:
    #include "myheaders.h"
    
    int main (void)
    
    {
    
    	lchairs *program = new lchairs;
    	delete program;
    	return 0;
    
    }
    "core.h" FILE:
    Code:
    class lchairs 
    
    {
    
    private:
    
    	int *elechairs;
    	void flagset( int &target, const int, const char *op_type );
    
    	bool flagcheck ( int target, const int bit );
    	void elecmain();
    
    public:
    
    	lchairs(void);
    	~lchairs(void) { delete elechairs; }
    
    };
    
    void lchairs::elecmain()
    
    {
    
    	char menuchoice;
    	while ( *elechairs > 0 ) 
    
    	{
    	
    		cout<<"\nMenu\n\n1)Rollcall\n2 Execute\n3Replacement\n4)Exit\n";
    		cin>>menuchoice;
    		switch ( menuchoice )
    
    		{
    
    		case '1':
    
    			{
    			
    				for ( int x = 1 ; x >=8 ; x++ )
    
    				{
    
    					if ( flagcheck(*elechairs, x) == true)
    						
    					{
    				
    						cout<<"Number "<<x<<" is alive\n\n";
    				
    					}
    				
    				}
    
    			}
    					
    		break ;
    
    		case 2:
    
    		
    		
    		case 3:
    
    		
    		
    		case 4:
    
    		
    		
    		default:
    		break;
    
    		}
    
    	}
    
    }
    
    lchairs::lchairs()
    
    {
    		
    	int *elechairs = new int;
    
    	*elechairs = 0;
    	
    	for ( int x = 1 ; x <=8 ; x++ )
    
    	flagset ( *elechairs, x, "raise" );
    
    	elecmain();
    	
    }
    
    
    void lchairs::flagset( int &target, const int bit, const char*op_type )
    
    {
    
    	if (!strcmp(op_type, "lower"))
    
    		target = target & (int)(!(pow(2, bit - 1)));
    	
    	else if (!strcmp(op_type, "raise"))
    
    		target = target | (int)(pow(2, bit - 1));
    	
    }
    
    
    
    bool lchairs::flagcheck ( int target, const int bit )
    
    {
    
    	if ((target & (int)pow(2, bit - 1)) >= 1)
    
    		return true;
    	
    	else
    
    		return false;
    
    }
    "myheaders.h" FILE:
    Code:
    #include <iostream.h>
    #include <cmath>
    #include <cstring>
    
    #include "core.h"

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    What is the error?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

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

    Well...

    It DOES compile, but it's almost like I can't effect any change in the elechairs pointer. I am almost completely sure my bit flag check works. For "lowering" a flag, I take 2 to the power of (bit - 1), reverse it with bitwise negation, making, lets say 11011111. Then that's bitwise and to lets say 11110011. The zero in the 11011111 become a little binary wrecking ball. The opposite, 00100000 with bitwise OR will pop a flag up. Of course, taken raw these will "beat dead horses" and change 0 to 0 and 1 to 1. To check and see if a bit flag is up I just take say 00100000 and bitwise & it to a value and see if it is greater than zero. I suppose I should be using unsigned ints, but I didn't explicitly set any of them knowingly to a negative number.

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I don't like this constructor:

    Code:
    lchairs::lchairs()
    
    {
    		
    	int *elechairs = new int;
    
    	*elechairs = 0;
    	
    	for ( int x = 1 ; x <=8 ; x++ )
    
    	flagset ( *elechairs, x, "raise" );
    
    	elecmain();
    	
    }
    For some reason it compiles for me, when it shouldn't. elechairs is already declared with class scope, and my compiler is letting it be declared again. Odd.

    Anyway, I see what you're trying to do. Try this out:

    Code:
    lchairs::lchairs()
    
    {
    		
    	elechairs = new int;
    	*elechairs = 0;
    	
    	for ( int x = 1 ; x <=8 ; x++ )
    
    	flagset (elechairs, x, "raise" );
    
    	elecmain();
    	
    }
    You should dynamically allocate, then assign to the pointer that already exists. When passing by reference you don't have to dereference. This should work for you.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    My mistake, you were trying to pass a pointer through.

    Code:
    lchairs::lchairs()
    
    {
    		
    	elechairs = new int;
    
    	*elechairs = 0;
    	
    	for ( int x = 1 ; x <=8 ; x++ )
    
    	flagset ( *elechairs, x, "raise" );
    
    	elecmain();
    	
    }
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

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

    Ok, I made the changes...

    Thanks for pointing that out, but it still doesn't work. I think the problem may lie in the functions I am using to actually manipulate the flags, I'll look more closely at the checking routine...

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    	for ( int x = 1 ; x >=8 ; x++ )
    	{
    		if ( flagcheck(*elechairs, x) == true)
    		{
    			cout<<"Number "<<x<<" is alive\n\n";
    		}
    	}
    Take a good look at the condition check for the for loop. It is never satisfied.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

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

    How so?

    That's a Bool function that's set to return true. What's the problem?

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    for ( int x = 1 ; x >=8 ; x++ )
    	{
    Look VERY closely at that line.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

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

    DOOOOOH!!



    Four hours, four stinking hours I overlooked that!

    Thanks Benny, your eyes are sharper than mine!

  11. #11
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Don't mention it, I love to help. The way I found out the problem involved breakpointing and line-by-line debugging. If you have a compiler like MSVC++, you'll be able to do the same thing, otherwise, I dunno. This sort of problem is most easily solved through step-by-step debugging. Anyway, good luck with the rest of the program.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

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

    Yeah, I'm finishing up now...

    Yeah, this program isn't much, it isn't even PONG, but it does feel good to be getting it finished. It's a bad habit to leave programs undone.

    EDIT: By the way, it doesn't have any memory leaks does it?

  13. #13
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    No it looks fine to me. However, there are some things that could be improved.

    I think it may be better to put the bit values in an array rather than constantly using pow() to generate them. Maths functions use a lot of CPU me thinks. Maybe do the following:

    Code:
    class lchairs 
    
    {
    
    private:
    
    	int *elechairs;
                    int bits[8];
    	void flagset( int &target, const int, const char *op_type );
    
    	bool flagcheck ( int target, const int bit );
    	void elecmain();
    
    public:
    
    	lchairs(void);
    	~lchairs(void) { delete elechairs; }
    
    };
    
    lchairs::lchairs()
    
    {
    		
    	elechairs = new int;
    
    	*elechairs = 0;
    
                    for (int i=0;i<8;i++)
                             bits[i]=pow(2,i);
    	
    	for ( int x = 1 ; x <=8 ; x++ )
    
    	flagset ( *elechairs, x, "raise" );
    
    	elecmain();
    	
    }
    You see where I'm going? So, instead of using pow constantly, just refer back to the array.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

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

    One better maybe

    Would it be better to make the array constant to boot?

    EDIT: How can I convert a number grabbed via CIN from a Char number to a INT? I.E. make '1' = 1
    Last edited by Grins2Pain; 09-28-2003 at 05:23 AM.

  15. #15
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Yes, making the array const is better, because it should never be changed. It wont however result in better performance. It's just good programming etiquette.

    For converting strings to integers, google the function atoi(). You should find it relatively simple.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

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

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