Jump to content
  • Advertisement
Sign in to follow this  
dvail87

Is it possible to index integers in C++?

This topic is 4394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Also, see if you can optimize your modulus division (in code, not by the compiler [it'll teach you more]) by removing excess operations.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!