How to convert strings into ints in C++?

Dec 2, 2017 at 1:54pm
Hello,
I'm trying to write a Sudoku program in C++ and I'm having trouble converting a string of both chars and ints into usable ints. What I'm trying to do is receive a set of coordinates (a-4, b-6, h-7, etc.) from the user and then use those values to find and edit squares on the sudoku board. Does anyone have any suggestions?
Dec 2, 2017 at 3:55pm
Hello mmayed,

there are several ways you could change a string or character to a number. It is hard to say what you might need without seeing your code.

To change a string like "123" to a number "stoi" usually works: num = stoi(str);.

To change a single character like "a" to a number I would use: row = static_cast<int>(ch - 'a');. Note the case of "a". Where if ch equals 'A' subtracting 'a' would give you a negative number and where ch equals 'a' and subtracting 'A' would give you a number higher than you want. Look at an ASCII table to see what I mean.

If you can post your code I can give you a better idea of what you need to do.

Hope that helps,

Andy
Topic archived. No new replies allowed.