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.
// 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;
}