472,805 Members | 911 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

Converting String to Integer or Double

Its very simple in VC++. In the followeing code I have declared a
String, and a double than I am taking the string and converting it into
Double. getch() at the end is only to pause the screen so you can see
the result.
#include <conio.h>
#include <iostream>
using namespace std;
void main ()

{
string S ; //
S = "990123";
double D = double();

D = atoi( S.c_str());

cout << D << " <--- Interger String -----> " <<S.c_str();
getch();
}

Jul 23 '05 #1
4 3694
Its very simple in VC++. In the followeing code I have declared a
String, and a double than I am taking the string and converting it into

Double. getch() at the end is only to pause the screen so you can see
the result.
#include <conio.h>
#include <iostream>
using namespace std;
void main ()
{
string S ; //
S = "990123";
double D = double();
D = atoi( S.c_str());
cout << D << " <--- Interger String -----> "
<<S.c_str();
getch();
}

Jul 23 '05 #2
ra***********@yahoo.com wrote:
Its very simple in VC++. In the followeing code I have declared a
String, and a double than I am taking the string and converting it into
Double. getch() at the end is only to pause the screen so you can see
the result.
#include <conio.h>
#include <iostream>
using namespace std;
void main ()

{
string S ; //
S = "990123";
double D = double();

D = atoi( S.c_str());

cout << D << " <--- Interger String -----> " <<S.c_str();
getch();
}

try to use stringstreams . they are much more type-safe.
Jul 23 '05 #3
The Boost library has a 'lexical_cast' class that will do this with
stringstreams, so you don't have to mess with it.

It's not too difficult to do of course, but it provides a nice wrapper.
So here's the lexical_cast version of the original poster's code:
#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>

using namespace std;

void main ()
{
string S = "990123";

double D = lexical_cast<double>(S);

cout << D << " <--- Interger String -----> " <<S;
}

Additional comments about the original code:
Why are you using conio.h? Okay, I realize that getch() provides a nice
'get any key' function, but you can replace it with something like
cin.ignore and require the user to press enter to get a portable
solution.

Second suggestion is to try to put variable initialization at the same
place as the declaration as I have above.

Especially in the case of something like double D = double(); . What
your code does (assuming no optimization) is to assign 0 to D then
immediately assign the value of atoi(S.c_str()); to D. I realize that
premature microoptimizations like this are usually a bad idea, but that
doesn't mean you should try to create more work. Adding the '=
double()' there has no purpose and, IMO, reduces readability instead of
enhancing it.

Finally, why '<< S.c_str(); '? The C++ streams know about the string
class (or more accurately, the string class knows about the C++
streams), so '<< S' will work fine.

Jul 23 '05 #4
Sorry I guess when I typed I some how missed my question. My original
question was is there a better way to do what I am doing. And second I
do agree that declaring and initializing variables like this reduces
readability, but I think it makes more sense that a variable should be
assigned when it is created just to be on the safe side. Then I know
that my variable contains no garbage.

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Cyde Weys | last post by:
I'm currently working on converting a simulator program from Visual Basic 6.0 to Visual C++ .NET. I've figured out most of the stuff, but there's still one thing I haven't gotten to and I've never...
25
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual...
2
by: Asbjørn Ulsberg | last post by:
Hi. I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0 module to C#. I've managed to convert the VB code to VB.NET, which gave me the following code: Option Strict On Option...
2
by: D. Shane Fowlkes | last post by:
Here's a good one. I've been using an Excel spreadsheet for the past couple of years to calculate a file's Estimated Download Time based off of a solid 50kbs connection (dial up). This is for a...
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
2
by: Alex Buell | last post by:
Is there an elegant way of converting strings containing digits between different number bases in C++? I.e.: 10 (base 2) = 2 (base 10) FF (base 16) = 256 (base 10) F (base 16) = 1111 (base 2)...
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
10
by: Ron | last post by:
I want to calculate the surface area of a sphere from an inputed radius with option strict on. I guess I am not converting something correctly. Here is what I am doing: I have a textbox...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.