Converting strings to numbers

+3 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 because
the verification process uses mod 11 math to determine the check digit, which can range from 0
up to and including 10. 0 and 10 are NOT the same and the letter X is used to represent 10. So
if my ISBN is 052593944X or 1931499659, and I input the number into a string called ISBN. I need to multiply
ISBN[0] * 10 and ISBN[1] *9 and ISBN[2] * 8, etc. So in the example given "0" (ISBN[0]) needs to
become an integer 0 and "5" (ISBN[1]) needs to become an integer 5 and "2" needs to be an integer 2
right n down to the X. Any help would be appreciated.

Post a Reply

Replies

Oldest  Newest  Rating
+1 Mathias Frits Rørvik · January 23, 2015
Simple
std::string snum = "42";
int inum = std::stoi(snum);


make sure to pass the option "-std=c++11" to your compiler. Also this will only work with a C++11 compatible compiler.
0 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.
0 Val Almosnino · January 24, 2015
Here's the source code:

#include <iostream>
#include <string>

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;    
      }
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!
0 Bernard Pyc0d333 Parah · January 23, 2015
do you have any work you have tried on it yet?
0 Val Almosnino · January 23, 2015
@Bernard P - I have done a straight forward attack, I get odd output. 

#include <iostream>
#include <string>

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;
         }
    }
 
Output

ISBN y
480   0
441   1
400   2
357   3
312   4
265   5
216   6
165   7
112   8
57   9

Something's not quite right. the values should be:

0    0
9    1
16  2
21  3
24  4
25  5
24  6
21  7
16  8
9    9
0 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
0 Emmanuel Ntiamoah · January 23, 2015
I think that, this simple code would be kind of help to you.

#include <iostream> 
#include <sstream> 
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++

103,549 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator