The program was successfully compiled using Borland 5.02.

The program controls the input type by user using ASCII codes. The input mode is similar to QuickBasic's INKEY.

The program is working and tested okay. Much effort had been put in, but the problem is that the subroutines for handling string is not working yet. Also string to numeric conversion is not working. They have been remarked out.

Need some expert tips to enable subroutines and string to numeric variable conversion to work.

regards,
YongBM

#include <stdio.h>
#include <stdlib>
#include <string>
#include <math>
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;
int xc=0;
int condi;
string xcon;

int getche(void);
int main(void)
{
  string xconcate;
  string efield="";
  // Weight Condition 3 Numbers and decimals only
  string sweight="";
  efield="Weight";
  condi=3;
//  sweight=datainp(efield,condi);
//  cout << "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;
// char xcon, *endpter;
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
        {
          //   SUBROUTINE for handling backspacing NOT WORKING!!
          //   spaz(c,concate);
          if (c !=8)    // not backspace
          {
            cout << "\x08";
          }
          else
          {
            int len = concate.length();
            if (len >0)
            {
              cout << " ";
              cout << "\x08";
              concate=concate.substr(0,len-1);
            }
            else
            {
              cout << " ";
            }
          }
        }
      }
  } 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.
  {
    xcon=concate;
    cout << "\n  Weight entered as string = " << xcon;
    //  convert string to numeric variable
//    weight = strtod(xcon,&endptr);
    cout <<"\n\n  strtod(xcon,&endptr) Conversion from string to number is not working.. \n  Test weight is numeric, weight*10 = " << weight*10 << " " << endl;
  }
  //
  msg();
  c = getche();
  if (c != 27) {system("CLS"); goto redo;}
//  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;
}    //
*/

Is there a particular reason why you're doing this the hard way, with a "roll your own" per-character input?

Because there are standard library functions to read a whole line for you.

Is there a particular reason why you're doing this the hard way, with a "roll your own" per-character input?

Because there are standard library functions to read a whole line for you.

Yes, you are right. The standard library function can be used to read the whole line. However, my knowledge of C++ is very limited. I know that you need to press enter to complete an entry for a field, like cin for example. But a user may type in 35 lower case characters and press enter only to realize that the program must require certain type of input for consistency, like upper case only.

Perhaps there are some functions which can do it. I am not aware of them. I would like to force a user to enter only certain input types to preempt error in data entry. I have posted only 20% of my codes to save space. The full codes will allow only specific input like Upper case only, Proper case only, Numeric with dash only, Numeric with decimal points only, etc, etc. for different fields respectively, and the input types will vary according to the fields. So I have to preset the requirement or condition using the int condi for each entry, where the ASCII codes are used to specifically force the correct input type, eg. when condi==3, only Numeric variable and a Decimal point is allowed. The present program does not prevent entry of more than one decimal points. Perhaps an error trap need to be added for this purpose.

Of course, you are right. It is quite tedious in such codings. But my users love the consistency of input type for the various fields, and preemption of wrong input type. For instance, a user cannot enter a character for weight -- it would not register -- only numeric variables and decimal are allowed. I have seen C++ programs going into infinite loops where there was no control over input types.

So, this situation leads to the next thing, which explains why I need to use subroutines. I do not have problem with functions which handles numeric parameters, but I have big problems with handling string parameters in function calls. Programming C++ applications is not like QuickBasic or Foxpro which handle such thing very easily.

For example I can use strtod() to convert to numeric variable from a string variable obtained using cin, but I cannot use it to convert to numeric from a string variable obtained by getche() and string concatenation. In QuickBasic and Foxpro, they are treated the same, e,g, Weight=val(sWeight), where Weight is numeric and sWeight is string.

Please do not get me wrong. I am not putting down C++ in the kingdom of C++ enthusiasts. I Think it is a very powerful language, like a very sharp knife, I must know how to handle it with care and skill. I believe it is very much more powerful than QuickBasic and Foxpro put together. And that is why I am here seeking help to avail myself of this powerful tool, and the expertise of many of you here in this forum who not only know how to program very well indeed in C++ but can write and argue very well, very logically, very reasonably and very cogently. I think there are many geniuses here.

