Sign in to follow this  
dvail87

Is it possible to index integers in C++?

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
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
I made some improvements, I hope, to the program I did before and just for the heck of it I thought I'd post what changes I made.
(1)Got rid of my loose numbers by turning them into constants,
(2)Inserted an if-else statement to make sure the program recieved the right type of data,
(3)Used a while loop because I was sick of having to click run all the time
(4)Tried to improve the math portion like deadimp suggested
(5)Took out the static_cast since, as kylotan said, they weren't necessary

#include<iostream>

using namespace std;

int main()
{
int userNum;

const int INT_1000 = 1000;
const int INT_100 = 100;
const int INT_10 = 10;

char again = 'y';
while(again == 'y')
{
cout << "Please enter a positive four digit integer: ";
cin >> userNum;

if(userNum >= 1000 && userNum <= 9999)
{
cout << userNum/INT_1000 << endl;
cout << (userNum %= INT_1000)/INT_100 << endl;
cout << (userNum %= INT_100)/INT_10 << endl;
cout << userNum % INT_10 << endl;
}
else
cout << "Number is invalid.\n" << endl;

cout << "Do you want to try again? (y/n): ";
cin >> again;
cout << endl;
}

system("PAUSE");

return 0;
}

Share this post


Link to post
Share on other sites
INT_1000 is an example of how not to get rid of loose constants.

:)

Here are some hints:
Write "convert int to std::string in base 10" as a function.
Write "convert base 10 std::string to int" as a function.
Write "get max and min ints for an N diget number in base 10" as a function (or two functions).

If you are keen, you could even write the above in a generic base.

This will make the only special numbers in your code "number of digets" and "base of digets" -- both of these numbers make sense without understanding the details of your algorithm, and if you change either (to reasonable values) your program continues to work.


...

Instead of "fakeing" user input by setting again to be 'y', try having a bool loop variable called something like "keep_getting_input_from_user". Or just loop while(true) and explicity break; out of the loop when the user asks you to quit.

Faking use input is a lie. There isn't any reason to lie to your program in this case -- it doesn't make your program any tighter, easier to read, etc. And lieing makes baby jesus cry. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by dvail87

char again = 'y';
while(again == 'y')
{
...
}


Lookup 'do', it should work wonders here.

Your second draft is better than your first, although Yakk is correct to suggest that your constants are no better than what you had before.

Here's a thought for the next step, should you choose to keep plugging away at it: what happens if you reverse the process? Print '8024' as '4208.' Can you think of any simplifications for this case?

CM

*edit: removed unintentional pun.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this