Reading one digit at a time from a integer.

Pages: 12
Modulo '%' is the operator that will give the remainder of two numbers.

a = 10 % 6;

a is now 4.

@Brandon69: Correct cin.get() only seems to work with char out of the box.
Last edited on
Just use cin.get() to get the characters then convert them to integers.
if you need to use modulo and you know the size of input you can do things like this, but it's pretty case specific:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   int number = 15238;
   int dig1, dig2, dig3, dig4, dig5;

   dig5 = number % 10;
   number /= 10;

   dig4 = number % 10;
   number /= 10;

   dig3 = number % 10;
   number /= 10;

   dig2 = number % 10;
   number /= 10;
   
   dig1 = number;
Thank you everyone. I believe I have more than enought to go forward with this assignment. Been stuck for about a week, so thank you very much.

Respecfully,
Brandon
Topic archived. No new replies allowed.
Pages: 12