>>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
*******************************
// 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
*************************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,