| | |
Problem with string to numeric conversion
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2009
Posts: 55
Reputation:
Solved Threads: 0
I am inclined to suspect that Borland C++ compiler 5.02 is seemingly full of bugs. Even though my C++ codes could be bug free, it reported External errors on compiling and failed to create an EXE file to run.
Thanks to Mr. Salem, I have successfully compiled the above codes using the new and free C++ compiler CODE::BLOCK8.02 suggested by him in another thread. The program now works as it is supposed to work, except for the part on string to numeric conversion.
I still encounter problem with conversion of string variable to numeric variable using strtof() function call. It seems that that function requires a char instead of string for the conversion. Is it possible then to assign the values of a string variable to a char variable and use that function for conversion?
Is there any other way to do the conversion of a string to numeric variable?
The problematic part has been remarked out to prevent failure in compiling the codes.
// convert string to numeric variable
// weight = strtod(concate,&endptr);
Could somebody please help?
Thanks.
Thanks to Mr. Salem, I have successfully compiled the above codes using the new and free C++ compiler CODE::BLOCK8.02 suggested by him in another thread. The program now works as it is supposed to work, except for the part on string to numeric conversion.
I still encounter problem with conversion of string variable to numeric variable using strtof() function call. It seems that that function requires a char instead of string for the conversion. Is it possible then to assign the values of a string variable to a char variable and use that function for conversion?
Is there any other way to do the conversion of a string to numeric variable?
The problematic part has been remarked out to prevent failure in compiling the codes.
// convert string to numeric variable
// weight = strtod(concate,&endptr);
Could somebody please help?
Thanks.
C++ Syntax (Toggle Plain Text)
// inkey-Working by YBM #include <conio.h> #include <iostream> #include <iomanip> #include <stdio.h> #include <stdlib.h> #include <string> #include <math.h> using namespace std; string spaz(int,string); string datainp(string,int); void msg() {cout << "\nPress Esc to Exit, Any other key to continue."; } void presskey() { cout << "\n\nAny other key to continue."; cin.ignore(); // cin.get(); // system("CLS"); } void msg2() { cout << "\n// INPUT TYPE CONTROL"; cout << "\n// ------------------"; cout << "\n// Weight Condition 3 Numbers and decimals only "; cout << "\n\n"; } char *endptr; double weight; std::string sweight; int xc=0; int condi; std::string xcon; char xconcate; int getche(void); int main(void) { const char* xconcate; string efield=""; // Weight Condition 3 Numbers and decimals only sweight=""; efield="Weight"; condi=3; sweight=datainp(efield,condi); cout << "\n Validated Numbers and decimal, Weight: " << sweight << endl; return 0; } // Data Entry subroutine string datainp(string efield,int condi) { int xcond=condi; int c; int extended = 0; redo: std::string concate = ""; msg2(); msg(); condi==xcond; cout << "\nCondition :" << condi << endl; cout << "\n\n Enter "<< efield << ": "; do { c = getche(); if (c==27) { break; } char inp=c; // assign char from ASCII code Backspace's ASCII code is 8 if (!c) extended = getche(); if (extended) { } else if (condi==3) { // Only Numbers and decimal allowed if ((c >=48 && c <=57) || (c>=46 && c<=46)) { concate += inp; // string concatenation.. The character isn't extended } else { concate=spaz(c,concate);} } } while (c != 13); // ascii 13 is <enter> or <carriage return> int len = concate.length(); cout << "\n String length =" << len << " chars" << endl; if (condi==3) // Coversion from string to numeric. { cout << "\n Weight entered as string = " << concate; // convert string to numeric variable // weight = strtod(concate,&endptr); cout <<"\n\n strtod(concate,&endptr) Conversion from string to number is not working.. \n Test weight is numeric, weight*10 = " << weight*10 << " " << endl; } return concate; } // main terminate // SUBROUTINE TO MOVE CURSOR POSITION string spaz(int xc,string xconcate) { if (xc !=8) // not backspace { cout << "\x08"; } else { int len = xconcate.length(); if (len >0) { cout << " "; cout << "\x08"; xconcate=xconcate.substr(0,len-1); } else { cout << " "; } } return xconcate; }
Last edited by cscgal; Sep 8th, 2009 at 7:02 pm.
>>I still encounter problem with conversion of string variable to numeric variable using strtof() function call. It seems that that function requires a char instead of string for the conversion
If you had bothered to read the methods available to std::string you would have seen c_str(), which returns const char*. Next time read about the classes you are using instead of blindly tossing code to the screen hoping the compiler will fix your bugs for you.
And you should not have made this a "code snippet" because it ain't one.
If you had bothered to read the methods available to std::string you would have seen c_str(), which returns const char*. Next time read about the classes you are using instead of blindly tossing code to the screen hoping the compiler will fix your bugs for you.
And you should not have made this a "code snippet" because it ain't one.
Last edited by Ancient Dragon; Sep 8th, 2009 at 5:04 pm.
•
•
Join Date: Aug 2009
Posts: 55
Reputation:
Solved Threads: 0
Dear Ancient Dragon,
Your feedback and advice are noted with thanks.
BTW, how does c_str() work? I will read about c_str() later.
Actually, what is a code snippet supposed to be? I have checked the dictionary. Snippet means short extract, and I have posted a short extract of my program codes in the code window.
I have been wondering where to post the codes otherwise. Is it supposed to be in the comment window then?
Please explain.
Your feedback and advice are noted with thanks.
BTW, how does c_str() work? I will read about c_str() later.
Actually, what is a code snippet supposed to be? I have checked the dictionary. Snippet means short extract, and I have posted a short extract of my program codes in the code window.
I have been wondering where to post the codes otherwise. Is it supposed to be in the comment window then?
Please explain.
A "code snipped" is supposed to be some code you have finished and want to show everyone else to help them solve a similar problem. That's why there are no "reply" or "quote" links in your thread so that others can help you. Unfortunately Dani has decided, unwisely IMO, to mix code snippets in the same forum as c++ questions, confusing people like you who really don't know the difference between them.
•
•
Join Date: Aug 2009
Posts: 55
Reputation:
Solved Threads: 0
•
•
•
•
>>BTW, how does c_str() work? I will read about c_str() later.
Don't read about it later -- DO IT NOW.
Thank you very much for your patience and explanations. I am in awe of your acumen and formidable reputation in this forum.
So sorry, Sir for the late reply. I was exhausted after getting less than an hour of sleep during the last 24 hours, meeting deadlines.
I appreciate your methodical approach to C++ programming, and I respect people like you who are disciplined. Being an Ex-USAF, (fighter pilot?), and programmer, it is no wonder that you could contribute so much in this forum.
Because of time constraint, I have developed a bad habit of doing things the quick and dirty way, googling for solutions, studying other peoples codes, and modifying them for my own use without reading up about classes as you have suggested. I must find time to read up on C++ the programming of which I knew almost nothing before I post my first ever codes here a couple of days back. But i do know a little bit of QuickBasic and Foxpro for DOS and Windows, a little bit of battle-field experience, and of course I have known all along that C++ is the most powerful programming language on earth. However, it is not easy to get the hang of it, not like being spoon-fed by those high level languages such as those 4thGL languages.
I googled for answer to the problem raised by me in this thread, and I seem to have found the answer. But, the numeric expression with exponential after more than 7 digits, including the decimal point is not what i expected or desired. I am not sure if the format could be changed to show just numbers and not a scientific expression.
This is the way I have tested the codes for the conversion of a string to characters before using strtod() function call:
Actually, i thought they were the same, string and character variables. But they are not the same. Certain functions can be done on one and not the other and vice vesa, confusing me a lot.
START
*******************************
C++ Syntax (Toggle Plain Text)
// cstr2.cpp // Must compile using CODE::BLOCK // Borland 5.02 does not work #include <iostream> #include <string> #include <stdlib.h> using namespace std; void presskey() { cout << "\n\nAny other key to continue."; cin.ignore(); cin.get(); system("CLS"); } char *endptr; double weight=0; int main() { string concate; // declare a string variable int len; // find out the number of characters in the string cout << "Enter 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 weight=strtod(xxc,&endptr); cout << "Weight = " << weight; presskey(); return 0; } ***************** END START ************************************** This is how i have incorporated the strtod() inside my program, where they were remarked out earlier: int len = concate.length(); cout << "\n String length =" << len << " chars" << endl; if (condi==3) { // convert string to numeric variable char xxc[len]; // intialise array concate.copy(xxc,len); // fills in array with characters from the string weight = strtod(xxc,&endptr); // weight declared as global double
END
Pending feedbacks and critiques, I would like to consider the problem solved in a couple of days. Any comments?
I would like to pursue the manner numbers are expressed on screen, How to handle it, perhaps in another thread. I will take note of your kind advice and post my question not in the code snippets, lest it be ignored by most of the other participants in this forum.
Thank you for your kind assistance and advice. I got to go back to sleep. Goodnight, Sir.
Regards,
Last edited by John A; Sep 12th, 2009 at 12:20 am. Reason: added code tags
•
•
Join Date: Dec 2008
Posts: 21
Reputation:
Solved Threads: 1
wow, a lot to read I wonder if he will.... you are kinda being selfish if you expect him to read it all.
c-style strings like this
is avaible due
1. is valid C ( myChar is a pointer and it can point to any part of the memory)
2. Compatibility issues with C, the predecessor of C++
but working with 'em both is not expected to give you lots of trouble
Hope this code helps
c-style strings like this
C++ Syntax (Toggle Plain Text)
char * myChar = "some text"
1. is valid C ( myChar is a pointer and it can point to any part of the memory)
2. Compatibility issues with C, the predecessor of C++
but working with 'em both is not expected to give you lots of trouble
Hope this code helps
C++ Syntax (Toggle Plain Text)
#include <string> int main() { std::string str; char * c = "No cover!"; str = c; //you can assing a c-string to a string str[3]='h';//you can access the string as if it were an array char const * c2 = str.c_str(); //it has to be const std::cout << c << std::endl ; std::cout << str << std::endl ; std::cout << c2 << std::endl ; //note that you cant do the following, it compiles but crashes, the reason is that C++ doesnt store the c-style string as an array c[3] = 'h'; system("pause"); }
Last edited by namehere05; Sep 12th, 2009 at 12:08 am.
•
•
Join Date: Aug 2009
Posts: 55
Reputation:
Solved Threads: 0
Dear namehere05,
Thank you very much indeed for the clear and succinct explanation on a rather confusing string class to a novice like me.
Like Confucius said, "With great doubts comes great understanding."
You have initiated a process of gradually attaining great understanding, not only for me, but for many others out there, including students and professionals, etc. in my position.
I have tested your codes. Need to use CODE::BLOCK. Need to add #include <iostream>, else errors will be reported and will not compile.
BTW, what C++ compiler are you using?
Regards,
Thank you very much indeed for the clear and succinct explanation on a rather confusing string class to a novice like me.
Like Confucius said, "With great doubts comes great understanding."
You have initiated a process of gradually attaining great understanding, not only for me, but for many others out there, including students and professionals, etc. in my position.
I have tested your codes. Need to use CODE::BLOCK. Need to add #include <iostream>, else errors will be reported and will not compile.
BTW, what C++ compiler are you using?
Regards,
![]() |
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 |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






