After a brief and rocky relation, I have had jilted Borland 5.02 and found new love for CODE::BLOCK only to discover a while ago that Borland could compile a code snippet which CODE::BLOCK couldn't.

In the working program produced below, CODE::BLOCK gave out an error message and failed to compile the original codes, where I used Void main(void) in the main function; it was adamant that it would only accept int main(void).

Then I realized that I should not have abandoned Borland, recalling fond memories that when you highlighted a C++ keyword, and pressed F1, you could get help readily, c/w explanations and examples of usage. CODE::BLOCK8.02 could not do this. In this respect, it is less user-friendly than Borland.

So, Borland need not be discarded, since its help is handily available. Perhaps, it can coexist with CODE::BLOCK, playing a different role, perhaps for lookup on explanation and usage of keywords, etc.

However, I have also discovered out that Borland has bad habits, being sometimes unpredictable and infested with bugs, and failing to compile on bug-free codes with the message "External errors" and could even tolerate the "Wrong thing" in compiling C++ codes with Void main(void), without any complaint, which I found to be like mixing with the wrong company.

Now, coming back to the main purpose of this thread, as shown in the title.. In the C++ codes below, I have remarked out // inp="", otherwise it would cause compiling to fail. So, I have used inp=NULL, and was let off with a warning, and compiling could be completed, and the program could run.

