part of an array to integer?

This is a discussion on part of an array to integer? within the C++ Programming forums, part of the General Programming Boards category; Hello, does anyone know if it is possible to convert part of an array e.g Sarray[1] to Sarray[5] to a ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    4

    Question part of an array to integer?

    Hello, does anyone know if it is possible to convert part of an array e.g Sarray[1] to Sarray[5] to a single integer variable to perform calculation.

    Am trying to do a calculator in mfc with buttons (nothing to hide.. its a homework) but the teacher doesnot code in cpp, so i have no choice except to code in vb which i don't code in. despite the teacher want to help me he can't since its cpp.

    Am doing it using mfc.
    my algo is as follows....

    Assume am pressing button 5 , three times
    char Sarray [9] //max numbers on display
    counter=1;
    int actualnum
    {
    when button 5 press
    Sarray[counter]='5'
    put Sarray[1 to counter] in actualnum; //where am having trouble
    counter=counter+1;
    }

    The above doesnot have any real codes, except for the int and char etc.., i can manage with the rest, just wrote them in simple english for the easy understanding, Am stuck in the part where i would need to combine the
    Sarray[1]=5;
    Sarray[2]=5;
    Sarray[3]=5;
    into actualnum=555

    Any idea?

    Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    If you're reading user input, then this is most likely
    Sarray[1]='5';

    If you add
    Sarray[4] = '\0';

    Then you can say
    int myint = atoi( Sarray );
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Hello, does anyone know if it is possible to convert part of an array e.g Sarray[1] to Sarray[5] to a single integer variable to perform calculation.
    Of course there is , but I donīt know if this is a good solution. Start from the end-index and work your way to the beginning-index of the array. Beware of potential errors.
    Code:
    // Do not
    //* Pass invalid array bounderis (zero based) e.i int array[5] -> range 0 to 4
    //* Array with negative values
    //* pFromIndex greater than pToIndex
    //* Array with other than int elements
    int ConvertToNumber(const int *pArray, int pFromIndex, int pToIndex)
    {
    	int total = 0;
    	int multiply = 1;
    	for(int i = pToIndex; i >= pFromIndex; --i)
    	{
    		total += (multiply*pArray[i]);
    		multiply *= 10;
    	}
    	return total;
    }
    When you understand this code snippet it should be easy to convert it to your needs.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  4. #4
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    Code:
    int ConvertToNumber(const int *pArray, int pFromIndex, int pToIndex)
    {
    int total = 0;
    int multiply = 1;
    for(int i = pToIndex; i >= pFromIndex; --i)
    {
    if(pArray<=0){printf("error %i",i);
    total += (multiply*pArray[i]);
    multiply *= 10;
    }
    return total;
    }

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    4
    wow! was not expecting to be helped that quick, thanks guys. I'll now experiment with the codes you gave me to understand it better. Thanks. I'll drop in another message after i've tried out all of them.

    -eifer

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    4
    I used salem's code, since it was straight forward and simple.
    Thanks again salem, ripper and enjoy. I'd be doomed if it you were not here.
    -eifer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading an integer array from console??
    By pico in forum C Programming
    Replies: 16
    Last Post: 12-23-2008, 07:53 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. Convert Integer into an array
    By ubernos in forum C Programming
    Replies: 2
    Last Post: 11-08-2005, 09:30 AM
  5. Representing a Large Integer with an Array
    By random_accident in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2005, 11:23 AM

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