I'm having a problem with a part of my code that is designed to take a number (code) and extract the individual digits using a functino call for use later in the program. (At this point in the program I have alreay input the code number as an int)

void extractDigits(int); was my function prototype.

extractDigits (&code); was what I had in the main function.

void extractDigits(int& code)
{
     int base, digit1, digit2, digit3, digit4, digit5;
     base = code;
     digit5 = base % 10;
     base /= 10;
     digit4 = base % 10;
     base /= 10;
     digit3 = base % 10;
     base /= 10;
     digit2 = base % 10;
     base /= 10;
     digit1 = base % 10;
}

This was my function definition after the main function.

The main problem that I am having with this is how to return the value of the digits to the main function after the function call so that they can be used later in the program and not just in the scope of the function call above.

Any ideas would be greatly appreciated.

Recommended Answers

All 4 Replies

Change the extraction function to take a reference to an int as an argument and return an int. Extract just the least significant digit from the number passed and return it to the calling function and remove the last digit from the number passed in, just like you did in your original function.

Create a copy of the original number so you can keep track of the original number, if desired. Then use one copy of the original number to generate the digits using a loop. Run the loop while the number being digitized is greater than zero. Extract one digit from the number being digitized each time through the loop by calling the extraction function one time each time through the loop, storing the return value in whatever container you wish for later use.

An another way is you can store all digits in an array and pass the array name as int and in main program do a pointer to that and using for loop get all those number.
And an easy way is that, what I have done in my previous program just get a string of number and convert all index to int type.
thats all from mine.
happy coding.

See if something like this would work for you :

string toString(int num){
	stringstream strm;
	strm << num;
	return strm.str();
}

Then you can just use each digit like so :

string num = toString(125);
//num[0] == '1';
//num[1] == '2';
//num[2] == '5';

/* or if you want the int value */
int firstDig = num[0] - '0';
int secDig = num[1]  - '0';
int thirdDigit = num[2] - '0';

The " num[*] - '0' "; is a conversion from a char digit to a int digit. You
can create a function that does this to help make it look better :

int toIntDigit(char aDigit){ return aDigit - '0'; }
/* use it like so */
string num = toString(500);
int firstDigit = toIntDigit(num[0]);

Just throwing an idea out there.

Thanks guys, I got it working now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.