Is it possible to index integers in C++?

Started by
11 comments, last by Conner McCloud 17 years, 7 months ago
Is it possible to index integers in C++? I know it's possible to index strings by putting the index number in [] like name = John and then name[0] to call up J. But is there anyway to do this with numbers? The reason I'm asking is in my intro to C++ class we have a problem that states: Write a program that prompts the user to input a positive 4 digit integer and then ouputs the digits of the number, one digit per line. Ex. 1234 1 2 3 4 This is only from Ch.2 of the book so I doubt its anything complicated. The only thing in that chapter is basic input and output, arithmetic expressions, type converting, escape sequences, data types, reserved words and identifiers. It's just when it talked about one digit per line my first thought was indexing. Any help is appreciated.
Advertisement
Look into sprintf, or stringstream for a more C++ way of doing things. Since you know its a 4 digit number, you can also employ integer division to isolate the digits.

I don't want to say much more since its a school project.

EDIT: King of Men has a good point... you already have a string if you read the input into a string.
The simplest way would be to completely ignore that you are dealing with a number, and just treat "1234" as a string. This may get you into a little trouble if your teacher requires that you do error-checking, though; so think about how you could ensure that a four-character string is a number.
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
Thanks for the replies

smitty: I've never heard of sprintf b4, I'll have to look that up later. I get what you were implying about just dividing the number to isolate different digits but, call me slow, i haven't been able to isolate the digits past the first one. Take 9876 for example. I can do 9876/1000 = 9.876 and convert my answer to an integer but passed the first digit I'm lost. I'll play around some more with the numbers and see what I can come up with.

King of Men:
#include<iostream>
#include<string>

using namespace std;

int main()
{
string userNumber;

cout << "Please enter a positive four digit number: ";

cin >> userNumber;

cout << userNumber[0] << endl;
cout << userNumber[1] << endl;
cout << userNumber[2] << endl;
cout << userNumber[3] << endl;

I did what you suggested and it works, problem is it works for numbers and digits. If I go that way I've got to come up with a way to make sure the program only looks at digits...i'll think on it some more.
Quote:Original post by dvail87
Thanks for the replies

smitty: I've never heard of sprintf b4, I'll have to look that up later. I get what you were implying about just dividing the number to isolate different digits but, call me slow, i haven't been able to isolate the digits past the first one. Take 9876 for example. I can do 9876/1000 = 9.876 and convert my answer to an integer but passed the first digit I'm lost. I'll play around some more with the numbers and see what I can come up with.

Look into the % operator. Also consider that, once you have a certain digit, you can remove it so that it won't affect later calculations. How you remove the digit depends on how you found it in the first place.

CM
I got it! I forgot all about the modulus operator. Thanks McCloud. This is what I ended up doing:
#include<iostream>

using namespace std;

int main()
{
int userNumber;

cout << "Please enter a positive four digit number: ";
cin >> userNumber;

cout << static_cast<int>(userNumber/1000) << endl;
cout << static_cast<int>(userNumber%1000)/100 << endl;
cout << static_cast<int>(((userNumber%1000)%100)/10) << endl;
cout << static_cast<int>(((userNumber%1000)%100)%10) << endl;

system("PAUSE");

return 0;
}

Thanks again to those who replied. I was also able to do it with a string as posted above but this is probably more in line with what we've gone over in class.
Quote:Original post by dvail87
I got it! I forgot all about the modulus operator. Thanks McCloud. This is what I ended up doing:
#include<iostream>

using namespace std;

int main()
{
int userNumber;

cout << "Please enter a positive four digit number: ";
cin >> userNumber;

cout << static_cast<int>(userNumber/1000) << endl;
cout << static_cast<int>(userNumber%1000)/100 << endl;
cout << static_cast<int>(((userNumber%1000)%100)/10) << endl;
cout << static_cast<int>(((userNumber%1000)%100)%10) << endl;

system("PAUSE");

return 0;
}

Thanks again to those who replied. I was also able to do it with a string as posted above but this is probably more in line with what we've gone over in class.

Once you get a little farther in [especially when you get to loops, and you have more experience with variables], it would probably be a good idea to revisit this problem and try and make a more general solution. It might end up a later assignment, but even if not it'll be a good little excersize to test yourself.

CM
dvail87,

Just so you know, sprintf and stringstream allow you to "output" to a string. So you could make a string out of the number and iterate through the digits.

Just remember them for later, because they are incredibly handy, and you will certainly need them at a later date.
You should also take out the static_casts. Dividing an integer gives you an integer.
Also, see if you can optimize your modulus division (in code, not by the compiler [it'll teach you more]) by removing excess operations.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire

This topic is closed to new replies.

Advertisement