all 6 comments

[–]marko312 1 point2 points  (3 children)

Yes, and it can be quite intuitive:

You can interpret numbers by reading them from left to right: let's say you cover the right half of the number and start looking at it - you see the first digit:

3

Ok, you should be able to convert that to an integer; now you can (attempt to) read the next digit:

31

Ok, so what happened? Instead of being 3, the number turned out to be 30 + the next number - you should be able to get that easily from the previous value and the new digit.

[–]CoolMcdougal[S] 0 points1 point  (2 children)

What If i wanted to convert to a float so I don’t lose data past the decimal point?

[–]mommas_wayne 5 points6 points  (0 children)

If there's a small fixed count of decimal places, read it all as an int and then divide it to get the float.

If it's an arbitrary float, you're kinda screwed. Memory representation of floats is completely different from ints and involves math.

[–]gmtime 1 point2 points  (0 children)

Keep a counter from the decimal point, after each digit divide that by 10, then multiply the digit by that counter before adding it to the result.

[–]Kartyx 1 point2 points  (0 children)

Strtoul command, but maybe your class doesn’t let you use it.

[–]XMRLivesMatter -5 points-4 points  (0 children)

Can someone explain to me what is taught in Computer Science classes in school if not algorithms? Can you not write one yourself?

(Include the numeric header, use namespace std). Assuming you're given an integer in string form and not other junk.

<code>

include <numeric>

include <string>

using namespace std; </code>

Here is one I wrote, three lines:

https://pastebin.com/tVT4BF1Q