### separating individual digits from an integer

hi..

Can someone help me.. I am trying to improve my C++. It is so bad.I cant even do this.

I need to read an integer and then separate them into individual digits. I tried looking in the forums..but some of the solutiosn involve array and a static limit.

I need something more like..any mumber of integers, it can consist of 2 digits or 10 digits. For example, if I enter 8272, the program should display 8 2 7 2 and if I key in 75602, it should display 7 5 6 0 2 and then finally get the sum..

But I need to know how to get it to output the individual digits. I have created the following program, but it only outputs the last digit

 ``1234567891011121314151617181920212223242526`` ``````#include using namespace std; int main() { int enter_num, division=0; int remainder; cout<<"Please enter numbers"<<"\n"; cin>>enter_num; do { remainder=enter_num%10; division=enter_num/10; cout<

Now just why would you want to do that?

Either way... I recommend a for loop which divides your number by decreasing powers of ten (but do not change the original number), and stores that in an element of an integer array.

http://cplusplus.com/doc/tutorial/arrays/

You will also need a method of checking how large your number is. Try the reverse of the above that checks if number/poweroften is greater than one. Use a floating point number for this check.

-Albatross
The condition for the do-while loop, on line 21, is assigning 0 to division and resulting in false, which terminates the loop. Fix that condition and you'll be on your way to figuring this one out.
Looks like some practice assignment from some book or something. Nice to see you've tried alot. Problemsolving skill is something that grows on you, don't worry. And never be afraid to ask :)
hi..guys..
well.. I managed to come up with something, but I kinda converted the do while.. to while..

but somehow..I am getting the numbers reversed..arghhh..need some help

 ``123456789101112131415161718192021222324`` ``````#include using namespace std; int main() { int num,counter,limit,division; cout<<"Please enter how big the set will be"<<"\n"; cin>>limit; cout<<"Enter the integers please"<<"\n"; cin>>num; counter=0; while(counter

Last edited on
You could also read the individual digits as chars.

 ``1234567891011121314`` `````` std::string buffer; std::cin >> buffer; for(unsigned int i = 0; i < buffer.size(); ++i) { if(!isdigit(buffer[i])) // deal with bad input and leave the loop std::cout << buffer[i]; if(i != buffer.size() - 1) // no space required after the last digit { std::cout << ' '; } }``````
Last edited on
Hi thanks for the input

this program is actually ane exercise froma book, and its from the control structures chapter..and I need to use while loop to do it..

i have not gone to arrays part yet...

other solutions..using while ...?
try this

 ``12345678910111213141516`` ``````#include int main() { int num; std::cout << "Please enter a number"; std::cin << num; while(num > 0) { std::cout << num%10 << std::endl; // Output the last digit num /= 10; // Divides the number by 10 i.e. removes the last digit } std::cin.get(); return 0; }``````

@kalp1200
Yes, you get them reversed because you are getting the digits from least-significant to most-significant, and printing them in that order.
However, humans read numbers from most- to least-significant order.

123 --> 12 and 3 (print 3)
12 --> 1 and 2 (print 2)
1 --> 0 and 1 (print 1)

printed: 321

The way to fix it is either:
1) put the numbers you get into a vector or something and print them in reverse order
2) get the digits from most-significant to least (instead of least to most as you are doing now).

Good job and good luck!
Hi everyone, i think I have solved the question.

But mine is very long...now that i have solved..can anyone send me the shorter version of it..
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` ``````/*Outputs the digit*/ #include #include using namespace std; int main() { int numbers,numbers1,answer,i; int size,sum=0; double times; cout<<"Please enter how big the set will be"<<"\n"; cin>>size; cout<<"Enter the integers please"<<"\n"; cin>>numbers; if(numbers<0) { for(i=size;i>0;i--) { times= pow(10.0,size-1); //cout<(answer)=-(static_cast(numbers))/static_cast(times); cout<(numbers1)=static_cast(numbers)%static_cast(times); sum = sum + answer; //cout<0;i--) { times= pow(10.0,size-1); //cout<(answer)=static_cast(numbers)/static_cast(times); cout<(numbers1)=static_cast(numbers)%static_cast(times); //cout<
This one is smaller...

 ``123456789101112131415161718192021222324252627282930313233343536`` ``````int main() { int enter_num, temp_num, sum = 0; int divisor, digit, count = 0; cout<<"Please enter number"<<"\n"; cin>>enter_num; temp_num = enter_num; // Counting the number of digits in the entered integer while (temp_num != 0) { temp_num = temp_num/10; count++; } temp_num = enter_num; // Extracting the digits cout<<"Individual digits in the entered number are "; do { divisor = static_cast(pow(10.0, --count)); digit = temp_num / divisor; temp_num = temp_num % divisor; cout<<" "<
Last edited on
Topic archived. No new replies allowed.