# Stupid easy noob question.

• 04-10-2008
iAmFedor
Stupid easy noob question.
Hi, i've tried look about for this but cant find it :(.

Basically all i want to do its break a number up into single ints.

I.E

id have

int a
int b
int c

and a number, say 189. I want break 189 into 1,8 and 9. So a=1 b=8 and c=9.

thanks for any help.
• 04-10-2008
Daved
Use math, specifically division and modulus. What's (189 / 100) &#37; 10 in integer math? It's 1. What's (189 / 10) % 10? It's 8. What's (189 / 1) % 10? It's 9.
• 04-10-2008
Dino
Quote:

Originally Posted by Daved
What's 189 % 100? It's 9.

89..
• 04-10-2008
iAmFedor
oh, so i cant just break it up you can in java?
• 04-10-2008
Dino
If "189" is a string, then yes you can parse it out. You implied 189 was an integer by calling it a number, (and because you didn't put double quotes around it), thus you got a math solution.
• 04-10-2008
Salem
Of course you can, and Daved explained how.
• 04-10-2008
iAmFedor
yeah it is an int i will be reading in.

so there is no classes to do this sort of thing? sorry im a noob at c++

in java i would just use the scanner and nextInt()

thanks for the replys.
• 04-10-2008
CornedBee
No, you wouldn't do that in Java, as nextInt() only works with strings (you said it's an int) and would grab the entire number.
• 04-10-2008
Raigne
Or the std::stringstream class would work.
• 04-10-2008
Daved
>> yeah it is an int i will be reading in
Or you might be able to read it in as a string and get each digit that way. To convert a single character to an int just subtract the zero character:
Code:

```string num("189"); int second_digit = num[1] - '0'; // second_digit is 8.```