14 Replies - 794 Views - Last Post: 27 September 2009 - 02:42 PM Rate Topic: -----

#1 janine6192  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • 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]
Was This Post Helpful? 0
  • +
  • -

#3 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • 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);		   
	   
			}


 

Was This Post Helpful? 0
  • +
  • -

#4 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 carltech  Icon User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • 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;
}

Was This Post Helpful? 0
  • +
  • -

#7 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#8 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • 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? :)
Was This Post Helpful? 0
  • +
  • -

#11 carltech  Icon User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#12 janine6192  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#13 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Separating a digit

Posted 26 September 2009 - 06:00 PM

What code?
where does it glitch?
Sure we can :)
Was This Post Helpful? 0
  • +
  • -

#14 zhanfeng  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • 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. :D
#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;
}


Was This Post Helpful? 0
  • +
  • -

#15 janine6192  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Error executing link.exe.

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;

		//Calculations for outputting final answer

				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


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1