C++ is my first language and I've been reading the tutorial on this site too long without actually programming anything (got almost to the end without learning to use functions properly) so I'm going back to the basics - forgive me if I am asking stupid questions. Anyway I am trying to make a calculator that accepts a string like "9x53", breaks it down to it's components (first number, second number and x) and then does the math. My code uses a loop to assign each number or symbol to either the index of a string (next step is to turn it into a whole number) or a character to represent x, *, + etc. Anyway everything encodes with no errors but when I check to see that everything is assigned as it should be the array that holds the individual digits to be converted into whole numbers spits out "4953" when I input 15x9, it should spit out "159". Any ideas?
Your cout statement at the very end is printing out the ASCII values of '1' and '5.' Note that you made the same mistake on line 19. You are checking if the ASCII value of x[n] is between 0 and 9. A simple correction would be to use chars, i.e. '0' and '9.' Of course, you are still assigning the ASCII value of x[n] to y[n] and not the number you are expecting.
To convert characters/strings to numbers, use the stringstream object.
Thank you, that is a fantastic explanation. Not sure on how to use stringstream but I will review my notes and the tutorial section on it and I'm sure I'll be fine. If I may trouble you (or whomever) for a follow up answer, what determines whether the compiler/program sees an object as the ASCII code or as the value it points to?
Using arrays will only work with single digit numbers. In order to convert strings to int/float/double with stringstream, you would design your own algorithm to translate the number of digits into digits with a position (e.g. "100" to 1*100 + 0*10 + 0*1). In addition, you would need to figure out how to tell if the number in string form is negative, a decimal, an integer, in scientific notation, etc.
Just as std::string was designed as an easier way to work with an array of characters, you can use std::stringstream because it was created to make your life easier.
The purpose of the exercise is to learn how to code my own algorithms. I'm not to the point where I feel comfortable jumping into using libraries. I used getline but only because it was suggested in the tutorial. If this is a stupid approach feel free to let me know.