Thread: (newb) getting two diffrent answares with the same func

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    111

    (newb) getting two diffrent answares with the same func

    hello ..

    i can't understand why but the same code with small adjustements give me diffrent answares :
    when i use the C type of work char* complement(char* , char *); all works nice.
    but when i use cpp func char* complement(foo &object) i get strange answares.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    class foo{
    	char* number;
    	int size;
    	public:
    		foo();
    		foo(char *value);
    		~foo();
    	char* complement(foo &object);
    	char* complement(char* , char *);
    	};
    
    foo::foo(){
    	number=NULL;
    	size=0;
    }
    foo::foo(char *value){
    	size=strlen(value)+1;
    	number=new char[size];
    	strcpy(number,value);
    }
    
    foo::~foo(){
    	if (number!=NULL)
    		delete [] number;
    }
    /*
     this works fine  but it afaik its ain't oop
    
     char *foo::complement(foo &object){
         char result=complement(this->number,object.number);
        return result;
     }
    
    */
    
    char* foo::complement(foo &object){
    	
    	int second_size=object.size;
    	char *result=NULL;
    	int result_size=(size>second_size) ?size+1: second_size+1 ;//1 more for '\0'
    	result=new char [result_size];
    	int biggest_size=(size>second_size) ?size : second_size ;
    	int smallest_size=(size<second_size) ?size : second_size ;
    	char m[12]={'0','9','8','7','6','5','4','3','2','1','0','\0'};
    	int flag=0;
    	if(second_size<=size)
    		{
    			for (int i=smallest_size-1;i>-1;i--)
    			{
    			
    				result[i+biggest_size-smallest_size]=m[ object.number[i]-'0'+flag];
    				if (result[i+biggest_size-smallest_size]!='0')
    					flag=1;
    			}
    			for (int i=biggest_size - smallest_size-1;i>-1;i--)
    			{
    				result[i]='9';
    			}
    		}
    	
    	if (second_size > size)
    		{
    			for (int i=second_size-1;i>-1;i--)
    			{
    				result[i]=m[ object.number[i]-'0'+flag];
    				if (result[i]!='0')
    					flag=1;
    			}
    		}
    	
    	result[result_size -1]='\0';
    	
    	return result;
    
    }
    
    
    char* foo::complement(char *first,char *second){
    	int size=strlen(first);
    	int second_size=strlen(second);
    	char *result=NULL;
    	int result_size=(size>second_size) ?size+1: second_size+1 ;//1 more for '\0'
    	result=new char [result_size];
    	int biggest_size=(size>second_size) ?size : second_size ;
    	int smallest_size=(size<second_size) ?size : second_size ;
    	char m[12]={'0','9','8','7','6','5','4','3','2','1','0','\0'};
    	int flag=0;
    	if(second_size<=size){
    	for (int i=smallest_size-1;i>-1;i--)
    		{
    			result[i+biggest_size-smallest_size]=m[ second[i]-'0'+flag];
    			if (result[i+biggest_size-smallest_size]!='0')
    				flag=1;
    		}
    	for (int i=biggest_size - smallest_size-1;i>-1;i--)
    		{
    		result[i]='9';
    		}
    	}
    	if (second_size > size){
    		for (int i=second_size-1;i>-1;i--)
    		{
    			result[i]=m[ second[i]-'0'+flag];
    			if (result[i]!='0')
    				flag=1;
    		}
    	}
    	result[result_size -1]='\0';
    	
    	return result;
    }
    
    int main(){
    foo a("10");
    foo b("950");
    
    char *result;
    result=a.complement("10","950");
    cout<<result<<endl;
    delete [] result;
    result=a.complement(b);
    cout<<result<<endl;
    delete [] result;
    }
    im using g++ and running debian.
    no errors or warnings :

    $ g++ -o comp -Wall -Wextra -ansi -pedantic comp.cc
    $

    Yours truly
    Last edited by jabka; 04-16-2007 at 05:47 AM. Reason: fixed the size=2 to size=0 at constractiom and removed '\0' at constructer

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Is there a special reason why you have implemented the function twice ? You can pass both foo's strings to the function that takes two strings instead of debugging two functions
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    111
    yes i prefer to work with objects (im studing cpp now).
    the function with two string works gr8 but it was only written to show that almost the same code gives two diffrent answares.
    i prefer to take the code written allmost in c (two strings) and to transfer it to object oriented.

    for now i do this:

    Code:
     char* foo(foo object){
      char* result=complement(this ->number,object.number);
     return result;
    }
    but afaik it ain't OO programing

    more is that i can't understand why there are two diffrent answeres

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    111
    moment ..
    could you please compile and run it on your system (maybe the fault isn't in program but with some other soft that runs on my pc )

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by jabka View Post
    moment ..
    could you please compile and run it on your system (maybe the fault isn't in program but with some other soft that runs on my pc )
    No it's not. Without even compiling your code, I can guarantee that you won't stumble on compiler bugs that easily.

    yes i prefer to work with objects (im studing cpp now).
    If working with objects meant implementing code more than once, OOP would have been stillborn.


    As I see it, the most likely error is the off-by-one: You treat the sizes in the char* and the object version the same, when they aren't: the sizes in the object version already include the \0.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > number=NULL;
    > size=2;
    At least make the values consistent with one another, say NULL and zero

    > size=strlen(number)+1;
    > number=new char[size];
    > strcpy(number,value);
    > number[size-1]='\0';
    How can you do strlen() on it before you even initialise it?
    You should be doing strlen on the value parameter.
    Also, the assignment of a '\0' is a waste of time as the strcpy() will have already copied it.
    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.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    111
    Quote Originally Posted by Salem View Post
    > number=NULL;
    > size=2;
    At least make the values consistent with one another, say NULL and zero

    > size=strlen(number)+1;
    didn't see this .. (fixed it)
    thnx
    Quote Originally Posted by Salem View Post
    >
    > number=new char[size];
    > strcpy(number,value);
    > number[size-1]='\0';

    How can you do strlen() on it before you even initialise it?
    You should be doing strlen on the value parameter.
    Also, the assignment of a '\0' is a waste of time as the strcpy() will have already copied it.
    thnx didn't thout that strcpy opies '\0' as weal.
    but ..

    still the same output :
    050
    049XXX

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    111
    one of the strangest things is :
    in the second excution (the Object version) the i get other answar 049 instead if 050

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    111
    i have edited the code on the first paste to the correct form :
    changed size=2 to size=2
    removed '\0' at constructer

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well, it's called debugging, so now is the time to practice doing it.

    Step through the code one line at a time, and verify (for example) that the array indices are as you expect. It's a pretty sure bet that you're stepping off the end of an array somewhere.

    Also, consider writing a third function which both
    char* foo::complement(char *first,char *second)
    and
    char* foo::complement(foo &object)
    can call to do the bulk of the work, since the code is essentially the same. At least then you only have to debug it once rather than twice.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-22-2008, 11:37 PM
  2. Newb C++ Programmer
    By Philandrew in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 08:44 PM
  3. typedef a func
    By trekker in forum C Programming
    Replies: 4
    Last Post: 07-02-2002, 05:15 AM
  4. Delete & print func()
    By spentdome in forum C Programming
    Replies: 5
    Last Post: 05-29-2002, 09:02 AM