Problem with string to numeric conversion

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

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

Problem with string to numeric conversion

 
0
  #1
Sep 8th, 2009
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.

  1. // inkey-Working by YBM
  2. #include <conio.h>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string>
  8. #include <math.h>
  9. using namespace std;
  10.  
  11. string spaz(int,string);
  12. string datainp(string,int);
  13. void msg() {cout << "\nPress Esc to Exit, Any other key to continue."; }
  14. void presskey()
  15. {
  16. cout << "\n\nAny other key to continue.";
  17. cin.ignore();
  18. // cin.get();
  19. // system("CLS");
  20. }
  21.  
  22. void msg2()
  23. {
  24. cout << "\n// INPUT TYPE CONTROL";
  25. cout << "\n// ------------------";
  26. cout << "\n// Weight Condition 3 Numbers and decimals only ";
  27. cout << "\n\n";
  28. }
  29.  
  30. char *endptr;
  31. double weight;
  32. std::string sweight;
  33. int xc=0;
  34. int condi;
  35. std::string xcon;
  36. char xconcate;
  37. int getche(void);
  38. int main(void)
  39. {
  40. const char* xconcate;
  41. string efield="";
  42. // Weight Condition 3 Numbers and decimals only
  43. sweight="";
  44. efield="Weight";
  45. condi=3;
  46. sweight=datainp(efield,condi);
  47. cout << "\n Validated Numbers and decimal, Weight: " << sweight << endl;
  48. return 0;
  49. }
  50. // Data Entry subroutine
  51. string datainp(string efield,int condi)
  52. {
  53. int xcond=condi;
  54. int c;
  55. int extended = 0;
  56.  
  57. redo:
  58. std::string concate = "";
  59. msg2();
  60. msg();
  61. condi==xcond;
  62. cout << "\nCondition :" << condi << endl;
  63. cout << "\n\n Enter "<< efield << ": ";
  64. do
  65. {
  66. c = getche();
  67. if (c==27) { break; }
  68. char inp=c; // assign char from ASCII code Backspace's ASCII code is 8
  69. if (!c)
  70. extended = getche();
  71. if (extended) { }
  72. else if (condi==3) { // Only Numbers and decimal allowed
  73. if ((c >=48 && c <=57) || (c>=46 && c<=46))
  74. {
  75. concate += inp; // string concatenation.. The character isn't extended
  76. }
  77. else { concate=spaz(c,concate);}
  78. }
  79. } while (c != 13); // ascii 13 is <enter> or <carriage return>
  80. int len = concate.length();
  81. cout << "\n String length =" << len << " chars" << endl;
  82. if (condi==3) // Coversion from string to numeric.
  83. {
  84. cout << "\n Weight entered as string = " << concate;
  85. // convert string to numeric variable
  86. // weight = strtod(concate,&endptr);
  87. cout <<"\n\n strtod(concate,&endptr) Conversion from string to number is not working.. \n Test weight is numeric, weight*10 = " << weight*10 << " " << endl;
  88. }
  89. return concate;
  90. } // main terminate
  91.  
  92. // SUBROUTINE TO MOVE CURSOR POSITION
  93. string spaz(int xc,string xconcate)
  94. {
  95. if (xc !=8) // not backspace
  96. {
  97. cout << "\x08";
  98. }
  99. else
  100. {
  101. int len = xconcate.length();
  102. if (len >0)
  103. {
  104. cout << " ";
  105. cout << "\x08";
  106. xconcate=xconcate.substr(0,len-1);
  107. }
  108. else
  109. {
  110. cout << " ";
  111. }
  112. }
  113. return xconcate;
  114. }
Last edited by cscgal; Sep 8th, 2009 at 7:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,626
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: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Problem with string to numeric conversion

 
-7
  #2
Sep 8th, 2009
>>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.
Last edited by Ancient Dragon; Sep 8th, 2009 at 5:04 pm.
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
  #3
Sep 8th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,626
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: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Problem with string to numeric conversion

 
-7
  #4
Sep 8th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,626
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: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Problem with string to numeric conversion

 
-7
  #5
Sep 8th, 2009
>>BTW, how does c_str() work? I will read about c_str() later.
Don't read about it later -- DO IT NOW.
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
  #6
Sep 9th, 2009
Originally Posted by Ancient Dragon View Post
>>BTW, how does c_str() work? I will read about c_str() later.
Don't read about it later -- DO IT NOW.
Dear Ancient Dragon,

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
*******************************
  1. // cstr2.cpp
  2. // Must compile using CODE::BLOCK
  3. // Borland 5.02 does not work
  4. #include <iostream>
  5. #include <string>
  6. #include <stdlib.h>
  7. using namespace std;
  8. void presskey()
  9. {
  10. cout << "\n\nAny other key to continue.";
  11. cin.ignore();
  12. cin.get();
  13. system("CLS");
  14. }
  15. char *endptr;
  16. double weight=0;
  17. int main()
  18. {
  19. string concate; // declare a string variable
  20. int len; // find out the number of characters in the string
  21. cout << "Enter Weight: ";
  22. getline(cin, concate); // input string
  23. len=concate.length(); //find the number of characters in the string
  24.  
  25. char xxc[len]; // initialize array
  26. concate.copy( xxc, len ); //fills array in order with characters from the string
  27.  
  28. weight=strtod(xxc,&endptr);
  29. cout << "Weight = " << weight;
  30.  
  31. presskey();
  32.  
  33. return 0;
  34. }
  35.  
  36. *****************
  37. END
  38.  
  39.  
  40. START
  41. **************************************
  42. This is how i have incorporated the strtod() inside my program, where they were remarked out earlier:
  43.  
  44. int len = concate.length();
  45. cout << "\n String length =" << len << " chars" << endl;
  46. if (condi==3) {
  47. // convert string to numeric variable
  48. char xxc[len]; // intialise array
  49. concate.copy(xxc,len); // fills in array with characters from the string
  50. weight = strtod(xxc,&endptr); // weight declared as global double
*************************8******************
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
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

 
0
  #7
Sep 12th, 2009
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
  1. char * myChar = "some text"
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
  1. #include <string>
  2. int main()
  3. {
  4.  
  5. std::string str;
  6. char * c = "No cover!";
  7.  
  8. str = c; //you can assing a c-string to a string
  9. str[3]='h';//you can access the string as if it were an array
  10.  
  11. char const * c2 = str.c_str(); //it has to be const
  12.  
  13. std::cout << c << std::endl ;
  14. std::cout << str << std::endl ;
  15. std::cout << c2 << std::endl ;
  16.  
  17. //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
  18.  
  19. c[3] = 'h';
  20.  
  21. system("pause");
  22.  
  23. }
Last edited by namehere05; Sep 12th, 2009 at 12:08 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,444
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 187
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Problem with string to numeric conversion

 
0
  #8
Sep 12th, 2009
To convert string to numeric datatype look here
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
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
  #9
Sep 12th, 2009
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,
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
  #10
Sep 12th, 2009
Dear firstPerson,

Thank you for the references to templates on conversions. It is about STL?

Regards,
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC