Thread: Agh, what's wrong with this function?

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

    Agh, what's wrong with this function?

    Can anyone spot anything glaringly wrong with this function? It's supposed to change one of the bit flags in a int with a pass-by referance. If it matters, the int in question is a dynamically allocated pointer within a class.

    For some reason it isn't changing the target. I could go nuts here. If this isn't enough info I could post the whole program, but I don't THINK that should be necessary.

    Code:
    void body::flagset( int &target, const int bit, const char*op_type )
    
    {
    
    	if (op_type == "lower")
    
    		target = target & (int)(!(pow(2, bit - 1)));
    	
    	else if (op_type == "raise")
    
    		target = target | (int)(pow(2, bit - 1));
    	
    }

  2. #2
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    You can't compare c-style strings like that. This should work, assuming the the way you change the bit flags works.
    Code:
    void body::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));
    	
    }
    EDIT: You should #include <cstring> if you haven't done so already and using namespace std.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM