c++ long long int is not enough?? errors - c++

I am working in c++. I have a string that contains the following number
std::string s= "8133522648";
I want to convert this number in
long long int nr;
I did: nr=atoll(s.c_str()). The result is: -456410944. How to solve this error? Thanks
Edit:
In fact I have:
const char* str="8133523648";
I have to convert it into long long int nr=8133523648
Thanks for help! Appreciate!

use int64_t instead of long long. which is defined in stdint.h
If you rely on boost you can use
std::string s= "8133522648";
int64_t nr = boost::lexical_cast<int64_t, std::string>(s);

It can be done in better way as follows:
#include <sstream>
stringstream sstr;
sstr << "8133522648";
long long nr;
sstr >> nr;
Don't use atoll() as it is not defined by C++ standard. Some compiler may implement it while others don't. Also,
std::string s = 8133522648;
doesn't mean
std::string s = "8133522648";
which was probably what you wanted.

Below code is working fine:
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main() {
std::string s= "8133522648";
long long int nr = atoll(s.c_str());
cout << nr;
}

Related

"no operator >> matches these operands"

I'm a complete noob at C++, and the first problem I am encountering is the following:
no operator >> matches these operands
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "hello world!";
cin >> "hello world!";
}
std::cin needs to write to a variable, but you are passing it a const char[13] string literal instead.
You need to pass it something like a std::string instead:
std::string str;
std::cin >> str;
P.S. This is a good time to a) read compiler messages, b) avoid using namespace std; globally, c) get a good C++ book.

How do I add command lines as numeric data without using the atof function?

Is there a way I can avoid using the atof function? How can I convert a string to float?
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
double x = 0;
for(int i=0; i<argc; i++)
x += atof(argv[i]);
cout<<x<<endl;
return 0;
}
You can use stringstream.
#include <iostream>
#include <sstream>
using namespace std;
int main () {
int val;
stringstream ss (stringstream::in | stringstream::out);
ss << "120";
ss >> val;
return 0;
}
For converting a string to a floating-point number in C++, it's now (since the standardization of C++11) advisable to use one of std::stof, std::stod and std::strold (where these functions had been added to the C++ standard library):
std::string s = "120.0";
float f = std::stof(s);
double d = std::stod(s);
long double ld = std::stold(s);
The primary reason to prefer these functions over those in the C standard library is safety:
they don't operate on raw pointers but string objects (that also leads to a minor improvement in convenience and readability: one does not have to call c_str() over and over again when working with std::strings); and
they don't exhibit undefined behavior when the conversion is impossible (they predictably throw well-documented exceptions instead).
You can use boost::lexical_cast to convert between written and numeric types in a way that's very idiomatic for C++.
#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
using namespace std;
int main(int argc, char* argv[])
{
double x = 0;
for(int i=1; i<argc; i++)
x += boost::lexical_cast<float>(argv[i]);
cout<<x<<endl;
return 0;
}

strtoull was not declared in this scope while converting?

I am working with C++ in eclipse CDT and I am trying to convert string to uint64_t by using strtoull but everytime I get below error message -
..\src\HelloTest.cpp:39:42: error: strtoull was not declared in this scope
Below is my C++ example
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {
string str = "1234567";
uint64_t hashing = strtoull(str, 0, 0);
cout << hashing << endl;
}
return 0;
}
Is there anything wrong I am doing?
Why your solution doesn't work has already been pointed out by others. But there hasn't been a good alternative suggested yet.
Try this for C++03 strtoull usage instead:
#include <string>
#include <cstdlib>
int main()
{
std::string str = "1234";
// Using NULL for second parameter makes the call easier,
// but reduces your chances to recover from error. Check
// the docs for details.
unsigned long long ul = std::strtoull( str.c_str(), NULL, 0 );
}
Or, since C++11, do it directly from std::string via stoull (which is just a wrapper for the above, but saves on one include and one function call in your code):
#include <string>
int main()
{
std::string str = "1234";
// See comment above.
unsigned long long ul = std::stoull( str, nullptr, 0 );
}
Never use char[] or pointers if you have a working alternative. The dark side of C++, they are. Quicker, easier, more seductive. If once you start down the dark path, forever will it dominate your destiny, consume you it will. ;-)
the structure for strtoull is: strtoull(const char *, char * *, int)
You have given it a std::string as pointed out by #juanchopanza
This is the solution I came up with is
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
char str[] = "1234567";
unsigned long long ul;
char* new_pos;
charDoublePointer = 0;
ul = strtoull(str, &new_pos, 0);
cout << ul << endl;
return 0;
}
The output I got was: 1234567
Straight from the eclipse console.
Also at the end of your program you have return 0 out of scope with an extra curly brace.

How to convert an integer to a string with a fixed number of digits in c++?

For examples, with 4 digits, convert 0 to "0000"; and 12 to "0012".
Any good way in c++?
Sorry not making it clear, my compiler doesn't support snprintf, and I want a function like
std::string ToString(int value, int digitsCount);
char buf[5];
snprintf(buf, sizeof(buf), "%04d", intVal);
Maybe something like this
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
string ToString(int value,int digitsCount)
{
ostringstream os;
os<<setfill('0')<<setw(digitsCount)<<value;
return os.str();
}
int main()
{
cout<<ToString(0,4)<<endl;
cout<<ToString(12,4)<<endl;
}

Convert std::string to MSVC specific __int64

May I know how I can convert std::string, to MSVC specific __int64?
_atoi64, _atoi64_l, _wtoi64, _wtoi64_l
std::string str = "1234";
__int64 v =_atoi64(str.c_str());
See also this link (although it is for linux/unix): Why doesn't C++ reimplement C standard functions with C++ elements/style?
Here's one way:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string s( "1234567890987654321");
stringstream strm( s);
__int64 x;
strm >> x;
cout << x;
}
__int64, while an extension, is still just a numeric type. Use whichever method you would typically use.
Boost lexical cast is my favorite. It pretty much wraps up Michaels answer in an easy to use form:
__int64 x = boost::lexical_cast<__int64>("3473472936");
If you can't use boost, you can still do a pretty good job of making a simple version. Here's an implementation I wrote for another answer:
template <typename R>
const R lexical_cast(const std::string& s)
{
std::stringstream ss(s);
R result;
if ((ss >> result).fail() || !(ss >> std::ws).eof())
{
throw std::bad_cast();
}
return result;
}
It does some extras, like checking for trailing characters. ("123125asd" would fail). If the cast cannot be made, bad_cast is thrown. (Similar to boost.)
Also, if you have access to boost, you can avoid the need to use the MSVC-specific __int64 extension with:
#include <boost/cstdint.hpp>
typedef boost::int64_t int64;
To get int64 on any platform that provides it, without changing your code.