# Separating a digit

Page 1 of 1

## 14 Replies - 1005 Views - Last Post: 27 September 2009 - 02:42 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=126933&amp;s=418aa7299ee8b7f4bdccd689f86af485&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 janine6192

Reputation: 0
• Posts: 44
• Joined: 20-September 09

# Separating a digit

Posted 20 September 2009 - 09:39 AM

If the user inputs a digit of 678539, is there a way that I can separate each digit, so that it will look like this 6 7 8 5 3 9?
Is This A Good Question/Topic? 0

## Replies To: Separating a digit

### #2 gabehabe

• GabehabeSwamp

Reputation: 1399
• Posts: 10,965
• Joined: 06-February 08

## Re: Separating a digit

Posted 20 September 2009 - 09:40 AM

We don'd dish out code for free, but you should try searching the snippets.
http://www.dreaminco...snippet4015.htm

[rules][/rules]

### #3 Crunch

• D.I.C Lover

Reputation: 139
• Posts: 1,222
• Joined: 28-July 09

## Re: Separating a digit

Posted 20 September 2009 - 09:43 AM

```	 #include <stdio.h>

int main (void)
{

char *string = "hello world";

int i =0;
char c= ' ';

for(; c != '\0'; i++) {

printf("%c \n" , string[i] );
c = string[i];
}

printf(" Enter any value to exit");
int l =0;
scanf("%d " , &l);

}

```

### #4 poncho4all

Reputation: 123
• Posts: 1,413
• Joined: 15-July 09

## Re: Separating a digit

Posted 20 September 2009 - 09:45 AM

Well i would suggest to receive the number as char get them into and array and then cout with a ' ' in between them.

Simple but really easy f3

### #5 carltech

• What did you call me?

Reputation: 28
• Posts: 997
• Joined: 19-October 07

## Re: Separating a digit

Posted 20 September 2009 - 09:59 AM

either take in the input as a string and separate it or take it in as a number and use division to find each number in each place

### #6 gabehabe

• GabehabeSwamp

Reputation: 1399
• Posts: 10,965
• Joined: 06-February 08

## Re: Separating a digit

Posted 20 September 2009 - 10:02 AM

I think you guys missed my reply. Mod magic ftw.

```/*
* Function to convert an split an unsigned integer into
* an array, complete with leading zeros
* Author: Danny Battison
* Contact: gabehabe@gmail.com
* MODIFIED: Removed possibility of numeric overflow. kthx. :)
*/

void val_to_array(unsigned int to_convert, int array[]) {
// use length of 10 to match INT_MAX - since we're dealing with ints,
// it can't be greater than 2147483647*2 (unsigned int max)
int index = 9; // start from the end
while(index >= 0) { // while we still have a digit
// ZOMIGOSH TERNARY FUN!!1!ONE!
array[index--] = ((to_convert = (index == 8 ? to_convert : to_convert / 10)) > 0) ? to_convert % 10 : 0;
}
}

/** EXAMPLE USAGE **/
#include <iostream>
int main(int argc, char* argv[]) {
unsigned int input = 0;
std::cout << "Enter the value: ";
std::cin >> input;

int array[10] = {0};
val_to_array(input, array); // array will be modified by the function
for(int i = 0; i < 10; i++) {
std::cout << array[i];
}

std::cin.get();
return EXIT_SUCCESS;
}
```

### #7 AntonWebsters

• D.I.C Regular

Reputation: 88
• Posts: 428
• Joined: 15-August 09

## Re: Separating a digit

Posted 20 September 2009 - 11:13 AM

I would do it this way...
```#include<iostream>
#include<cmath>
using namespace std;
int main ()
{
int array[6];
int digit = 678539;
int num;
int length = sizeof(array)/sizeof(array[0]);
for(int i = length-1;i >= 0;i--){
num = digit % 10;
digit /= 10;
array[i] = num;
}

for(int i = 0;i < length;i++){
cout << array[i] << " ";
}
cout << endl;

return 0;
}

```

### #8 gabehabe

• GabehabeSwamp

Reputation: 1399
• Posts: 10,965
• Joined: 06-February 08

## Re: Separating a digit

Posted 20 September 2009 - 11:53 AM

That's EXACTLY how I do it, just without the ternary.

You could probably reduce my ternary down into a 1 line recursive call, but I'm too lazy to do it.

This post has been edited by gabehabe: 20 September 2009 - 11:57 AM

### #9 AntonWebsters

• D.I.C Regular

Reputation: 88
• Posts: 428
• Joined: 15-August 09

## Re: Separating a digit

Posted 20 September 2009 - 12:24 PM

Really? Sorry...I didn't notice it.
My god..I should seriously learn to use ternary operators lol...
I always go @.@ when I look at ternary...guess I'm not used to it.

### #10 gabehabe

• GabehabeSwamp

Reputation: 1399
• Posts: 10,965
• Joined: 06-February 08

## Re: Separating a digit

Posted 20 September 2009 - 12:45 PM

