C++//Microsoft Visual C++ .NET//Integer Arrays and String Manipulation functions

This is a discussion on C++//Microsoft Visual C++ .NET//Integer Arrays and String Manipulation functions within the C++ Programming forums, part of the General Programming Boards category; Question: Can we use a null terminated integer array in the sting manipulation functions? I want to find an easy ...

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Arrow C++//Microsoft Visual C++ .NET//Integer Arrays and String Manipulation functions

    Question:

    Can we use a null terminated integer array in the sting manipulation functions? I want to find an easy way to compare to integer strings, but it doesn't work.

    Code:
    Array1[0]=9
    Array1[1]=8
    Array1[2]=7
    Array1[3]='\0'
    
    Array2[0]=6
    Array2[1]=5
    Array2[2]=4
    Array2[3]='\0'
    
    strcmp(Array1,Array2)
    If I initialize it and pass it to function like a constant * shouldn't it work?

    Code:
    bool HugeInteger::isNotEqualTo(const int *Array1,const int *Counter1, const int *Array2,const int *Counter2)
    Does it have to be a char string or can we manipulate a integer array by some form of convertion so that it is compatable with strcmp();

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Interesting...

    The c-style string manipulation/compare functions are looking for a an array of type-char values. With type-ints you might be confusing it... You might try using type-char. (A char is big enough to hold any single digit.)

    Also, if any of your digits are zero, they are going to look like null-terminations to the c-style string functions. This could really foul things up.

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Can we use a null terminated integer array in the sting manipulation functions? I want to find an easy way to compare to integer strings, but it doesn't work.
    In general NO, but some could work. When you are declaring a variable like this
    Code:
    int numb = '\0';
    //int num = 0;
    then the ascii-value for \0 is 0. Character string works a different and therefore it is not possible to use then. E.i,
    Code:
    strcmp(Array1,Array2)
    would fail. This page will help you in this matter.

    Your version of Equal-function could indeed work but if I were to use it I would found it very hard to use. Why compare a HugeInteger object with a int-array? Wouldnt it be intuitive to compare a HugeInteger with a HugeInteger object?
    Code:
    bool HugeInteger::isNotEqualTo(const HugeInteger * const pRef)
    //boll HugeInteger::isNotEqualTo(const HugeInteger &pRef)
    P.S. Donīt open mutiple threads on topics that are almost identical. Stick to one.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  4. #4
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Sorry about the second thread

    I'm sorry for creating a second thread for the same topic.
    I'm a newbie so I'm sorry if I didn't recognize it as the same topic.
    Thanks for the web link. It has a really cool selection of information.

    modf()
    Can modf() be used to split a Hugeint into it's seperate integers?
    say I want 12345 to be split into 1,2,3,4, and 5.

    modf(12345,Name of pointer);

    The way I'm currently doing seems like to much to type *lazy*. See below.
    Code:
    //prototype
    bool TestInteger(int*,const int,int*)
    
    //member function call
    TestInteger(Num1,SIZE,Buffer1)
    
    //member function definition
    bool HugeInteger::TestInteger(int *Array,const int SIZE,int *Num) 
    {
    	for(x=0;x<SIZE;x++)// devides integer 
    	{
    		Array[x]=*Num%10;
    		*Num/=10;
    	}
    
    
    	for(x=0;x<SIZE;x++)
    	{
    		if(Array[x]<'48'&&Array[x]>'57')
    		{		
    			cout<<"Data FAILED Integer confirmation!!"<<endl;
    			return false;
    		}
    	}
    	cout<<"Data PASSED Integer confirmation"<<endl;
    	return true;	
    }
    This doesn't work
    Can you see any logic error in this?
    I'm trying to test every location of the array for valid data. It doesn't prevent errors from inputing invalid data.
    I want it to error out on digits like (e.g. 123e3, 1234\3, etc.)
    A test for typos kind of thing
    Last edited by xviddivxoggmp3; 09-19-2003 at 12:15 AM.

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    No modf() is used when you want to split up the integer part and the fractional part. You have to write your own version of seperate func. Your first part of the TestInteger member function should work because this works
    Code:
    #include <iostream> 
    
    using namespace std;
    
    int main(void)
    { 
    int numb = 15679;
    //Convienient if you made a function that calculates how many digits there is in a number
    const int Size = 5;
    int reminder;
    for (int i = 0; i < Size; ++i)
    {
    	reminder = numb % 10;
    	numb /=10;
    	cout << reminder << endl;
    }
    return 0;
    }
    The second part where you actually make an error check is an error. You canīt compare it like that. Do this and you will understand why
    Code:
    cout << '48' << endl;
    The soultion is actually very easy. Instedd of retrieving values for ASCII you could do a simple test as
    Code:
    if ((test >= 0) && (test <=9))
    This is not the best error handling but it may be sufficient for your app.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  6. #6
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Unhappy no work.

    if ((test >= 0) && (test <=9))
    It still did not work. What happens, I think, is at execution if i mistype 12w2, then followed by a return.

    I receive confirmation of the data passing the check, and results in values of num1[0]=2, num1[1]=1, and num2[0]='\0' being asigned(i think). Then erronious results produce from my calculations.

    I want it to error out and trigger the repeat of the data acquiring code.
    //output
    Please enter a Huge Integer terminated by a return.
    Entry:
    12w2
    Array Location0= 2
    Array Location1= 1
    Data PASSED Integer confirmation
    Please enter a Huge Integer terminated by a return.
    Entry:
    Data PASSED Integer confirmation
    Your inputed strings are not equal
    Digit Equality: Fail (1) Array Location (0)
    Inputed Integer 1
    21
    Inputed Integer 2


    Integer Result of Addition
    0
    Integer Result of Subtraction
    0
    Do you wish to try again?(y/n)
    I even tried to change it this morning, but no luck.
    Another verification process I attempted was to cin a dummy char to catch any error and trigger failure.
    Code:
     
    	for(x=0;x<SIZE;x++)
    	{
    		Array[x]=*Num%10;
    		*Num/=10;
    		if (Array[x]!='\0')
    			*Count+=1;
    
    		cin>>dummychar;
    
    		if(dummychar!='\0')
    		{		
    			cout<<"Data FAILED Integer confirmation!!"<<endl;
    			return false;
    		}
    	}
    if you do not input a char it hangs until you enter a char.
    How can I validate data that is inputed into the below code?
    Code:
    int buffer=0;
    
    cin>>buffer;
    Last edited by xviddivxoggmp3; 09-19-2003 at 06:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String manipulation
    By Karpaty in forum C Programming
    Replies: 16
    Last Post: 12-21-2007, 06:16 PM
  2. Separate long string into multiple arrays
    By cashmerelc in forum C Programming
    Replies: 6
    Last Post: 11-27-2007, 02:57 AM
  3. String Compare Using Dynamic Arrays
    By djwicks in forum C Programming
    Replies: 4
    Last Post: 03-31-2005, 08:01 PM
  4. string arrays
    By Raison in forum C Programming
    Replies: 27
    Last Post: 10-02-2003, 07:27 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 08:36 PM

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