# Converting strings to numbers

 Val Almosnino · January 23, 2015 I'm looking for a way to convert numeric data which is entered as a string into actual numbers.I am trying to verify that a 10 digit ISBN is correct. The data has to be entered as a string becausethe verification process uses mod 11 math to determine the check digit, which can range from 0up to and including 10. 0 and 10 are NOT the same and the letter X is used to represent 10. Soif my ISBN is 052593944X or 1931499659, and I input the number into a string called ISBN. I need to multiplyISBN[0] * 10 and ISBN[1] *9 and ISBN[2] * 8, etc. So in the example given "0" (ISBN[0]) needs tobecome an integer 0 and "5" (ISBN[1]) needs to become an integer 5 and "2" needs to be an integer 2right n down to the X. Any help would be appreciated.

## Replies

 Val Almosnino · January 23, 2015 Thanks Emmanuel, I'll give that a try in Code Blocks because the stoi command did not even though I told the co.piler to use C++ 11. I did get stoi to work with dev5? compiler. You. can't do it the way I asked Matthias because the number generated by the string conversion is to big to be an integer. However by using a for loop and passing each element to a variable and then doing a stoi th the variable worked just fine. Ill post the source code when I get home and can access my puter. Val Almosnino · January 24, 2015 Here's the source code:#include #include using namespace std;int choice = 10;float y, sum = 0.0;string ISBN, Assistisbn;int main(){    y=0;    ISBN = "9876543210";        for(int x=0;x<10;x++, choice--)        {          Assistisbn=ISBN[x];           y= stoi(Assistisbn) * choice;           sum +=y;           cout << y << "     " << sum << endl;            y=0;            }          return 0;           } Val Almosnino · January 24, 2015 Thank you Krootusha. I like code blocks and my version doesn't handle stoi even though I have told the compiler to use c++11. If I change y to char and add z as an int, I can do z=y-48 *choice but I still have to text for x/X and I think I was realizing that a switch was the answer. Thanks again! Bernard Pyc0d333 Parah · January 23, 2015 do you have any work you have tried on it yet? Val Almosnino · January 23, 2015 @Bernard P - I have done a straight forward attack, I get odd output. #include #include using namespace std;int y = 0;string ISBN = "9876543210";int main()                 {        for(int x=9; x > -1;x--)        {            y=0;            y =ISBN[x] * (x+1);            cout << y << " " << ISBN[x] << endl;         }    }  OutputISBN y480   0441   1400   2357   3312   4265   5216   6165   7112   857   9Something's not quite right. the values should be:0    09    116  221  324  425  524  621  716  89    9 Val Almosnino · January 23, 2015 Hi Mathias,Using your example, then I could change inum's declaration to something like:int inum = (std::stoi(snum[0]*10) +(std::stoi(snum[2] * 9); //? (question mark not part of the c++ statement but finishes the question)I really need to be able to access each element of the string.Thanks,Val Emmanuel Ntiamoah · January 23, 2015 I think that, this simple code would be kind of help to you.#include  #include  using namespace std; int main (){          string input;            int converted;            getline(cin, input);            stringstream(input)>>converted;                       //trying to use the converted as a number in addition          converted +=3;           cout<< converted;          return 0; }Thanks I hope it helped
• 1

## C++

129,920 followers