d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help On Converting String To Int
Add Reply New Topic New Poll
Member
Posts: 19,256
Joined: Mar 24 2008
Gold: 710.00
Oct 22 2014 06:38pm
Basically I have This Format from a input file. Im using the getline function to read get each line. Lets say the strings name is string, im basically using string[0] to Test if the first letter is a "D", if that test succeeds, them I basically need to get the value of string[7] to convert it into an int, then that number will go through further processing. So my problem is how would i convert the string[7] from a char to an int. And also in the case of Day04, where there is now a number in 2 digits, how will i be able to extract both string[7] and string[8] and convert them to the number 14.

ps. I suck at programming , so this is the method i came up with to get the data xD.


Vendor: Smith Levi
Product: Dank Cookies
Day01: 1
Day02: 5
Day03: 6
Day04: 14



tl;df version

What would be the simplest way to get the numerical values from these strings. ( the 1,5,6,14)

Member
Posts: 6,562
Joined: Oct 29 2007
Gold: 4.00
Oct 22 2014 07:07pm
is this in c or c++

You'll likely want to use this: http://www.cplusplus.com/reference/cstdlib/atoi/

This post was edited by Aimed_Shot on Oct 22 2014 07:09pm
Member
Posts: 19,256
Joined: Mar 24 2008
Gold: 710.00
Oct 22 2014 07:09pm
Quote (Aimed_Shot @ Oct 22 2014 09:07pm)
is this in c or c++


wow xD the most essential part of the problem just flew right past me

its c++

Member
Posts: 6,562
Joined: Oct 29 2007
Gold: 4.00
Oct 22 2014 07:12pm
Quote (Pino38 @ Oct 22 2014 07:09pm)
wow xD the most essential part of the problem just flew right past me

its c++


I think you could do...

Code
string num = str.substr(7); //will get the substring of str from position 7 to the end.

int value = atoi(num.c_str()); //should do it for you


make sure to include stdlib
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 22 2014 07:18pm
Quote (Aimed_Shot @ Oct 22 2014 09:12pm)
I think you could do...

Code
string num = str.substr(7);  //will get the substring of str from position 7 to the end.

int value = atoi(num.c_str()); //should do it for you


make sure to include stdlib


atoi is such a C thing to do. I'm pretty sure there is a integer method for strings like .to_i()

Edit:: I was wrong, although you can simply do this:

Code
string num = str.substr(7);
int number;

num >> number;


Pretty sure that works in C++ all be it, it wont be nowhere as fast as atoi.

This post was edited by AbDuCt on Oct 22 2014 07:39pm
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Oct 22 2014 07:34pm
e/ ignore

This post was edited by SelfTaught on Oct 22 2014 07:34pm
Member
Posts: 19,256
Joined: Mar 24 2008
Gold: 710.00
Oct 22 2014 07:38pm
Than
Quote (Aimed_Shot @ Oct 22 2014 09:12pm)
I think you could do...

Code
string num = str.substr(7);  //will get the substring of str from position 7 to the end.

int value = atoi(num.c_str()); //should do it for you


make sure to include stdlib


thank you very much sir :thumbsup:

ill try to integrate that into my code , and get it working. Thanks again!





Quote (AbDuCt @ Oct 22 2014 09:18pm)
atoi is such a C thing to do. I'm pretty sure there is a integer method for strings like .to_i()


I will try to look into that possibly when i got the time, but right now im going to use almed_shots method. im racing the clock xD

This post was edited by Pino38 on Oct 22 2014 07:40pm
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Oct 22 2014 09:03pm
There's std::stoi you should use in C++, but my suggestion is that you use something more robust, for example a regex.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll