Reading one digit at a time from a integer.

Pages: 12
Hello all,

I have look at 4 difference books and all seem to skip this part. I'm trying to reading in one digit at a time from an integer so that the individual number can be work with. For example, if the integer is 1458; I want to store 1,4,5, and 8 in int a,b,c and d. I believe in C, I would use fscan( &a,&b,&c,&d). How would I do this in c++? Thank you for you help.

respectfully,
Brandon
Thank you, but the link point to a C page. How would I do this in c++? Thank you for you help.

respectfully,
Brandon
I'm certain it works in C++ as well. Is there some reason you are avoiding the commands C++ inherited from C? If that's the case then what input stream are you using? File? Keyboard? There are better choices for each.
Yes, it's an assignment for a C++ class. The input going to be coming from a keyboard.
Actually, it isn't coming from a keyboard. It's coming from RAM unless you turn of line-buffering, in which case it's coming from a file on the hard disk.

Anyway, you want to read the integer one digit at a time? Do the digits need to go over 255 each? If not you can use std::cin.get() to read each digit one-by-one, typecast them to integers, and store them in an array.

Do you have to store the integer in one variable, or in an array (aka list)? If it's the former, then this gets fun, otherwise, it's easy.
Last edited on
Good point, I'll remember that for my mid-term. Thank you.
Is there some reason you are avoiding the commands C++ inherited from C?

There is a good reason to avoid stuff inherited from C. (type safety for instance).

Although you don't need any streaming stuff for this:

1
2
3
4
5
6
7
8
9
std::vector<int> getdigits(int num) {
    std::vector<int> res;
    do {
        res.push_back(num%10);
        num/=10;
    } while ( num > 0 );
    std::reverse(res.begin(), res.end());
    return res;
}


EDIT: Did I, or anybody else misunderstood the question?
For example, if the integer is 1458
Last edited on
We'll set aside the argument that 1458 IS one digit represented by four integers (read one thousand four hundred fiftey eight) and focus on your request.

Can we use cin.get()? They would be char not int but unless you are doing math there is no differance from what you have outlined.
I think you'd be best off using the get function. It returns a signed integer, and on an x86 CPU this means a range of -2,147,483,648 to +2,147,483,647 for each digit.

So what you would do, is have a loop. In the loop you use std::cin.get() to take an integral number and place it in the current position of an array or string. Then you, depending on whether you used an array of integers (a) or a string (b),
a. convert the array into a single integer
b. use a stringstream, which is easier by far.

1458 IS one digit represented by four integers

Wait, what? No it isn't! It's one integer represented by four digits! It's one-thousand, four-hundred and fifty-eight; not 1, 4, 5 and 8 conveniently placed closely together!

Edit: what OP wants is to get the single integer 1458 and then read it as four separate integers (actually, 1458 has no relevance to the topic at all and is being used as an example).
Last edited on
I think storing the integer in 4 difference variable would be best because this is the 3rd week of school and array wouldn't be cover until later on late in class toward the end. And the digits is going to be between 0-9. Basic the assignment ask for a 4 digits integer and than do something with each of the 4.
Last edited on
"No it isn't! It's one integer represented by four digits! It's one-thousand, four-hundred and fifty-eight; not 1, 4, 5 and 8 conveniently placed closely together!"

Thank you, this help a little.
Ah, then this is a trivial task.

Use a for loop:
1
2
for (int i = 0; i < number of integers to read; i++) // Replace "number of integers to read" with, uhh...
                                                     // the number of integers to read. 


Read the digits into four integers:
int digit_1 = std::cin.get(); // Grab an integer from standard input

Print the result, conveniently without spaces:
std::cout << "You entered: " << digit_1 << [...]

Put it together, make sure you understand it, and you're good to go... I just hope I didn't give you too much. But you don't seem like the typical "hi i hav dis homewurk kwestchun do it for me" that we get here... it's like a breath of fresh air... Trust me, I'm not usually this nice.
Last edited on
"Can we use cin.get()? They would be char not int but unless you are doing math there is no differance from what you have outlined."

I need to do math with each of the 4 digit
@ chrisname: Bah, I was trying to make a joke and got the wording switched you all know what I meant! i realise that 1458 has no relavance I was continuing with the example given.

If you need to do math then cin.get() for integers doesn't work, and if you are in week 3 skip the type conversion stuff. Can be cleaver and divide this by 1000 then 100 then 10 passing the remainder as we go to seperate out the individual integers?
Last edited on
chris,

Thank you so much. That's exactly what I needed. Just a figure pointing the way so that I can read and pu some more work in. Thank you.

And everyone else would reply: Thank you.

Respectfully,
Brandon
@Brandon,
Expand the code I've given you, and you can do what you like to the four integer numbers (I named them digit_1 but you should name them whatever seems appropriate to you... I can't stand when in tutorials people tell you what to save your files as, and what to call your variables...)

@Computergeek,
Ok, my mistake.
@Computergeek



Yes I do need to do math them. How do I pass the remainder?
"Can be cleaver and divide this by 1000 then 100 then 10 passing the remainder as we go to seperate out the individual integers?"

with the example number: 1458.

1458 % 1000 = 1.458 passing the reminder work to store the 1
1458 % 100 = 14.48 ?? how would I store the 4 ?


I'm sorry all, very new at this.
So just to make sure I understand this completely correct:

cin.get() <--- wouldn't be helpful because I do need to do math with each individual digit ?

Pages: 12