Thread: const char to int

  1. #1
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655

    const char to int

    im working on code so that you will be able to input code w/comments and this function will take them out after i compile i get a const char to int error, i think i know what this error is, im trying to convert a char into an int, i know c fairly well and this is my first jump to .cpp...i was just looking for some input on how to get this right. Thanks
    Code:
    void FilterComments (const char *in, char *out)
    {
    	const char *inPTR;
    	char *outPTR;
    
    	inPTR = in;
    	outPTR = out;
    
    	while((inPTR) != '\0') /* while inPTR != null*/
    	{							
    		if ((inPTR) == '/' || (inPTR) == '\')
    		{
    			inPTR++;
    			if ((inPTR) == '/' || (inPTR)== '*')
    			{
    				outPTR = ' ';
    				outPTR++;
    			}			
    			
    			else						
    			{
    				if(inPTR == '"')
    				{
    					do{
    						inPTR++;
    					}
    					while((inPTR) != '"');
    				}
    				
    				else
    				{
    					outPTR = inPTR;
    					inPTR++;
    					outPTR++;
    				}
    			}
    			
    		}
    		
    		else						
    		{
    			if(inPTR == '"')
    			{
    				do{
    					inPTR++;
    				}
    				while((inPTR) != '"');
    			}
    			
    			else
    			{
    				outPTR = inPTR;
    				inPTR++;
    				outPTR++;
    			}
    		}
    	
    	}
    
    }
    guns dont kill people, abortion clinics kill people.

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    What line is it on?
    I'm sure you problem can be solved with some form of casting or another.

  3. #3
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    to be honest i get a lot of errors, ecspecially w/ the lines like
    if ((inPTR) == '/' || (inPTR)== '*')
    if(inPTR == ' " ')
    the errors say something like error: no conversion from const* to int
    guns dont kill people, abortion clinics kill people.

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Perhaps you mean to dereference the pointer first?

  5. #5
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    i tried that and still keep getting the same errors
    guns dont kill people, abortion clinics kill people.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    inPTR is a const meaning you can't change it. You do that mean you add one to it to see the next char. Do this instead

    Code:
    char *inPTR=const_cast<char*>(in);
    This takes away the const-ness of in so you can modify it. This really takes away the purpose of having in as const except to assure the user that it won't change.

    Also, use for loops, and how about single quotes? You only handle double quotes....

  7. #7
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    HA HA i got it, i was dereferencing the wrong things,
    thanks for the help guys
    guns dont kill people, abortion clinics kill people.

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Speedy5
    inPTR is a const meaning you can't change it. You do that mean you add one to it to see the next char. Do this instead

    Code:
    char *inPTR=const_cast<char*>(in);
    This takes away the const-ness of in so you can modify it. This really takes away the purpose of having in as const except to assure the user that it won't change.

    Also, use for loops, and how about single quotes? You only handle double quotes....
    No, you are incorrect on this.

    const char* ptr;

    doesn't say that the POINTER is constant, it just says that the data it POINTS to is constant. You can alter the value of a const char*, just not the value of what it points to.

    In order to create a constant POINTER, you put the const after the * like:

    char* const ptr; // constant pointer to a char

    or

    const char* const ptr; // constant pointer to a constant char

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    interesting....

    this is why i love c++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  4. Display Lists (OpenGL)
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 06-05-2005, 12:11 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM