| | |
Problem with string to numeric conversion
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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?•
•
Join Date: Dec 2008
Posts: 21
Reputation:
Solved Threads: 1
•
•
•
•
I have tested your codes. Need to use CODE::BLOCK. Need to add #include <iostream>, else errors will be reported and will not compile.
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
•
•
Join Date: Aug 2009
Posts: 55
Reputation:
Solved Threads: 0
Surely you're joking Mr. firstPerson. STL is for Standard Template Library.
•
•
Join Date: Aug 2009
Posts: 55
Reputation:
Solved Threads: 0
•
•
•
•
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
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.
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.
No I know what STL is, my question was with your response
•
•
•
•
It is about STL?
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?•
•
Join Date: Aug 2009
Posts: 55
Reputation:
Solved Threads: 0
•
•
•
•
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)
// string2num.cpp YBM assisted by namehere05, et al. // String to char conversion, char to numeric conversion using strtod() illegal entries will be ignored. // Formatted output // Must compile using CODE::BLOCK // Borland 5.02 does not work #include <stdio.h> #include <conio.h> #include <iostream> #include <string> #include <stdlib.h> #include <fstream> #include <iomanip> using namespace std; int c=0; // Subroutine to fix decimal places void decima(int &dec) { cout << setiosflags(ios::fixed |ios::showpoint) << setprecision(dec); } void waiting() { cout << "\n\nEsc to Exit.\n"; c=getch(); } char *endptr; double weight=0; int main() { system ("color 0a"); // green text on black background string concate; // declare a string variable int len; // find out the number of characters in the string 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"; cout << "\n\nNormal rather than exponential and decimal places can be preset.\n\n"; do { int dec=10; decima(dec); cout << "\n\nEnter Weight: "; getline(cin, concate); // input string len=concate.length(); //find the number of characters in the string char xxc[len]; // initialize array concate.copy( xxc, len ); //fills array in order with characters from the string char const * c2 = concate.c_str(); // using c_str() weight=strtod(xxc,&endptr); cout << "\nWeight from copy() : " << weight; weight=strtod(c2,&endptr); cout << "\nWeight from c_str() : " << weight; cout << "\n\nWeight displayed : " << setw(20) << weight << endl; waiting(); } while (c !=27); return 0; }
•
•
Join Date: Aug 2009
Posts: 55
Reputation:
Solved Threads: 0
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.
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,
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)
// string5 originally written by namehere05. #include <iostream> #include <string> using namespace std; int main() { system ("color 0a"); // green text on black bkgrd double numeric; string str; char *endptr; char * c = "No cover!"; // declaration of c-style string char chax[] = "123.45"; // Declaraition of c-style string string ch = "123.45"; // Decalration of c-style string string* pc; str = chax; //you can assign a c-string to a string cout << "\nSTRING MANPULATIONS AND CONVERSION TO NUMERIC VARIABLE\n\n"; cout << "\nC-STYLE STRING chax"; char *cstr = "Hello"; printf( "\nThe second character of %s is %c.\n", cstr, cstr[1] ); cout << "\nBefore replacement of 3rd char with X, chax : " << chax; chax[2]='X'; cout << "\nAfter replacement of 3rd char with X,chax : " << chax; chax[2]='3'; cout << "\nAfter replacement of 3rd char with 3, chax : " << chax; cout << "\n\nC++ STRING str and C-STYLE STRING chax MANIPULATION "; cout << "\n\nstr's initial value : "<< str; str[3]='0';//you can access the string as if it were an array cout << "\nstr after str[3]='0' : " << str; pc = &ch; // pc is assigned address of c cout << "\n\nch is assigned value : " << ch; cout << "\nAddress of &ch assigned to pc : " << pc; cout << "\nContent pointed by *pc : " << *pc; cout << "\n\nchax c-style = " << chax ; str=chax; cout << "\nstr = chax = " << str << endl ; //cout << "\nc2 from conv = " << c2 << std::endl ; // C-style string manipulation chax[3] = 'h'; cout << "\nafter chax[3] = 'h', content of chax is : " << chax; str[3]='0'; cout << "\nafter str[3] = '0', content of str is : " << str; char const * c2 = str.c_str(); //it has to be const numeric = strtod(chax,&endptr); cout << "\n\nnumeric from strtod conversion of chax ignoring illegal char onwards : " << numeric; numeric = strtod(c2,&endptr); cout << "\n\nnumeric from strtod conversion of str where all numbers are converted : " << numeric; std::cout << "\n\n"; system("pause"); }
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,
>>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.
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.
•
•
•
•
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.
"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
![]() |
Similar Threads
- atof problem (string to double) (C++)
- string conversion (Python)
- String to Double Conversion without using strtod() or atof() (C)
- Check a string is numeric or not (Java)
- String to integer conversion (C)
- Problem with string variable in void prnt function (C++)
Other Threads in the C++ Forum
- Previous Thread: Noob - Program Keeps Crashing
- Next Thread: API Flag Setting, is there no better way?
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






