943,729 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2037
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Sep 12th, 2009
0

Re: Problem with string to numeric conversion

Click to Expand / Collapse  Quote originally posted by yonghc ...
Dear firstPerson,
It is about STL?
What do you mean?
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Sep 12th, 2009
1

Re: Problem with string to numeric conversion

Click to Expand / Collapse  Quote originally posted by yonghc ...

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

Click to Expand / Collapse  Quote originally posted by yonghc ...
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
Reputation Points: 11
Solved Threads: 1
Light Poster
namehere05 is offline Offline
25 posts
since Dec 2008
Sep 13th, 2009
0

Re: Problem with string to numeric conversion

What do you mean?
Surely you're joking Mr. firstPerson. STL is for Standard Template Library.
Reputation Points: 39
Solved Threads: 0
Junior Poster in Training
yonghc is offline Offline
55 posts
since Aug 2009
Sep 13th, 2009
0

Re: Problem with string to numeric conversion

Click to Expand / Collapse  Quote originally posted by namehere05 ...
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.
Reputation Points: 39
Solved Threads: 0
Junior Poster in Training
yonghc is offline Offline
55 posts
since Aug 2009
Sep 13th, 2009
0

Re: Problem with string to numeric conversion

Click to Expand / Collapse  Quote originally posted by yonghc ...
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
Quote ...
It is about STL?
I didn't quite get your question.
Last edited by firstPerson; Sep 13th, 2009 at 12:41 pm.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Sep 13th, 2009
0

Re: Problem with string to numeric conversion

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:

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 39
Solved Threads: 0
Junior Poster in Training
yonghc is offline Offline
55 posts
since Aug 2009
Sep 18th, 2009
0

Re: Problem with string to numeric conversion

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.

C++ Syntax (Toggle Plain Text)
  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,
Reputation Points: 39
Solved Threads: 0
Junior Poster in Training
yonghc is offline Offline
55 posts
since Aug 2009
Sep 18th, 2009
-7

Re: Problem with string to numeric conversion

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Sep 18th, 2009
0

Re: Problem with string to numeric conversion

Click to Expand / Collapse  Quote originally posted by yonghc ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 18th, 2009
0

Re: Problem with string to numeric conversion

>>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.
Reputation Points: 39
Solved Threads: 0
Junior Poster in Training
yonghc is offline Offline
55 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Noob - Program Keeps Crashing
Next Thread in C++ Forum Timeline: API Flag Setting, is there no better way?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC