Solved

Convert string to integer

Posted on 2014-04-07
10
234 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 70

Assisted Solution

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

Accepted Solution

by:
jkr earned 250 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

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ā€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationā€¦
Viewers will learn the different options available in the Backstage view in Excel 2013.
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.
Suggested Courses

615 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