But, that line inp=NULL had been ignored on execution of the program. :(

It looks like a simple assignment of a character variable to NULL, for those familiar with other high level computer languages but i have not figured out how to do it in C++ yet.

I believe that the time a self-taught person take to learn C++ programming or any other skills in isolation would range from 3 X more to infinity -- which means not in a lifetime -- more compared with a person lucky to be pointed the right direction by a good and knowledgeable mentor, ceteris paribus. I also believe in Karma and that what goes around, comes around.

There is an intention to develop functions to make C++ programs more user-friendly. Ultimately, it is hoped that extended ASCII codes produced by pressing those Up, Dn, Left, Right, PgUp and PgDn keys could be used for data entry where Input type could be predetermined, and user is pre-empted from making errors before confirming with the Enter key. And the cursor can move from field to field, and page to page using extended keys mentioned above.

So, please help.

// ASCII-getche    ASCII from keypress
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

int main(void) {
system ("color 0a");
int c=0;
char inp;
//inp="":
do {
     system("CLS");
     cout << "\n\n\n\n\n\tThis program produces ASCII code for any key pressed.";
     cout << "\n\n\n\n\n\tChar : " << inp << " ASCII: " << c <<"\n\n\n\tEsc to Exit,\tPresskey a key: ";
     c = getche();  //* assign ASCII code to c
     inp=c;    // CHR$ from ASCII     if extended, c=0
     if (c==0) {
     c=getche();
     inp=NULL;
     }
   }  while (c != 27);    /* ESC to escape*/
   cout << "\n\n\n\tBye\n\n\n";
   getch();
}

Don't know if this will help, but these little fixes cause it to run in CodeBlocks without errors or warnings....

// ASCII-getche
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

int main(void)
{
 system ("color 0a");
 int c=0;
 char inp;
 inp=' ';    //Since you are saying inp is a char, give it a char
 do
 {
     system("CLS");
     cout << "\n\n\n\n\n\tThis program produces ASCII code for any key pressed.";
     cout << "\n\n\n\n\n\tChar : " << inp << " ASCII: " << c <<"\n\n\n\tEsc to Exit,\tPresskey a key: ";
     c = getche();  //* assign ASCII code to c
     inp=c;    // CHR$ from ASCII     if extended, c=0
     if(c==0)
     {
      c=getche();
      inp=(char)NULL;  //tell it to consider NULL a char, i.e., the NULL char
     }
   } while (c != 27);    /* ESC to escape*/
   cout << "\n\n\n\tBye\n\n\n";
   getch();
}

Dear Frederick2,

Thanks for your prompt like-speed-of-light reply.

Now there is some progress -- no more letting off with a warning. Nevertheless, that line was still ignored, else PgDn should not display "Q". The extended ASCII code of PgDn is the same as Q, 81.

I suspect there is a bug in the codes.

I put together a C++ version of the basic Inkey function. Its not exact. My intention was to make it exact, but I ran into some excrutiating problems. The Inkey$ function returns a dynamic basic string (allocated by the OLE String Engine). As such, it can contain null characters. I have my own string class, but it isn't based on Ole strings, but rather on Asciiz null terminated strings, and as such I can't embed nulls in the string. Therefore, I wasn't able to return a string from my string class that has a null as the first character, and the extended character code in the 2nd byte (cursor motion keys and so forth). So what I did was create a typedef like so...

typedef struct KEY
{
 bool blnKeyPressed;   //1 byte
 bool blnExtended;     //1 byte
 char ch;              //1 byte
 char code;            //1 byte
}KEY;

Here is my Inkey function which gets passed by reference a KEY struct...

void Inkey(KEY& k)
{
 int ch;

 memset(&k,0,4);
 if(_kbhit())
 {
    ch=getch();
    if(ch==0x00||ch==0xE0)
    {
       k.blnExtended=true,  k.blnKeyPressed=true;
       k.ch='\0',           k.code=getch();
    }
    else
    {
       k.blnExtended=false, k.blnKeyPressed=true;
       k.ch=(char)ch,       k.code=(char)ch;
    }
 }
 else
    k.blnKeyPressed = false;
}

Here is a program that uses it...

//Main.cpp
#include <stdio.h>
#include <conio.h>
#include <string.h>


typedef struct KEY
{
 bool blnKeyPressed;   //1 byte
 bool blnExtended;     //1 byte
 char ch;              //1 byte
 char code;            //1 byte
}KEY;


void Inkey(KEY& k)
{
 int ch;

 memset(&k,0,4);
 if(_kbhit())
 {
    ch=getch();
    if(ch==0x00||ch==0xE0)
    {
       k.blnExtended=true,  k.blnKeyPressed=true;
       k.ch='\0',           k.code=getch();
    }
    else
    {
       k.blnExtended=false, k.blnKeyPressed=true;
       k.ch=(char)ch,       k.code=(char)ch;
    }
 }
 else
    k.blnKeyPressed = false;
}


int main(void)
{
 KEY key;
 
 printf("key.blnKeyPressed\tkey.blnExtended\t\tkey.ch\t\tkey.code\n");
 printf("========================================================================\n");
 while(1)
 {
  Inkey(key);
  if(key.blnKeyPressed)
  {
     if(key.code==13||key.code==27)
        break;
     printf("%u\t\t\t%u\t\t\t%c\t\t%u\n",key.blnKeyPressed,key.blnExtended,key.ch,key.code);
  }
 };

 return 0;
}

I used VC++ 6 to create the program, and while I've a half dozen other compilers, I've only tested it in VC6. Give it a shot. I think you'll find it works. You can end the program by hitting either the [ENTER] key or escape key. This represents your first step in the achievement of your goal of being able to do BASIC like input in a C or C++ program. Another easier possibility would be to just use a 32 bit BASIC compiler that can create consoles and has a good implementation of the basic language. Personally, I use PowerBASIC.

Dear Mr. Frederick2,

Thank you very much indeed for the effort you put in to assist me. With your help and namehere05's help, I think I have got the ASCII program as shown below working.

It works under Borland, dev C++ and CODE::BLOCK.

BTW, where do you download the Microsoft Visual C++ from ? Is it free?

The program below will ring a bell if an extended key is pressed.

What I have found out using the program are as follows:
1. Where normal ASCII codes are concerned, all compilers tested are in agreement. I guess the odd out would definitely get into trouble and may be rejected by users.

2. CODE::BLOCK and dev c++ captured the first byte of extended ASCII codes as follows: Up, Dn, Left, Right, PgUp, PgDn, F11 and F12 -> ASCII code 224 which is alpha. Function keys F1 to F10 -> ASCII code 0, char not displayed. NULL?

3. Borland captured first byte of extended ASCII code as corresponding characters of second byte, with the same ASCII code, Q for ASCII 81. It displays all kinds of characters for Function keys, but the ASCII codes are correct.

But, despite differences of first byte captured by different compilers, it does not matter, pressing an extended key will ring a bell in the program, and we can use the second byte captured which are consistent for all compilers, Borland included.

With this foundation, I believe C++ programmers may be able to write more user friendly programs, which is actually my intention, not that I would like to use Basic-like inkey input. I think most user-friendly programs can do it. I mean you press a key to activate a subroutine, like F1 for Help, F2 for Edit, F10 to print, etc, Dn Up, Left, Right, to move to various fields, etc. without having to press <Enter> every time.

BTW, how do you assign a variable beside NULL to a character variable. your inp =(char)NULL is working alright. Thanks.

// ASCII-getche   ASCII from keypress    by YBM assisted by Frederick2 and namehere05  Sep 2009
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

int main(void) {
system ("color 0a");
int c=0;
string inps;
char inp;
do {
     system("CLS");
     cout << "\n\n\n\n\n\tThis program is supposed to produce ASCII code for any key pressed.";
     cout << "\n\n\n\n\n\tChar : " << inps << " ASCII: " << c <<"\n\n\n\tEsc to Exit,\tPresskey a key (1st byte): ";
     c = getche();  //* assign ASCII code to c
     cout << "ASCII: " << c;
     inp=c;    // CHR$ from ASCII     if extended, c=0
     inps=inp;
     if (c==0x00||c==0xE0) {
     c=getche();
     cout << "\x7";    // ring a bell
//     inp=(char)NULL;
      inps="Extended";       // inp=(char)"Extended" does not work, something else appeared.  How to assign to character variable? learnt from namehere05 that you can assign char to string, but not vice versa.
     }
   }  while (c != 27);    /* ESC to escape*/
   cout << "\n\n\n\tBye\n\n\n";
   getch();
}
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.