You can pursue this in the strategy that you are using, but make sure you realize the power of interpretting a string as an array of characters. A string as an array has each element as a separate character from that string. The characters in the string each have a corresponding array index depending on the order in which they appear in the string.
Here's an example that I just threw together and tested to demonstrate this point.
Side note: Keep in mind that I learned with C and may have some "bad" habits when it comes to sticking to C++ practices...I tend to use
C-based strategies or C code while utilizing some of the benefits of C++ when I see fit. But again that is probably based on habit and the way I learned programming. C++ was developed as "C with classes", so most of C++ utilizes the same functionality as C.
Now on to the example. . .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#include <string>
#include <iostream>
using namespace std;
int main ()
{
// Initialize a string
string myNum;
myNum = "1234"; // myNum now has the string "1234" stored in it
// Print out each character in myNum
cout << "myNum[0]: " << myNum[0] << endl;
cout << "myNum[1]: " << myNum[1] << endl;
cout << "myNum[2]: " << myNum[2] << endl;
cout << "myNum[3]: " << myNum[3] << endl << endl;
// Using the std::string class and capitalizing on objects...
int my_size; // Initialize variable to store the string's size
my_size = myNum.size(); // Use the .size() method, which returns the size of the string,
// not counting the null-character
cout << "my_size = " << my_size << endl;
system("pause");
return 0;
}
|
This simple main function is all you need to get a basic idea of storing a string, and then getting data from it, character-by-character.
This code produces the output:
1 2 3 4 5 6 7 8 9 10 11
|
myNum[0]: 1
myNum[1]: 2
myNum[2]: 3
myNum[3]: 4
my_size = 4
// Note the array indexes 0 - 3 actually confirm the concept
// of an nth element having index = [n - 1]. Size is 4, but
// the 4th element has index 3. (Hopefully I'm not beating this
// concept into the ground)
|
I think this idea will help in grabbing individual numbers, but there still lies an issue with knowing what power of 10 your digit represents...but wait! You know the index of each element! To save you some time, here's what I might do:
- Don't have the user include commas in their input to your program. For your purposes it might be better to focus on the process itself, and when you become more familiar with strings and arrays there are some cool tricks you could employ if you want to come back. I think you said previously there was something in the book you were learning from about employing the use of the commas or digit positions. This solution idea still uses digit positions, but in a different way. You can decide how you want to use this information.
- You're probably thinking "Oh it's all well and good knowing the index of the characters in the string, but how will that help me with getting digits and interpreting the
actual number they represent??!"
The answer is quite simple actually. Think in terms of foundationally what our number system
is, and particularly what each digit in a number actually represent.
This relates to my original comment that may have confused you and started this whole back-and-forth discussion. Our number system is decimal. Which means each position in which a number can reside represents a power of 10. Any number in that position is then
multiplied by that power of 10 to get back the actual number that the position represents.
Think of it this way...you have the number 122. What is this number actually? It is one 100, two 10's and two 2's all added together.
122 = (1 * 100) + (2 * 10) + (2 * 1) = 100 + 20 + 2
|
This idea is SO much easier understand with drawings and an in-person discussion, but I think you get the point.
One position to the left of the decimal place is the 0th position. Ten to the power of zero, 10^0, is 1. Hence the one's place, and any digit in that position (2 in our example) becomes the value of simply 2. For the second position, or the ten's place, at
index = 1, it is 2 * (10^1), which is 2*10 = 20. Your strings in your program have characters that represent digits, that are in certain digit positions, and have a unique index assigned to each character. Are you starting to see the power that arrays may have in this situation?? :)
Think through that a bit more, maybe try to look things up on it. Lessons on converting binary may help as well. Looking at numbers like this for Binary or Decimal are both fundamentally the same idea, but binary is base 2 instead of base 10. Numbers being represented by a sum of its digits and their position representing a power remains the same.
This is getting REALLY long, but I've still only hinted of how to implement this for your program. There's a lot of foundation for this idea though. Think indexes. You now have some user-input number as a string and need to determine each digit's value as an individual number. Arrays can be traversed backwards simply by using the index and the string's length or size methods to start from the
end, but in our case the end of the string is actually the beginning (or the one's place). I would try maybe implementing some code in addition to practicing and understanding my previously posted code in this post.
Start with something like:
cout << "myNum[my_size - 1] = " << myNum[my_size - 1] << endl;
You should find that this expression results in displaying the last element, remembering that the last element is NOT at myNum[my_size] but at [my_size - 1], and thus the one's place of the current string we're working with. Hopefully this gives you more to work on and I didn't simply confuse you more. Read this multiple times and try to let it sink in, as well as coding, writing things down, drawing things...sometimes it takes a bit of thought to apply new concepts like these to difficult problems. :)
**EDIT: Oh! And there are SO many ways to do this...someone's probably going to get on here and be like "do this, and then that and you're done. This ENIGMA guy is trying to teach you quantum mechanics while making ice cream from scratch..." or something.
I figured this was a cool time to learn a bit of stuff about how and why certain things work. This isn't just info to help you solve only this problem, but start you learning and thinking about how these concepts can shape you as a problem solver. If you'd rather do this the easy way and you feel that I'm wasting your time, let me know.