Getting digits out of an int?

May 14, 2016 at 11:11am
I'm having trouble with getting the digits out of an integer.
Here is my assignment instructions.

Define a class named MyInteger that stores an integer. It has default and argument constructor(s), and has functions to get and set the integer value. Overload the [] operator so that the index returns the digit in position i, where i=0 is the least significant digit. If no such digit exists,then -1 should be returned. This [] operator should not change the integer value of the object. For example, if x is of type MyInteger and is set to 418, then x[0] should return 8, x[1] should return 1, x[2] should return 4, and x[3] should return -1

This is the class I have made:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class MyInteger
{
private:
	int x;
public:
	MyInteger() {
		x = 0;
	};
	MyInteger(int a) {
		x = a;
	};
	int get;
	void set(int a) { x = a; };

	//Overloaded subscript operator
	int& operator[] (const int index);
};


But how can I define the operator[] to get the digit of an integer? Isn't this only to be used for an array? ints dont work like strings, do they?

So far, I have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int MyInteger::operator[] (const int index)
{
	string strx = to_string(x);
	if (index == 0)
	{
		return (x % 10);
	}
	else if (index <= strx.length())
	{
		return (x / (index * 10) % 10);
	}
	else
		return -1;
}


But when it comes to the last(first) integer, it returns 0
Last edited on May 14, 2016 at 11:34am
May 14, 2016 at 11:35am
When you define the operator[], it does whatever you tell it to. It's just a function, and you can program a function to do whatever you like.

Here's an example

1
2
3
4
	int operator[] (const int index)
        {
           return (std::to_string(x)[index] - '0');
        }       


This uses the std::to_string function from the <string> header. If that's something you're not expected to be using, you can find various ways to take an integer and isolate a single digit from it. Don't forget to check for the -1 return case.

The point is, operators are just functions, and you can write them to do whatever you like.

Last edited on May 14, 2016 at 11:36am
May 14, 2016 at 12:47pm
Figured it out, had to use one of those various ways to isolate a digit from an integer. Came up with this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int MyInteger::operator[] (const int index)
{
	string strx = to_string(x); // to determine length of the int.

	if (index == 0)
	{
		return (x % 10);
	}
	else if (index <= strx.length() -1)
	{
		//clunky looking. Had to use pow() which didnt know which instance of the function to call
		//so each member of the function had to be cast.
		// Y is always 10^the index, used to subtract a digit.
		int y = pow((double)10, (int)(index)); 
		cout << endl << y << endl;
		return x / y % 10;
	}
	else
		return -1;
}
May 14, 2016 at 2:02pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string>

class MyInteger
{
    private: int x ;

    public:
        MyInteger( int v ) : x(v) {}
        
        // [] operator should not change the integer value of the object. ergo, const-qualified member function
        // parameter index is passed by value; qualifying it with const does not add anything to the interface 
        int operator[] ( const int index ) const ;
};

int MyInteger::operator[] ( const int index ) const
{
    if( index < 0 ) return -1 ;

    const std::string strx = std::to_string( x < 0 ? -x : x ); // ignore the sign

    if( index >= int( strx.length() ) ) return - 1 ; // out of range
    else return strx.rbegin()[index] - '0' ; // index from right to left (of the string in reverse) 
                                             // http://en.cppreference.com/w/cpp/string/basic_string/rbegin
	                                     // '7' - '0' == 7 etc.
}

http://coliru.stacked-crooked.com/a/865d6ee77f291477
Last edited on May 14, 2016 at 2:05pm
Topic archived. No new replies allowed.