Convert C++ string variable to long - c++

I have a variable:
string item;
It gets initialized at run-time. I need to convert it to long. How to do it? I have tried atol() and strtol() but I always get following error for strtol() and atol() respectively:
cannot convert 'std::string' to 'const char*' for argument '1' to 'long int strtol(const char*, char**, int)'
cannot convert 'std::string' to 'const char*' for argument '1' to 'long int atol(const char*)'

c++11:
long l = std::stol(item);
http://en.cppreference.com/w/cpp/string/basic_string/stol
C++98:
char * pEnd;.
long l = std::strtol(item.c_str(),&pEnd,10);
http://en.cppreference.com/w/cpp/string/byte/strtol

Try like this:
long i = atol(item.c_str());

Use a string stream.
#include <sstream>
// code...
std::string text;
std::stringstream buffer(text);
long var;
buffer >> var;

Use std::stol < characters to fill space >

If you don't have access to C++11, and you can use the boost library, you can consider this option:
long l = boost::lexical_cast< long >( item );

Related

invalid operands of types 'float' and 'const c'

I just started learning C++ and I need some help.
targetDistance is a float variable and I want to add a string "a" to it, is it possible?
I tried this:
targetDistance = targetDistance <<"a"
It gives me this error:
invalid operands of types 'float' and 'const c'
If targetDistance is a float, you need to convert it to a string before you can concatenate it with another string. For example:
auto result = std::to_string(targetDistance) + "a";
The idea is to convert the float variable(in this case targetDistance) into a string.
Make sure that you have included this header:
#include <string>
The code below:
string s; //to store our float variable
s= to_string( targetDistance ); //to_string function converts into string
s= s+ "a";
Here is just the short version of it:
string s = to_string( targetDistance ) + "a" ;

How to convert an integer declared as a string to an int type?

I tried this solution but it doesn't work.
string mystring = "77";
int mynum = atoi(mystring);
error: cannot convert ‘std::string {aka std::basic_string}’ to ‘const char*’ for argument ‘1’ to ‘int atoi(const char*)’
int mynum = atoi(mystring);
If you use std::string you may use atoi with std::string::c_str() method, or directly by using std::stoi:
int val = std::stoi(mystring);
std::stoi reference
With boost you may use boost::lexical_cast:
int val = boost::lexical_cast<int>(mystring);
boost::lexical_cast reference
Note, that atoi is a history already, mainly because it is old C standard library function. Modern code is using lexical_cast or stoi.
Try this:
string mystring = "77";
int mynum = atoi(mystring.c_str());
or in C++11:
string mystring = "77";
int mynum = stoi(mystring);
Use this
int mynum = atoi(mystring.c_str());

Error in printing all substrings of a string

This is in reference to the following answer by Synxis.
https://codereview.stackexchange.com/questions/18684/find-all-substrings-interview-query-in-c/18715#18715
Suppose, I have to print all substrings of the string "cbaa". To do this, I have to invoke the method like this:
findAllSubstrings2("cbaa");
If I take a string from user, and do the following:
string s;
cin>>s;
findAllSubstrings2(s);
it gives the following error:
[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'void findAllSubstrings2(const char*)'
Why does this happen?
As the error message says the parameter of function findAllSubstrings2 is declared as having type const char * while you are trying to pass an argument of type std::string
string s;
//...
findAllSubstrings2(s);
You should use member function c_str or data (starting from C++ 11) of class std::string. For example
findAllSubstrings2(s.c_str());
you using string, in function is char try to use char[] s;
use c_str() method in string class when passing the argument
string s;
cin>>s;
findAllSubstrings2(s.c_str());
You probably should change the type of the parameter of the function. Somethink like:
void findAllSubstrings2(string s){
//... function implementation...
}

atoi and string array

I have a string array and an integer array. I want to convert the elements of string array to integer and then store them in the integer array. I wrote this code :
string yuzy[360];
int yuza[360];
for(int x = 0;x<360;x++)
{
if(yuzy[x].empty() == false)
{
yuza[x]=atoi(yuzy[x]);
cout<<yuza[x]<<endl;
}
else
continue;
}
this piece of code gives this error:
error: cannot convert 'std::string {aka std::basic_string}' to 'const char*' for argument '1' to 'int atoi(const char*)'
When I write the content of the string (-75dbm) in atoi function it works fine. But when I write (yuzy[x]), I get the error. How can I make atoi works well with string array?
Thanks.
atoi() takes C strings (char pointers) and not C++ string objects. Use
atoi(yuzy[x].c_str());
instead.
As an alternative to atoi, you could use std::stoi and related functions, if you have C++11 support.
yuza[x] = std::stoi(yuzy[x]);
atoi accept a c-style string as parametter, so, you could use atoi(yuzy[x].c_str());

C++: how do I convert hex char to unsigned char?

I have a method that takes unsigned chars, but I want to pass it 0x01.
I know I can represent hex chars in strings by going "\x01"...
but that is still a signed char.
EDIT:
some code:
kennys_hash((unsigned char const *)"\x00"); // the method call
the error:
src/main.cpp:88: error: invalid conversion from ‘const unsigned char*’ to ‘unsigned char’
src/main.cpp:88: error: initializing argument 1 of ‘unsigned char kennys_hash(unsigned char)’
the method header:
unsigned char kennys_hash(unsigned char out)
ALso, when the cast is just to unsigned char, I get this error:
src/main.cpp:88: error: cast from ‘const char*’ to ‘unsigned char’ loses precision
0x01 is the same as 1, which is positive, and thus it doesn't matter if it's considered to be signed or unsigned, the value is the same.
If you want to have an unsigned type on the literal, use 0x01u.
Note that "\x00" is an string constant (read: array of char), and not a single character constant.
Use single quotes: '\x00' is a character constant.
The type might be char, but that is automatically converted to unsigned char when needed. Some compilers might issue a warning though.
You can use boost::lexical_cast:
unsigned char bar = boost::lexical_cast<unsigned char>( "\x71" );
void foo(unsigned char const * u);
...
foo( (unsigned char const *) "\x01");
You can pass it an array of unsigned chars, like this:
unsigned char data[5] = {1, 2, 3, 4, 5};
func_that_takes_unsigned_chars(data, sizeof data);
I bet you can't send it \x-01. Therefore you are sending it an unsigned int.
You can always test you your input before you send it to the func. If inp < 0 then send it back.