Yeah. This one's pretty ugly, but it works quite nicely.
array[index--] = ((to_convert = (index == 8 ? to_convert : to_convert / 10)) > 0) ? to_convert % 10 : 0;

Notice the division and modulus in there?

### #11 carltech

• What did you call me?

Reputation: 28
• Posts: 997
• Joined: 19-October 07

## Re: Separating a digit

Posted 20 September 2009 - 08:11 PM

Thats some dodo code there Gabe. Nah just kidding. Ternary isn't as bad as people make it seem

### #12 janine6192

Reputation: 0
• Posts: 44
• Joined: 20-September 09

## Re: Separating a digit

Posted 26 September 2009 - 05:38 PM

thanks for your responses but I created a new called Creating a Digit Separator Program. I have displayed my code on that topic. but it still has glitches. could you guys help me please.

### #13 poncho4all

Reputation: 123
• Posts: 1,413
• Joined: 15-July 09

## Re: Separating a digit

Posted 26 September 2009 - 06:00 PM

What code?
where does it glitch?
Sure we can

### #14 zhanfeng

Reputation: 6
• Posts: 75
• Joined: 09-November 08

## Re: Separating a digit

Posted 26 September 2009 - 07:02 PM

Here is how I would have done it.
```#include <iostream>
//#include <cmath>

using std::cout;
using std::cin;
//using std::pow;
using std::endl;

int main()
{
long number, divisor;
int digit = 0, numberOfDigits = 0;

cout<<"Enter a number: ";
cin>>number;

//Determine the number of digits in a number

do {
numberOfDigits++;
divisor = 1;
for (int k = 1; k <= numberOfDigits; k++)
divisor *= 10;
} while (number % divisor != number);

divisor /= 10;

//Obtain the left most digit of the number and discard that digit from the number each run

for (int k = 1; k <= numberOfDigits; k++) {
digit = number / divisor;
number %= divisor;
divisor /= 10;
cout<<digit <<" ";
}
cout<<endl;

cin.get();
cin.get();
return 0;
}

```

### #15 janine6192

Reputation: 0
• Posts: 44
• Joined: 20-September 09

## Re: Separating a digit

Posted 27 September 2009 - 02:42 PM

The glitch with my code is that it compiles but it does not run. It states these errors:

DigitDriver.obj : error LNK2001: unresolved external symbol "public: void __thiscall DigitSeparator::setSeparator(long)" (?setSeparator@DigitSeparator@@QAEXJ@Z)
DigitDriver.obj : error LNK2001: unresolved external symbol "public: __thiscall DigitSeparator::DigitSeparator(void)" (??0DigitSeparator@@QAE@XZ)
Debug/DigitDriver.exe : fatal error LNK1120: 2 unresolved externals

DigitDriver.exe - 3 error(s), 0 warning(s)

Please help me! I have been working on this program for more than a week and it is due this Wednesday. Any assistance so that this program will run will be appreciated.

``` #include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "DigitSeparator.h"//include the class definition of DigitSeparator

int main()
{

long int number;			  //user input

cout<<"Welcome to the Digit Separator Program!\n";//what the user sees on the screen
cout<<"Please enter a six digit number.\n";//prompts user to enter a digit
cin>>number;//input by user is stored in number

DigitSeparator myDigitSeparator;
myDigitSeparator.setSeparator(number);

return 0;

}

```

```/*Implementations of the DigitSeparator member-function definitions.
The setSeparator function performs validation.
*/

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "DigitSeparator.h"//include definition of class DigitSeparator

// class DigitSeparator definition

class DigitSeparator
{
public:
//constructor initializes digits with integer supplied as argument
DigitSeparator(long int number)
{
setSeparator(number)

}//end DigitSeparator constructor

//function to set the digit
void setSeparator(long int number)
{
Separator = number;//store the digit in the object

/*Declaration of variables used for:
Integer Division and Modulus Operations
*/
int	 num1;
int b;
int	 num2;
int d;
int num3;
int f;
int num4;
int h;
int num5;
int num6;

//Below is the Integer and Modulus Operations

num1 = Separator/100000;
b = Separator%100000;
num2 = b/10000;
d = b%10000;
num3 = d/1000;
f = d%1000;
num4 = f/100;
h = f%100;
num5 = h/10;
num6= h%10;

num1 = num1 * 2
num2 = num2 * 2
num3 = num3 * 2
num4 = num4 * 2
num5 = num5 * 2
num6 = num6 * 2

cout<<num1<<" "<<num2<<" "<<num3<<" "<<num4<<" "<<num5<<" "<<num6;
}
}
}; //Ends the DigitSeparator class

```

```/*DigitSeparator class definition presents the publice interface
of the class. Member-function definitions appear in DigitSeparator.cpp.
*/

//DigitSeparator class definition

class DigitSeparator
{
public:
DigitSeparator();//constructor that initializes a DigitSeparator object
void setSeparator(long int); //function that sets the Separator

};//end class DigitSeparator

```