need help with simple string to int code

Apr 8, 2017 at 7:49am
Hi, I have this small code where I first have an array of int from string, then for each int I multiply power of 10 based on next number, so if next number is 9, I multiply my current 117 with 10 and then add 9, so it becomes 1179. but the result of the addition is not right, the lower for loop logic seems right to eventually have a perfect int version of string.


long int convertint(string message)
{

long int converted=0;
int size=message.length();
int arr[size];
cout<<message.length()<<endl;
for (int i=0;i<size;i++)
{
arr[i]=static_cast<int>(message[i]);
cout<<arr[i]<<endl;
}
for (int i=0;i<size-1;i++)
{
int ugh;
int mul=1000;
if(i<size-1)
{
if(arr[i+1]<10)
mul=10;
else if(arr[i+1]<100)
mul=100;
}
cout<<converted<<endl;
ugh=arr[i]* mul;
converted+=ugh;
}
return converted;
}



my output is
please enter the message you want to convert
why
3
119
104
121
0
119000
223000


Last edited on Apr 9, 2017 at 6:54pm
Apr 8, 2017 at 8:58am
use std::stoi() to convert numeric std::string to int:
http://coliru.stacked-crooked.com/a/71a143e974ed12be
Apr 9, 2017 at 9:21am
Hi
the conversion is right the issue is when I am trying to have a string of numbers after all the procedures I am going through to have the internet version of string.
Apr 9, 2017 at 10:43am
kindly explain
the internet version of string.
???
Apr 9, 2017 at 6:51pm
the lower for loop is adding a variable "converted" with int of current int array multiplied with a power of 10 based on number of digit of next int of the array. what you mean by internet version of string? I am here for c++ help therefore I am talking about string in c++.
Apr 9, 2017 at 7:10pm
You have a short memory @llll - about 9.5 hours by the gap between your posts.
the conversion is right the issue is when I am trying to have a string of numbers after all the procedures I am going through to have the internet version of string.
what you mean by internet version of string?



You don't need the array arr[].

static_cast<>() doesn't do what you think it does. Just note that (a) characters are in successive sequence, including '0', '1', '2', ... and (b) they are represented by an integer in their collating sequence. Thus, irrespective of what their actual position is, you can get a corresponding integer digit by subtracting '0' (as below).

I assume this is what you want. As @gunnerfunner pointed out, there is a standard-library function std::stoi() which will do this for you if you prefer.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

long convertint( string message )
{
   long converted = 0;
   for ( int i = 0; i < message.size(); i++ )
   {
      int digit = message[i] - '0';
      converted = 10 * converted + digit;
   }
   return converted;
}


int main()
{
   string s;
   cout << "Input a string: ";
   getline( cin, s );
   cout << "The corresponding integer is ... wait for it ... " << convertint( s );
}
Last edited on Apr 9, 2017 at 7:11pm
Apr 9, 2017 at 8:15pm
hi
I believe my method is synonymous to yours because of ascii you can just cast, at least that's what I have learned in school and I have tried your the way I done both works, that's not the point, the point is, the int version of each letter is saved in index, how do I have a string of int out of the whole array
Apr 9, 2017 at 8:50pm
I believe my method is synonymous to yours

I beg to differ.

at least that's what I have learned in school

We obviously went to different schools.

Try the code below. static_cast<>() gives the position in the character-collating sequence - probably, but not necessarily, ASCII. That position in the sequence will be nowhere near any numerical value that might be portrayed when you print that character. On the other hand, since the number digits are in successive order in the character sequence, subtracting from this collating-sequence position the corresponding collating-sequence position for '0' gives the required decimal digit.


1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main()
{
   char c = '3';
   cout << "Character is " << c << endl;
   cout << "Using static_cast gives: " << static_cast<int>( c ) << endl;
   cout << "Using character difference from '0' gives: " << c - '0' << endl;
}


Character is 3
Using static_cast gives: 51
Using character difference from '0' gives: 3

Last edited on Apr 9, 2017 at 9:26pm
Topic archived. No new replies allowed.