atoi with ints and doubles - c++

Can I use atoi to convert a text input to a dialog box to a double?
I need to do a calculation on several double values that have been input using a dialog box. I only know of 'atoi' but is this for integers only?

Similar to atoi() there is double atof ( const char * str ) that you can use
Reference

Assuming Boost is an option, Boost.lexical_cast is a popular approach for converting to and from string representations of numerical values, e.g.:
char const s[] = "1.2345";
try
{
double d = boost::lexical_cast<double>(s);
...
}
catch (boost::bad_lexical_cast &)
{
...
}

Check the atoi, atol, strtol family :
http://www.fiveanddime.net/man-pages/strtol.3.html
http://www.kernel.org/doc/man-pages/online/pages/man3/strtol.3.html

If you are really using C++ (not just C) then you can parse text into floats using stl's std::istringstream.

You can use std::stringstream as:
std::stringstream ss(text);
double value;
if ( !( ss >> value ) )
{
std::cout << "error : text is not double" << std::endl;
}

Both atoi and atof are more or less broken; there's no way to do any error checking. In most cases, the simplest solution would be to use strtod:
char* endPtr;
errno = 0;
value = strtod( input, &endPtr );
if ( errno != 0 || *skipSpaces( endPtr ) != '\0' )
// Illegal input, conversion failed.
(I'm generally a fan of istringstream, but in this case, it seems overkill.)

Related

Convert octal string to decimal string in C++

What is the easiest way to convert string holding octal number into string holding decimal representation of the same number?
I could convert it into int value using strtol, then convert into string again using stringstream:
string oct_number("203");
// converts into integer
int value = strtol(oct_number.c_str(), NULL, 8);
// converts int to decimal string
stringstream ss;
ss << value;
string dec_number = ss.str();
But: is there any quicker way to do it?
I have a rather poor understanding of the stringstream class, am I missing something?
std::string result;
Try
{
result = std::to_string( std::stoi( oct_number, 0, 8 ) );
}
catch ( ... )
{
//...
}
In c++11 you can use string dec_number = to_string (value);.
Also you can use sprintf but you will need some buffer:
char buf[64];
sprintf(buf,"%d",value);
string dec_number = buf;

How to validate that there are only digits in a string?

I'm new to C++. I'm working on a project where I need to read mostly integers from the user through the console. In order to avoid someone entering non-digit characters I thought about reading the input as a string, checking there are only digits in it, and then converting it to an integer. I created a function since I need to check for integers several times:
bool isanInt(int *y){
string z;
int x;
getline(cin,z);
for (int n=0; n < z.length(); n++) {
if(!((z[n] >= '0' && z[n] <= '9') || z[n] == ' ') ){
cout << "That is not a valid input!" << endl;
return false;
}
}
istringstream convert(z); //converting the string to integer
convert >> x;
*y = x;
return true;
}
When I need the user to input an integer I'll call this function. But for some reason when I make a call tho this function the program doesn't wait for an input, it jumps immediately to the for-loop processing an empty string. Any thoughts? Thanks for your help.
There are many ways to test a string for only numeric characters. One is
bool is_digits(const std::string &str) {
return str.find_first_not_of("0123456789") == std::string::npos;
}
This would work:
#include <algorithm> // for std::all_of
#include <cctype> // for std::isdigit
bool all_digits(const std::string& s)
{
return std::all_of(s.begin(),
s.end(),
[](char c) { return std::isdigit(c); });
}
You can cast the string in a try/catch block so that if the cast fails you it would raise an exception and you can write whatever you want in the console.
For example:
try
{
int myNum = strtoint(myString);
}
catch (std::bad_cast& bc)
{
std::cerr << "Please insert only numbers "<< '\n';
}
Character-classification is a job typically delegated to the ctype facets of a locale. You're going to need a function that takes into account all 9 digits including the thousands separator and the radix point:
bool is_numeric_string(const std::string& str, std::locale loc = std::locale())
{
using ctype = std::ctype<char>;
using numpunct = std::numpunct<char>;
using traits_type = std::string::traits_type;
auto& ct_f = std::use_facet<ctype>(loc);
auto& np_f = std::use_facet<numpunct>(loc);
return std::all_of(str.begin(), str.end(), [&str, &ct_f, &np_f] (char c)
{
return ct_f.is(std::ctype_base::digit, c) || traits_type::eq(c, np_f.thousands_sep())
|| traits_type::eq(c, np_f.decimal_point());
});
}
Note that extra effort can go into making sure the thousands separator is not the first character.
try another way like cin.getline(str,sizeof(str)), and str here is char*. I think ur problem may be cause by other functions before calling this function. Maybe u can examine other parts of ur codes carefully. Breakpoints setting is recommended too.
Always use off-the-shelf functions. Never write alone.
I recommend
std::regex
Enjoy.

istringstream invalid error beginner

I have this piece of code :
if(flag == 0)
{
// converting string value to integer
istringstream(temp) >> value ;
value = (int) value ; // value is a
}
I am not sure if I am using the istringstream operator right . I want to convert the variable "value" to integer.
Compiler error : Invalid use of istringstream.
How should I fix it ?
After trying to fix with the first given answer . it's showing me the following error :
stoi was not declared in this scope
Is there a way we can work past it . The code i am using right now is :
int i = 0 ;
while(temp[i] != '\0')
{
if(temp[i] == '.')
{
flag = 1;
double value = stod(temp);
}
i++ ;
}
if(flag == 0)
{
// converting string value to integer
int value = stoi(temp) ;
}
Unless you really need to do otherwise, consider just using something like:
int value = std::stoi(temp);
If you must use a stringstream, you typically want to use it wrapped in a lexical_cast function:
int value = lexical_cast<int>(temp);
The code for that looks something like:
template <class T, class U>
T lexical_cast(U const &input) {
std::istringstream buffer(input);
T result;
buffer >> result;
return result;
}
As to how to imitation stoi if your don't have one, I'd use strtol as the starting point:
int stoi(const string &s, size_t *end = NULL, int base = 10) {
return static_cast<int>(strtol(s.c_str(), end, base);
}
Note that this is pretty much a quick and dirty imitation that doesn't really fulfill the requirements of stoi correctly at all. For example, it should really throw an exception if the input couldn't be converted at all (e.g., passing letters in base 10).
For double you can implement stod about the same way, but using strtod instead.
First of all, istringstream is not an operator. It is an input stream class to operate on strings.
You may do something like the following:
istringstream temp(value);
temp>> value;
cout << "value = " << value;
You can find a simple example of istringstream usage here: http://www.cplusplus.com/reference/sstream/istringstream/istringstream/

c++ Converting a std::string to int, double, etc WITHOUT using a char array

In C++ I have only seen this done by converting the string object into an array of characters. The tutorials with an array are a bit hard for me to understand. But I want to do the conversion without the array.
I do have an idea how to do it: the string is "1234". After that I convert this text to an integer this way:
if (symol4 == "4") int_var += 4 * 1;
if (symol3 == "3") int_var += 3 * 10;
if (symol3 == "2") int_var += 2 * 100;
if (symol3 == "1") int_var += 1 * 1000; //Don't worry, I'm familiar with cycles, this code is only for explaining my algorithm
I hope you can understand the idea.
But I don't know if this is the best way. I don't know if there is a library that has a function that allows me to do that (I won't be surprised if there is one).
I don't know if not using a char array is a good idea. But that's a different question that I'm going to ask later.
What's the best way to convert a string to an integer, double, etc WITHOUT using an array of characters.
boost::lexical_cast to the rescue: int result = boost::lexical_cast<int>(input)
If you don't want to rely on boost, you can use a stringstream, something like:
std::stringstream ss;
int result;
ss << input;
ss >> result;
but that's rather roundabout imo
And no don't use atoi - that function was flawed even back in C and it hasn't gotten better with time. It returns 0 when an error happened while parsing - which has the obvious problem how you distinguish an error from parsing the string "0".
I really can't get what your pasted code is about, but in C++ the best way to convert string to integer or float is to use stringstream.
const char* str = "10 20.5";
std::stringstream ss(str);
int x;
float y;
ss >> x >> y;
There is a function atoi which you can use. This converts it to a character array, but you don't have to do the math involved with indexing the array in a for loop.
#include <stdlib.h>
...
String number = "1234";
int value = atoi(number.c_str());
std::cout << number;
...
For the atoi nay sayers, hopefully he'll understand this >.>
#include <boost/lexical_cast.hpp>
try {
int x = boost::lexical_cast<int>( "123" );
} catch( boost::bad_lexical_cast const& ) {
std::cout << "Error: input string was not valid" << std::endl;
}
The best way is the most efficient way, I don't think you'll find a better alternative to this, or using a character array.
The standard string class already has a member function that gives you access to the internal character array, c_str(), so you can just pass this to one of the standard C library functions that parse integers, such as strtol():
string s = "1234";
long n = strtol(s.c_str(), 0, 10);
That's the simplest code if you already know the string is a valid integer and don't care about error checking. If you want full error checking you would do something like this:
char* end = 0;
errno = 0;
long n = strtol(s.c_str(), &end, 10);
if (end == 0 || *end == 0)
throw invalid_argument("Not a number");
else if (errno == ERANGE)
throw overflow_error("Number is out of range");
else if (errno != 0)
throw invalid_argument("Not a number");
Alternatively you could use C++ streams if you want to avoid C style character arrays completely (or rather, hide them completely inside the classes):
istringstream in(s);
int n;
in >> n;
You could also use boost::lexical_cast, which does basically the same thing.
I recommend Boost.Lexical_Cast
Or see the upcoming Boost.Conversion
Can also be achieved using Boost.Spirit, but is somewhat more complex
See "The String Formatters of Manor Farm" article by Herb Sutter.
You might want to look at the atoi function.

How do I check if a C++ string is an int?

When I use getline, I would input a bunch of strings or numbers, but I only want the while loop to output the "word" if it is not a number.
So is there any way to check if "word" is a number or not? I know I could use atoi() for
C-strings but how about for strings of the string class?
int main () {
stringstream ss (stringstream::in | stringstream::out);
string word;
string str;
getline(cin,str);
ss<<str;
while(ss>>word)
{
//if( )
cout<<word<<endl;
}
}
Another version...
Use strtol, wrapping it inside a simple function to hide its complexity :
inline bool isInteger(const std::string & s)
{
if(s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false;
char * p;
strtol(s.c_str(), &p, 10);
return (*p == 0);
}
Why strtol ?
As far as I love C++, sometimes the C API is the best answer as far as I am concerned:
using exceptions is overkill for a test that is authorized to fail
the temporary stream object creation by the lexical cast is overkill and over-inefficient when the C standard library has a little known dedicated function that does the job.
How does it work ?
strtol seems quite raw at first glance, so an explanation will make the code simpler to read :
strtol will parse the string, stopping at the first character that cannot be considered part of an integer. If you provide p (as I did above), it sets p right at this first non-integer character.
My reasoning is that if p is not set to the end of the string (the 0 character), then there is a non-integer character in the string s, meaning s is not a correct integer.
The first tests are there to eliminate corner cases (leading spaces, empty string, etc.).
This function should be, of course, customized to your needs (are leading spaces an error? etc.).
Sources :
See the description of strtol at: http://en.cppreference.com/w/cpp/string/byte/strtol.
See, too, the description of strtol's sister functions (strtod, strtoul, etc.).
The accepted answer will give a false positive if the input is a number plus text, because "stol" will convert the firsts digits and ignore the rest.
I like the following version the most, since it's a nice one-liner that doesn't need to define a function and you can just copy and paste wherever you need it.
#include <string>
...
std::string s;
bool has_only_digits = (s.find_first_not_of( "0123456789" ) == std::string::npos);
EDIT: if you like this implementation but you do want to use it as a function, then this should do:
bool has_only_digits(const string s){
return s.find_first_not_of( "0123456789" ) == string::npos;
}
You might try boost::lexical_cast. It throws an bad_lexical_cast exception if it fails.
In your case:
int number;
try
{
number = boost::lexical_cast<int>(word);
}
catch(boost::bad_lexical_cast& e)
{
std::cout << word << "isn't a number" << std::endl;
}
If you're just checking if word is a number, that's not too hard:
#include <ctype.h>
...
string word;
bool isNumber = true;
for(string::const_iterator k = word.begin(); k != word.end(); ++k)
isNumber &&= isdigit(*k);
Optimize as desired.
Use the all-powerful C stdio/string functions:
int dummy_int;
int scan_value = std::sscanf( some_string.c_str(), "%d", &dummy_int);
if (scan_value == 0)
// does not start with integer
else
// starts with integer
You can use boost::lexical_cast, as suggested, but if you have any prior knowledge about the strings (i.e. that if a string contains an integer literal it won't have any leading space, or that integers are never written with exponents), then rolling your own function should be both more efficient, and not particularly difficult.
Ok, the way I see it you have 3 options.
1: If you simply wish to check whether the number is an integer, and don't care about converting it, but simply wish to keep it as a string and don't care about potential overflows, checking whether it matches a regex for an integer would be ideal here.
2: You can use boost::lexical_cast and then catch a potential boost::bad_lexical_cast exception to see if the conversion failed. This would work well if you can use boost and if failing the conversion is an exceptional condition.
3: Roll your own function similar to lexical_cast that checks the conversion and returns true/false depending on whether it's successful or not. This would work in case 1 & 2 doesn't fit your requirements.
Here is another solution.
try
{
(void) std::stoi(myString); //cast to void to ignore the return value
//Success! myString contained an integer
}
catch (const std::logic_error &e)
{
//Failure! myString did not contain an integer
}
Since C++11 you can make use of std::all_of and ::isdigit:
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string_view>
int main([[maybe_unused]] int argc, [[maybe_unused]] char *argv[])
{
auto isInt = [](std::string_view str) -> bool {
return std::all_of(str.cbegin(), str.cend(), ::isdigit);
};
for(auto &test : {"abc", "123abc", "123.0", "+123", "-123", "123"}) {
std::cout << "Is '" << test << "' numeric? "
<< (isInt(test) ? "true" : "false") << std::endl;
}
return 0;
}
Check out the result with Godbolt.
template <typename T>
const T to(const string& sval)
{
T val;
stringstream ss;
ss << sval;
ss >> val;
if(ss.fail())
throw runtime_error((string)typeid(T).name() + " type wanted: " + sval);
return val;
}
And then you can use it like that:
double d = to<double>("4.3");
or
int i = to<int>("4123");
I have modified paercebal's method to meet my needs:
typedef std::string String;
bool isInt(const String& s, int base){
if(s.empty() || std::isspace(s[0])) return false ;
char * p ;
strtol(s.c_str(), &p, base) ;
return (*p == 0) ;
}
bool isPositiveInt(const String& s, int base){
if(s.empty() || std::isspace(s[0]) || s[0]=='-') return false ;
char * p ;
strtol(s.c_str(), &p, base) ;
return (*p == 0) ;
}
bool isNegativeInt(const String& s, int base){
if(s.empty() || std::isspace(s[0]) || s[0]!='-') return false ;
char * p ;
strtol(s.c_str(), &p, base) ;
return (*p == 0) ;
}
Note:
You can check for various bases (binary, oct, hex and others)
Make sure you don't pass 1, negative value or value >36 as base.
If you pass 0 as the base, it will auto detect the base i.e for a string starting with 0x will be treated as hex and string starting with 0 will be treated as oct. The characters are case-insensitive.
Any white space in string will make it return false.