Solved

Convert string to integer

Posted on 2014-04-07
10
214 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
10 Comments
 
LVL 68

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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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 8

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Problem: You created a new custom form in Outlook for your contacts (added fields, deleted fields, changed the layout of fields, whatever) and made it the default form for contacts. The good news is that all new contacts will utilize the new form. T…
This collection of functions covers all the normal rounding methods of just about any numeric value.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now