Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert string to integer

Posted on 2014-04-07
10
Medium Priority
?
240 Views
Last Modified: 2015-05-18
Is there any C++ function which will convert wide char string to int. If wide char string is double "100.01" then function should return error. I can use isDigit and add own logic to validate this but wanted to check whether there is any in build function for the same. I am using VC++ on windows.
0
Comment
Question by:RahulKajave
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 1000 total points
ID: 39984038
atoi / atof should have a wide string sibling
0
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 39984523
'strtol()' is the 'standard', next to 'atoi()' that qlemo already mentioned, with the 1st allowing better error checking. See http://en.cppreference.com/w/cpp/string/byte/strtol

E.g.

#include <iostream>
#include <cstdlib>
using namespace std;

char* str = "100.01";
char* cnv_end;

long l = strtol(str,&cnv_end,10);

if (cnv_end != NULL) {

  cout << "Conversion failed, invalid character at " << cnv_end << end;

} else {

  cout << "Conversion succeded, value is " << l << endl;
}

Open in new window


Alternatively, you can use a 'std::stringstream' for that like

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

char* str = "100.01";
int n;

stringstream ss;

ss << str;

ss >> n; // try to extract an integer

if (ss.fail()) {

  cout << "Conversion failed" << end;

} else {

  cout << "Conversion succeded, value is " << n << endl;
}

Open in new window

0
 

Author Comment

by:RahulKajave
ID: 39985062
What about localization? Will strtol and stringstream handle localization properly. What if decimal separtor is "," instead of ".".
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 86

Expert Comment

by:jkr
ID: 39985652
Yup. In both cases the constraint is 'non-numeric character', regardless of the localization.
0
 
LVL 86

Expert Comment

by:jkr
ID: 39990301
Do you still have issues with this or have you just not thought of closing the question?
0
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 40020059
// What about localization?
You can write a function to replace the special characters(, with .) and then pass through stringstream to get the double/int value if needed as specified above by JKR.
0
 
LVL 86

Expert Comment

by:jkr
ID: 40021727
Um, integers won't have a double-type separator such as a comma or a semicolon in the 1st place ;o)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question