By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

Seperate numbers from String as double

P: 6
HI All;

I am writing a software for a machine and it is working.But the out put comes as a string in this format:

Expand|Select|Wrap|Line Numbers
  1. READOUT "  -0.01 V,  -0.04 A"  /n
Is it possible to get the two values i.e -0.01 and 0.04 into two double variables?This is necessary as this has to be exported to a function in this format.

Thanks in advance and regards
Jun 18 '07 #1
Share this Question
Share on Google+
5 Replies


Savage
Expert 100+
P: 1,764
HI All;

I am writing a software for a machine and it is working.But the out put comes as a string in this format:

Expand|Select|Wrap|Line Numbers
  1. READOUT "  -0.01 V,  -0.04 A"  /n
Is it possible to get the two values i.e -0.01 and 0.04 into two double variables?This is necessary as this has to be exported to a function in this format.

Thanks in advance and regards
By output do you mean output to the file or output to the screen.If it is to the file check article in article section:

How to parse a line?

Savage
Jun 18 '07 #2

P: 6
Hi Savage;
Its is to be posted to a file.But its by a dedicated function.So if its possible to have the values in 2 variables lets say a=0.04 and b=0.01 it will be okay.

Thnx and regards
Jun 18 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
READOUT " -0.01 V, -0.04 A" /n
Use a stringstream. Insert our string into the stream an extract into separate variables.
Expand|Select|Wrap|Line Numbers
  1. void YourFunction(char* yourstring)
  2. {
  3. stringstream ss;
  4. ss << yourstring;
  5. char str[20];
  6. char ch;
  7. double d1;
  8. double d2;
  9. //READOUT  "   -0.01       V        ,  -0.04        A       "  /n
  10.           V        V       V       V       V       V        V       V
  11. ss >> str >> ch >> d1 >> ch >> ch >> d2 >> ch >> ch;
  12. ss.ignore();  //eat the \n
  13. {
  14.  
Jun 18 '07 #4

Savage
Expert 100+
P: 1,764
Hi Savage;
Its is to be posted to a file.But its by a dedicated function.So if its possible to have the values in 2 variables lets say a=0.04 and b=0.01 it will be okay.

Thnx and regards
If you are using c++ you can use formatedd output overloaded operator >>,but if you are using c you will need to read as unformated data and then convert to double using atof.

Savage
Jun 18 '07 #5

P: 6
Hi Savage;
Thanks for the info.I am using C.So I will try to use the atof().

Expand|Select|Wrap|Line Numbers
  1. ss >> str >> ch >> d1 >> ch >> ch >> d2 >> ch >> ch;
Does this line try to extract all the individual parts?

Regards
Jun 19 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.