473,805 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Smart conversion from a string to different type of numbers

I have a C++ code that is reading a list of parameters from a file.

PARAMETERS stringParam="st ringValue", intParam=4, doubleParam =
3.533, ... END

The values can be strings as well as integers or doubles, and I don't
know what they will be ( I don't know how the parameter list will look
like) . At the moment I have a mechanism that select all the values
and read them into C++ strings, regardless of their type.

What I need is a function that converts smartly my strings into
numbers: for instance, if I fed it with "4", it would return me the
type "integer" and store the value 4 accordingly, whereas if I fed it
with a "3.533" it would return the type "double" and store the value
3.533.

Any idea as to something like that exists already?

Thanks in advance

Daniele

Apr 30 '07 #1
3 1386
>>>>"d" == d avitabile <d.*********@gm ail.comwrites:

dI have a C++ code

.....which you asked about in comp.lang.c.

comp.lang.c++ is down the hall. Good luck.

Charlton
--
Charlton Wilbur
cw*****@chromat ico.net
Apr 30 '07 #2
d.*********@gma il.com wrote On 04/30/07 13:44,:
I have a C++ code that is reading a list of parameters from a file.

PARAMETERS stringParam="st ringValue", intParam=4, doubleParam =
3.533, ... END

The values can be strings as well as integers or doubles, and I don't
know what they will be ( I don't know how the parameter list will look
like) . At the moment I have a mechanism that select all the values
and read them into C++ strings, regardless of their type.

What I need is a function that converts smartly my strings into
numbers: for instance, if I fed it with "4", it would return me the
type "integer" and store the value 4 accordingly, whereas if I fed it
with a "3.533" it would return the type "double" and store the value
3.533.
If you want a C++ solution, try a C++ newsgroup.

In C, I'd approach the problem by using a struct
and a union, something like:

struct number {
enum { ERROR, LONG, DOUBLE } type;
union {
long l;
double d;
} value;
};

.... with a function that first tries to convert the string
with strtol(), and if that doesn't work tries again with
strtod(), and gives up if neither works:

#include <stdlib.h>

struct number
getNumber(const char *string)
{
struct number n;
char *endp;

n.value.l = strtol(string, &endp, 10);
if (endp != string && *endp == '\0') {
n.type = LONG;
return n;
}

n.value.d = strtod(string, &endp);
if (endp != string && *endp == '\0') {
n.type = DOUBLE;
return n;
}

n.type = ERROR;
return n;
}

A C++ addict might take a different approach.

--
Er*********@sun .com
Apr 30 '07 #3
d.*********@gma il.com wrote:
I have a C++ code that is reading a list of parameters from a file.
Bummer. The C++ language is not any more topical in comp.lang.c than
are COBOL, LISP, or Fortran. My opinion of that bloated language
designed for obfuscation will be left unsaid.
Apr 30 '07 #4

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

Similar topics

5
5067
by: Bolin | last post by:
Hi all, A question about smart pointers of constant objects. The problem is to convert from Ptr<T> to Ptr<const T>. I have look up and seen some answers to this question, but I guess I am too stupid to understand and make them work. E.g. I have read that boost's smart pointers are able to do this convertion, but the following code doesn't compile (VC++6.0):
5
3038
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a templatized conversion operator defined as follows:
7
3262
by: Michael Lehn | last post by:
Hi, I have a question regarding the conversion of objects. When is the conversion done by the constructor and when by the operator. My feeling tells me that the constructor is preferred. But I couldn't find the exact rule in the C++ standard. And what if the classes have template parameters? It would be great if somebody could get me a rough hint where in the
4
6489
by: Nikhil Patel | last post by:
Hi all, I am a VB6 programmer and learning C#. I am currently reading a chapter on types. I have question regarding enums. Why do we need to convert enum members to the value that they represent? Thanks in advance... -Nikhil
15
2442
by: Peter Afonin | last post by:
Hello, I'm struggling with the string conversion to MD5 which I've never user before. I have a string that I need to encode which looks approximately like this: "pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Email=petera_gudzon.net:lang =ru:shp_PaymentNo=20040825205308:shp_UserID=pva:shp_Price=2.95:shp_HostPlan= BU:shp_Term=2"
3
6942
by: Sean S - Perth, WA | last post by:
Hi all, I'm wondering if there is a way to find (to strip or process) smart quotes in text submitted via a form? These don't work: strOutput = Replace(strOutput, "“", "“") ' left smart quote char as replace value strOutput = Replace(strOutput, Chr(147), "“") ' my best guess at where the
14
8920
by: sharmaharish | last post by:
I need a conversion function that converts values from string to a particular type. For this I have a template function that looks like this ... template<class T> T value(const string& s) { istringstream(s); T val; is >val;
33
5088
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
69
2655
by: Bill Reid | last post by:
This is how I handle a check that the last character of a text file is a newline: /* checks if newline is last character of text file */ unsigned check_text_file_newline_termination(FILE *test_file) { int end_char; fseek(test_file,-1L,SEEK_END); end_char=getc(test_file);
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10356
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.