Hello! I was just asked a question that I need some help with.
The question was given two strings that are numbers. Then add the numbers together (find the sum). Then to convert the answer back to a string.
The catch is I was asked to do this without casting.
I know how to solve the problem with casting. But I have no idea how to go about doing it without. I've tried browsing around and haven't come up with anything.
I keep forgetting about using stringstream for conversion from/to numeric values. The <string> functions are nice, but <sstream> is IMO better suited for simple conversions. Using stream I/O is consistent no matter what the source or destination.
If you can read a numeric value from std::cin, you can read a value from file or a stringstream in the same manner. The same with outputting a value.
it may be faster and simpler to do this yourself in a little loop that adds the numbers as strings.
that may not work if you have a school assignment to do it their way, but it seems like it would save a lot of complexity -- you just iterate backwards over the strings, carry and add... ?
I have it done would be the best way to go about it?
That's a good way to do it - but depending upon other factors would depend upon whether it is the best way. eg, do you need to know where the end of the converted string is? What if the string to be converted isn't a number, is performance a factor (use from_chars() [for VS2019]) etc etc.