Problem with string to numeric conversion

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 1,142
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 144
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Re: Problem with string to numeric conversion

 
0
  #11
Sep 12th, 2009
Originally Posted by yonghc View Post
Dear firstPerson,
It is about STL?
What do you mean?
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: namehere05 is an unknown quantity at this point 
Solved Threads: 1
namehere05 namehere05 is offline Offline
Newbie Poster

Re: Problem with string to numeric conversion

 
1
  #12
Sep 12th, 2009
Originally Posted by yonghc View Post

I have tested your codes. Need to use CODE::BLOCK. Need to add #include <iostream>, else errors will be reported and will not compile.
You got that right

Originally Posted by yonghc View Post
BTW, what C++ compiler are you using?
dev c++ you can find it here (1st page in google for "dev c++")
http://www.bloodshed.net/devcpp.html

Im a novice myself so I feel somehow uncomfortable with your comments, hehe
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 55
Reputation: yonghc is an unknown quantity at this point 
Solved Threads: 0
yonghc yonghc is offline Offline
Junior Poster in Training

Re: Problem with string to numeric conversion

 
0
  #13
Sep 13th, 2009
Originally Posted by firstPerson View Post
What do you mean?
Surely you're joking Mr. firstPerson. STL is for Standard Template Library.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 55
Reputation: yonghc is an unknown quantity at this point 
Solved Threads: 0
yonghc yonghc is offline Offline
Junior Poster in Training

Re: Problem with string to numeric conversion

 
0
  #14
Sep 13th, 2009
Originally Posted by namehere05 View Post
You got that right



dev c++ you can find it here (1st page in google for "dev c++")
http://www.bloodshed.net/devcpp.html

Im a novice myself so I feel somehow uncomfortable with your comments, hehe
Dear Sir,
Thanks for the info. I have added dev c++ as my 3rd C++compiler. Since various flavors of C++ are used in this forum, I need to try out the codes on the compiler which works.

Actually, i sincerely appreciate your help very much. Never mind being a novice. It is altruistic, and that is more important.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,142
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 144
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Re: Problem with string to numeric conversion

 
0
  #15
Sep 13th, 2009
Originally Posted by yonghc View Post
Surely you're joking Mr. firstPerson. STL is for Standard Template Library.
One of the few times someone has called be Mr.
No I know what STL is, my question was with your response
It is about STL?
I didn't quite get your question.
Last edited by firstPerson; Sep 13th, 2009 at 12:41 pm.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 55
Reputation: yonghc is an unknown quantity at this point 
Solved Threads: 0
yonghc yonghc is offline Offline
Junior Poster in Training

Re: Problem with string to numeric conversion

 
0
  #16
Sep 13th, 2009
Originally Posted by firstPerson View Post
One of the few times someone has called be Mr.
No I know what STL is, my question was with your response

I didn't quite get your question.

Just kidding. You have read about Mr. Feynman? It just came to my mind. I could not understand about STL. So, I guess since you understand about STL, you got to be as smart as Professor Feynman. Of course you know what STL mean, else you would not have written about it. Thanks for your input.

I am thankful to all of you -- Frederick2, namehere05, firstPerson and last but not least Ancient Dragon -- for all the help I am receiving, I have a much better understanding about string, c-string (there is a g-string in C++?), and their conversions to numeric variable.