I am sure some of you, C++ veterans know that my problems with subroutine and conversion can be solved with greater understanding of string variables, and how they are supposed to be used as parameters in function calls. My problem is that even though the compiler, Borland 5.02 did not find any bugs, It reported External Errors on compiling. And compiling will fail to create an EXE file. I have to remark out some codes to remove the function calls to make the program work to a very limited extend.

Please help.

Thanks.

You read a line
You validate a line
If the line isn't what the user was asked to provide, then print an error message and read again.

You read a line
You validate a line
If the line isn't what the user was asked to provide, then print an error message and read again.

I understand that your suggestion will simplify things for programmers. However, sometimes an easy way out may not be the best solution. My users have tasted input control using structured foxpro database data files where input types are predetermined, and users can not enter something else inadvertently. Remember what Mahatma Ghandi said about customers?

But that was not my problem because the codings for per character data entry with ASCII code control seem to work fine.

My problems are actually with the C++codings for function calls where parameters are strings, which would be manipulated inside function calls before returning to the calling routine with some strings already modified, using string& and data conversion from string to numeric input. Some C++ examples, like strtod() work okay by themselves, but not when incorporated into my program.
value=input.length() sometimes work; sometimes do not work; sometimes it give wrong answers.

I found that
if (condi=5) {.....}

will cause condi to take the value of 5, which confused me.

if (condi==5) {...}
seems to work fine.

I am still learning, sometimes finding out bugs the hard way when my program behaved illogically.

I need some C++ experts to lend their hands and brains to correct my mistakes. Hopefully these saviors could enable hard learning to be softened a bit.

If a full-fledged data type control input function can be developed, perhaps as a header, I believe it would benefit many C++ programmers in this forum. I am thinking that two parameters need to be passed to such a function, the description of the field, e.g. "Weight" and a flag -- I used condi (short for condition, foxpro use 4 letter words -- not swear word -- e.g. like brow for browse). The flag will define the scope of permissible input characters using ASCII codes.

Thanks for your suggestion anyway.

Somebody in this forum linked me momentarily to the profile of a 16 year old genius by the name of William something born in Portugal. I was deeply impressed by some of his work in games programming, presumably using C++. Could that person provide me that link again. I would like to view William's fantastic works again.

Indirectly Mr. Salem has solved my problem. After reading his profile and his postings in other threads, I must salute him.

I have downloaded codeblock8.02 as suggested by Mr. Salem in another thread, and managed to compile my program successfully, with minimal changes, like changing stdlib to stdlib.h. The subroutines are working fine now. It is incredible that Borland 5.02 could be full of bugs. So, my program is working now. I shall post the much shortened working codes c/w subroutines for manipulating string parameters in a new thread with the yet unresolved problem of conversion from string to numeric variable.

I think a variable need to be declared as char, like input[80] in order to use strtod() for the conversion to numeric variable e.g. double. My question is how to convert a string variable to numeric variable.
If char has to be used, how to convert string to char?

As far as the inkey data type input control and subroutines are concerned, the problem in this thread has been resolved.

Thanks, Mr. Salem.

Hi yonghc,

If you need the exact functionality of the Inkey function as its usually implemented in various BASIC dialects, then you might want to look at the Win32 Console Api. To the best of my limited knowledge, none of the C or C++ libraries provide this exact functionality. It has been my experience at least that all the stdio or conio functions 'block' awaiting some event such as hitting a key. This is not how Inkey traditionally worked. In the DOS world at least (you had mentioned QuickBasic), Inkey was probably implemented by checking a flag in the keyboard registers, and immediately returned and didn't block.

The Windows Console Api includes functions such as ReadConsoleInput() and PeekConsoleInput() that could be used to create an exact duplication of how Basic's Inkey works.

As you know, using these techniques, you'll have the keypress before it is displayed, and you can decide whether to disgard it or use it.

Dear Mr. Frederick,

Thanks for your feedback. Can Windows Console Api be incorporated into C++ and compiled using CODE::BLOCK? Forget about Boralnd 5.02.

How about kbhit() function in C++? How does it compare with the functionality of Basic's INKEY?

I just checked that one out and it does look like Inkey. I wasn't aware of it. I possibly could have used that years ago in a project I did.

Anyway, with the Win32 console functions you would be able to do mouse stuff too. I've worked with that more than the keyboard stuff. Yes, all those functions work with CodeBlocks, Dev-Cpp, VC6-9, etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.