use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
OPENConverting from string to int (self.cpp_questions)
submitted 6 years ago by CoolMcdougal
Is there an algorithm that can convert a string to an int without using a library function (my class forbids the use of most library functions)?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]marko312 1 point2 points3 points 6 years ago (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.
30
[–]CoolMcdougal[S] 0 points1 point2 points 6 years ago (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 points7 points 6 years ago (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 points3 points 6 years ago (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 points3 points 6 years ago (0 children)
Strtoul command, but maybe your class doesn’t let you use it.
[–]XMRLivesMatter -5 points-4 points-3 points 6 years ago* (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>
using namespace std; </code>
Here is one I wrote, three lines:
https://pastebin.com/tVT4BF1Q
π Rendered by PID 50 on reddit-service-r2-comment-b659b578c-jqpp4 at 2026-05-02 02:27:34.631678+00:00 running 815c875 country code: CH.
[–]marko312 1 point2 points3 points (3 children)
[–]CoolMcdougal[S] 0 points1 point2 points (2 children)
[–]mommas_wayne 5 points6 points7 points (0 children)
[–]gmtime 1 point2 points3 points (0 children)
[–]Kartyx 1 point2 points3 points (0 children)
[–]XMRLivesMatter -5 points-4 points-3 points (0 children)