The question raised in this thread is solved. The final program codes for this thread are shown below:

  1. // string2num.cpp YBM assisted by namehere05, et al.
  2. // String to char conversion, char to numeric conversion using strtod() illegal entries will be ignored.
  3. // Formatted output
  4. // Must compile using CODE::BLOCK
  5. // Borland 5.02 does not work
  6. #include <stdio.h>
  7. #include <conio.h>
  8. #include <iostream>
  9. #include <string>
  10. #include <stdlib.h>
  11. #include <fstream>
  12. #include <iomanip>
  13. using namespace std;
  14. int c=0;
  15.  
  16. // Subroutine to fix decimal places
  17. void decima(int &dec)
  18. {
  19. cout << setiosflags(ios::fixed |ios::showpoint) << setprecision(dec);
  20. }
  21.  
  22. void waiting()
  23. {
  24. cout << "\n\nEsc to Exit.\n";
  25. c=getch();
  26. }
  27. char *endptr;
  28. double weight=0;
  29.  
  30. int main()
  31. {
  32. system ("color 0a"); // green text on black background
  33. string concate; // declare a string variable
  34. int len; // find out the number of characters in the string
  35. cout << "\n\nThis program uses copy() and c_str() to convert string to char (c-string), \nthen char to numeric. Illegal entries will be ignored instead of program crashing";
  36. cout << "\n\nNormal rather than exponential and decimal places can be preset.\n\n";
  37. do
  38. {
  39. int dec=10;
  40. decima(dec);
  41. cout << "\n\nEnter Weight: ";
  42. getline(cin, concate); // input string
  43. len=concate.length(); //find the number of characters in the string
  44. char xxc[len]; // initialize array
  45. concate.copy( xxc, len ); //fills array in order with characters from the string
  46. char const * c2 = concate.c_str(); // using c_str()
  47. weight=strtod(xxc,&endptr);
  48. cout << "\nWeight from copy() : " << weight;
  49. weight=strtod(c2,&endptr);
  50. cout << "\nWeight from c_str() : " << weight;
  51. cout << "\n\nWeight displayed : " << setw(20) << weight << endl;
  52. waiting();
  53. } while (c !=27);
  54. return 0;
  55. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 55
Reputation: yonghc is an unknown quantity at this point 
Solved Threads: 0
yonghc yonghc is offline Offline
Junior Poster in Training

Re: Problem with string to numeric conversion

 
0
  #17
Sep 18th, 2009
In view of my better understanding of c-style string and C++ string, this is an update of some codes posted by namehere05. Some of the new things I learnt are as follows:

1. Add the line "using namespace std;" in order to obviate the need to use std:: before cout and endl, etc., like std::cout <<... to save typing std::

2. C-style strings are arrays. Their elements can be accessed and manipulated, like C++ string.

3. When pointers are used for char* data type, elements of a C-style string, say c can be accessed, but can not be replaced with, say c[3]='h'. However for normal C-Style string declared like chax[]="123.456", elements can be replaced with, say chax[3]='0';

4. strtod() function can be used for the conversion of C-Style string variable to numeric variable for C-Style string declared normally with [] or as pointer type C-Style string. See updated codes below:

Please note that #include <string> is only necessary for Borland. CODE::BLOCK and Dev-C++ do not need it.

  1. // string5 originally written by namehere05.
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. int main() {
  6. system ("color 0a"); // green text on black bkgrd
  7. double numeric;
  8. string str;
  9. char *endptr;
  10. char * c = "No cover!"; // declaration of c-style string
  11. char chax[] = "123.45"; // Declaraition of c-style string
  12. string ch = "123.45"; // Decalration of c-style string
  13. string* pc;
  14. str = chax; //you can assign a c-string to a string
  15.  
  16. cout << "\nSTRING MANPULATIONS AND CONVERSION TO NUMERIC VARIABLE\n\n";
  17. cout << "\nC-STYLE STRING chax";
  18. char *cstr = "Hello";
  19. printf( "\nThe second character of %s is %c.\n",
  20. cstr, cstr[1] );
  21. cout << "\nBefore replacement of 3rd char with X, chax : " << chax;
  22. chax[2]='X';
  23. cout << "\nAfter replacement of 3rd char with X,chax : " << chax;
  24. chax[2]='3';
  25. cout << "\nAfter replacement of 3rd char with 3, chax : " << chax;
  26. cout << "\n\nC++ STRING str and C-STYLE STRING chax MANIPULATION ";
  27. cout << "\n\nstr's initial value : "<< str;
  28. str[3]='0';//you can access the string as if it were an array
  29. cout << "\nstr after str[3]='0' : " << str;
  30. pc = &ch; // pc is assigned address of c
  31. cout << "\n\nch is assigned value : " << ch;
  32. cout << "\nAddress of &ch assigned to pc : " << pc;
  33. cout << "\nContent pointed by *pc : " << *pc;
  34. cout << "\n\nchax c-style = " << chax ;
  35. str=chax;
  36. cout << "\nstr = chax = " << str << endl ;
  37. //cout << "\nc2 from conv = " << c2 << std::endl ;
  38.  
  39. // C-style string manipulation
  40.  
  41. chax[3] = 'h';
  42. cout << "\nafter chax[3] = 'h', content of chax is : " << chax;
  43. str[3]='0';
  44. cout << "\nafter str[3] = '0', content of str is : " << str;
  45. char const * c2 = str.c_str(); //it has to be const
  46. numeric = strtod(chax,&endptr);
  47. cout << "\n\nnumeric from strtod conversion of chax ignoring illegal char onwards : " << numeric;
  48. numeric = strtod(c2,&endptr);
  49. cout << "\n\nnumeric from strtod conversion of str where all numbers are converted : " << numeric;
  50.  
  51. std::cout << "\n\n";
  52. system("pause");
  53. }

Some notes on pointer type string courtesy of www.cplusplus.com is reproduced below:

Declaring variables of pointer types
Due to the ability of a pointer to directly refer to the value that it points to, it becomes necessary to specify in its declaration which data type a pointer is going to point to. It is not the same thing to point to a char as to point to an int or a float.

The declaration of pointers follows this format:

type * name;

where type is the data type of the value that the pointer is intended to point to. This type is not the type of the pointer itself! but the type of the data the pointer points to. For example:

int * number;
char * character;
float * greatnumber;


These are three declarations of pointers. Each one is intended to point to a different data type, but in fact all of them are pointers and all of them will occupy the same amount of space in memory (the size in memory of a pointer depends on the platform where the code is going to run). Nevertheless, the data to which they point to do not occupy the same amount of space nor are of the same type: the first one points to an int, the second one to a char and the last one to a float. Therefore, although these three example variables are all of them pointers which occupy the same size in memory, they are said to have different types: int*, char* and float* respectively, depending on the type they point to.

I want to emphasize that the asterisk sign (*) that we use when declaring a pointer only means that it is a pointer (it is part of its type compound specifier), and should not be confused with the dereference operator that we have seen a bit earlier, but which is also written with an asterisk (*). They are simply two different things represented with the same sign.

Comments please if any.

Regards,
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Problem with string to numeric conversion

 
-7
  #18
Sep 18th, 2009
>>Please note that #include <string> is only necessary for Borland. CODE::BLOCK and Dev-C++ do not need it.

That may be true, but its always good programming practice to include it anyway so that the code is portable across compilers.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Problem with string to numeric conversion

 
0
  #19
Sep 18th, 2009
Originally Posted by yonghc View Post
For example:

int * number;
char * character;
float * greatnumber;


These are three declarations of pointers. Each one is intended to point to a different data type, but in fact all of them are pointers and all of them will occupy the same amount of space in memory (the size in memory of a pointer depends on the platform where the code is going to run). Nevertheless, the data to which they point to do not occupy the same amount of space nor are of the same type: the first one points to an int, the second one to a char and the last one to a float. Therefore, although these three example variables are all of them pointers which occupy the same size in memory, they are said to have different types: int*, char* and float* respectively, depending on the type they point to.
It's more of a common convenience these days, but strictly speaking I don't believe that's true. I wouldn't dwell on it all that much, though.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 55
Reputation: yonghc is an unknown quantity at this point 
Solved Threads: 0
yonghc yonghc is offline Offline
Junior Poster in Training

Re: Problem with string to numeric conversion

 
0
  #20
Sep 18th, 2009
Originally Posted by Ancient Dragon View Post
>>Please note that #include <string> is only necessary for Borland. CODE::BLOCK and Dev-C++ do not need it.
That may be true, but its always good programming practice to include it anyway so that the code is portable across compilers.
Agreed. That is a very good piece of advice. I will remember that. Thanks.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC